Day 8 | 344. Reverse String | 541. Reverse String II | 替换空格 | 151.Reverse Words in a String | 左旋转字符串

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum


LeetCode 344. Reverse String

Question Link

Solution:

class Solution {
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;
        while(left < right){
            s[left] ^= s[right];
            s[right] ^= s[left];
            s[left] ^= s[right];
            left++;
            right--;
        }
    }
}

class Solution {
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length-1;
        while(left < right){
            char temp = s[right];
            s[right] = s[left];
            s[left] = temp;
            right--;
            left++;
        }
    }
}

Thought:

  • We can adopt Double Pointer Method
  • Use ^= to exchange the position of left and right

LeetCode 541. Reverse String II

Question Link

Solution:

class Solution {
    public String reverseStr(String s, int k) {
        char[] ch = s.toCharArray();
        for(int i = 0; i < ch.length; i+=2*k){
            int left = i;
            // determine the position of right
            int right = Math.min(i+k-1, ch.length-1);
            while(left < right){
                ch[left] ^= ch[right];
                ch[right] ^= ch[left];
                ch[left] ^= ch[right];
                left++;
                right--;
            }
        }
        return new String(ch);
    }
}

Thought:

  • Let i move forward 2*k each time
  • Notice that use Math.min() to determine the position of right
  • Use new String(ch) to convert char to String

LeetCode 剑指 Offer 05. 替换空格

Question Link

Solution:

class Solution {
    public String replaceSpace(String s) {
        if(s.length() == 0 || s == null)
            return s;
        StringBuilder str = new StringBuilder();
        // 扩充2倍空格的空间数量
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == ' ')
                str.append("  ");
        }
        // 没有空格则直接返回
        if(str.length() == 0)
            return s;

        // 有空格则定义两个指针
        int left = s.length() - 1;
        s += str.toString();
        int right = s.length() - 1;
        char[] ch = s.toCharArray();
        while(left >= 0){
            if(ch[left] == ' '){
                ch[right--] = '0';
                ch[right--] = '2';
                ch[right] = '%';
            }else{
                ch[right] = ch[left];
            }
            left--;
            right--;
       }    
       return new String(ch);
    }
}

Thought:

  • Expand the array space by 2 times the number of spaces
  • Define two pointers, fill the new array from right to left

LeetCode 151. Reverse Words in a String

Question Link

Solution:

class Solution {
    public String reverseWords(String s) {
        char[] ch = s.toCharArray();
        // 1、remove the extra space at the beginning, end and middle
        ch = removeSpace(ch);
        // 2、reverse the whole String
        reverseString(ch, 0, ch.length-1);
        // 3、reverse each word
        reverseEachWord(ch);
        return new String(ch);
    }

    char[] removeSpace(char[] ch){
        int slow = 0;
        for(int fast= 0; fast < ch.length; fast++){
            // use a fast pointer to remove all space
            if(ch[fast] != ' '){
                // add space behind each word
                if(slow != 0)
                    ch[slow++] = ' ';
                // When the `fast` pointer encounters a space or traverses to the end of the string, it proves that a word has been traversed.
                while(fast < ch.length && ch[fast] != ' '){
                    ch[slow++] = ch[fast++];
                }
            }            
        }
	    // Create a new char array for resizing.
        char[] newChar = new char[slow];
        System.arraycopy(ch, 0, newChar, 0, slow);
        return newChar;
    }

    void reverseString(char[] ch, int left, int right){
        if (right >= ch.length) {
            System.out.println("set a wrong right");
            return;
        }
        while(left < right){
            ch[left] ^= ch[right];
            ch[right] ^= ch[left];
            ch[left] ^= ch[right];
            left++;
            right--;
        }
    }

    void reverseEachWord(char[] ch){
        int left = 0;
        // <= is to make the `right` always points to the position after the end of the word.
        for(int right = 0; right <= ch.length; right++){
            // When the `right` pointer reaches the end of the string or the space behind a word.
            if(right == ch.length || ch[right]==' '){
                reverseString(ch, left, right-1);
                left = right + 1;         
            }
        }
    }
}

Thought:

  • We need three steps to solving this question:
  • Remove the extra space at the beginning, end and middle
    • Double Pointer Method
    • Use the fast pointer to remove all space
    • Use the slow pointer to add a space before every word, excluding the first word.
    • When the fast pointer encounters a space or traverses to the end of the string, it proves that a word has been traversed.
    • Create a new char array for resizing.
  • Reverse the whole string
  • Reverse each word
    • <= is to make the right always points to the position after the end of the word.
    • When the right pointer rechees the end of the string or the space behind a word.

LeetCode 剑指 Offer 58 - II. 左旋转字符串

Question Link

Solution:

class Solution {
    public String reverseLeftWords(String s, int n) {
        char[] ch = s.toCharArray();
        // reverse the first n characters
        reverseString(ch, 0, n-1);
        // revere the characters after n
        reverseString(ch, n, ch.length-1);
        // reverse all characters
        reverseString(ch, 0, ch.length-1);
        return new String(ch);
    }

    void reverseString(char[] ch, int left, int right){
        int slow = 0;
        while(left < right){
            ch[left] ^= ch[right];
            ch[right] ^= ch[left];
            ch[left] ^= ch[right];
            left++;
            right--;
        }
    }
}

Thought:

  • reverse the first n characters
  • revere the characters after n
  • reverse all characters
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值