Java Solution


  • 1
    C
        public String reverseStr(String s, int k) {
            if(k <= 1 || s.length() <= 1){
                return s;
            }
            
            char[] cArr = s.toCharArray();
            
            int len = cArr.length;
            
            for(int i=0; i<len; i = i + 2*k){
                reverse(i, Math.min(i + k-1, len - 1), cArr);
            }
            
            return String.valueOf(cArr);
        }
        
        private void reverse(int beg, int end, char[] cArr){
            while(beg < end){
                char temp = cArr[beg];
                cArr[beg++] = cArr[end];
                cArr[end--] = temp;
            }
        }
    

Log in to reply
 

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