代码随想录训练营二刷第八天 | 344.反转字符串 541. 反转字符串II 剑指Offer 05.替换空格 151.翻转字符串里的单词 剑指Offer58-II.左旋转字符串

代码随想录训练营二刷第八天 | 344.反转字符串 541. 反转字符串II 剑指Offer 05.替换空格 151.翻转字符串里的单词 剑指Offer58-II.左旋转字符串

一、344.反转字符串

题目链接:https://leetcode.cn/problems/reverse-string/
思路:双指针两头交换。

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

二、541. 反转字符串II

题目链接:https://leetcode.cn/problems/reverse-string-ii/
思路:如果使用StringBuilder以及内置翻转函数会更快一些。

class Solution {
    public String reverseStr(String s, int k) {
        char[] array = s.toCharArray();
        int h = 2 * k;
        int num = s.length() / (h);
        for (int i = 0; i < num; i++) {
            reverse(array, i * h, i * h + k - 1);
        }
        if (array.length - num * h < k) {
            reverse(array, num * h, array.length - 1);
        }else {
            reverse(array, num * h, num * h + k - 1);
        }
        StringBuilder result = new StringBuilder();
        for (char c : array) {
            result.append(c);
        }
        return result.toString();
    }

    public void reverse(char[] s, int l, int r) {
        while (l < r) {
            char temp = s[l];
            s[l] = s[r];
            s[r] = temp;
            l++;
            r--;
        }
    }
}

三、剑指Offer 05.替换空格

题目链接:https://leetcode.cn/problems/ti-huan-kong-ge-lcof/
思路:字符串操作一般使用StringBuilder比较快。

class Solution {
    public String replaceSpace(String s) {
        if (s == null || s.length() == 0) return s;
        StringBuilder t = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                t.append(s.charAt(i));
            }else {
                t.append("%20");
            }
        }
        return t.toString();
    }
}

四、151.翻转字符串里的单词

题目链接:https://leetcode.cn/problems/reverse-words-in-a-string/
思路:注意空格的处理。

class Solution {
    public String reverseWords(String s) {
        StringBuilder builder = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ' ') {
                temp.append(s.charAt(i));
            }else if (temp.length() != 0){
                builder.append(temp.reverse()).append(' ');
                temp = new StringBuilder();
            }
        }
        if (temp.length() != 0) {
            builder.append(temp.reverse()).append(' ');
        }
        StringBuilder delete = builder.delete(builder.length() - 1, builder.length());
        delete.reverse();
        return delete.toString();
    }
}

五、剑指Offer58-II.左旋转字符串

题目链接:https://leetcode.cn/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/
思路:分段翻转、总体翻转

class Solution {
    public String reverseLeftWords(String s, int n) {
        StringBuilder builder = new StringBuilder();
        StringBuilder b1 = new StringBuilder(s.substring(0, n));
        StringBuilder b2 = new StringBuilder(s.substring(n));
        builder.append(b1.reverse());
        builder.append(b2.reverse());
        return builder.reverse().toString();
    }
}

你可能感兴趣的:(力扣算法题,算法)