代码随想录day8|344. 反转字符串541. 反转字符串 II剑指 Offer 05. 替换空格151. 反转字符串中的单词剑指 Offer 58 - II. 左旋转字符串

代码随想录day8|344. 反转字符串541. 反转字符串 II剑指 Offer 05. 替换空格151. 反转字符串中的单词剑指 Offer 58 - II. 左旋转字符串

344. 反转字符串

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

思路:左右指针

541. 反转字符串 II

class Solution {
    public String reverseStr(String s, int k) {
        if(s.length() < 2)return s;
        char[] chs = s.toCharArray();
        for(int i = 0 ; i < chs.length ; i=i+k+k){
            reverseStr(chs,i,i+k-1>chs.length-1?chs.length-1:i+k-1);
        }
        return String.valueOf(chs);
    }
    void reverseStr(char[] chs , int left , int right){
        while(left < right){
            char temp = chs[left];
            chs[left] = chs[right];
            chs[right] = temp;
            left++;
            right--;
        }
    }
}

思路:将字符串变为字符数组,再按2k循环并反转

剑指 Offer 05. 替换空格

class Solution {
    public String replaceSpace(String s) {
        if(s == null || s.length() == 0)return s;

        StringBuilder str = new StringBuilder();
        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[] chars = s.toCharArray();
        while(left >= 0){
            if(chars[left] ==' '){
                chars[right--] = '0';
                chars[right--] = '2';
                chars[right] = '%';
            }else{
                chars[right] = chars[left];
            }
            left--;
            right--;
        }
        return new String(chars);
    }
}

思路:记录空格数,建一个新字符串,长度为空格数*2+原字符串长度,在从后往前遍历,遇到空格加%20

151. 反转字符串中的单词

class Solution {
    public String reverseWords(String s) {
        //1.去除首尾以及中间多余的空格
        StringBuilder sb = removeSpace(s);
        //2.反转整个字符串
        reverseString(sb,0,sb.length() - 1);
        //3.反转各个单词
        reverseEachWord(sb);
        return sb.toString();
    }
    StringBuilder removeSpace(String s){
        int start = 0;
        int end = s.length() - 1;
        while(s.charAt(start) == ' ')start++;
        while(s.charAt(end) == ' ')end--;
        StringBuilder sb = new StringBuilder();
        while(start <= end){
            char c = s.charAt(start);
            if(c != ' ' || sb.charAt(sb.length() - 1) != ' '){
                sb.append(c);
            }
            start++;
        }
        return sb;
    }
    void reverseString(StringBuilder sb ,int start , int end){
        while(start < end){
             char temp = sb.charAt(start);
            sb.setCharAt(start, sb.charAt(end));
            sb.setCharAt(end, temp);
            start++;
            end--;
        }
    }
    void reverseEachWord(StringBuilder sb){
        int start = 0;
        int end = 1;
        int n = sb.length();
        while(start < n){
             while (end < n && sb.charAt(end) != ' ') {
                end++;
            }
            reverseString(sb, start, end - 1);
            start = end + 1;
            end = start + 1;
            
        }
    }
}

思路:去掉多余空格,反转整个字符串,再反转单词

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

class Solution {
    public String reverseLeftWords(String s, int n) {
        char[] chs = s.toCharArray();
        reverseLeftWords(chs,0,n%s.length()-1);
        reverseLeftWords(chs,n%s.length(),s.length()-1);
        reverseLeftWords(chs,0,s.length()-1);
        return String.valueOf(chs);
    }
    char[] reverseLeftWords(char[] chs , int left , int right){
        while(left < right){
            char temp = chs[left];
            chs[left] = chs[right];
            chs[right] = temp;
            left++;
            right--;
        }
        return chs;
    }
}

思路:分别反转前k个,k+1到最后,再反转全部字符串

你可能感兴趣的:(算法,c++,leetcode)