Very simple c++ 12ms solution


  • 0
    K
        int numsize = nums.size();
        if (k > numsize) k = numsize;
        if (k>1 &&k == numsize && nums[0] == nums[k-1]) return true;
        if (k>1 && k == numsize) k--;
        for (int i=0;i<numsize-k;i++){
            int pos = 1;
            while (pos <=k){
                if (nums[i] == nums[pos+i] || nums[numsize-i-1] == nums[numsize-i-pos-1]) return true;
                pos++;
            }
            if (2*i+k > numsize) break;
        }
        return false;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.