Leetcode刷题第三周

字符串:

反转字符串

344

class Solution {
    public void reverseString(char[] s) {
        // 左右指针
        int leftNode = 0;
        int rifhtNode = s.length - 1;
        char temp;
        while(leftNode <= rifhtNode){
            temp = s[rifhtNode];
            s[rifhtNode] = s[leftNode];
            s[leftNode] = temp;
            leftNode++;
            rifhtNode--;
        }
    }
}

反转字符串 II

541

class Solution {
    public String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        for(int i = 0; i < arr.length; i=i+2*k){
            if((i+k)<=arr.length){
                reverse(arr,i,i+k-1);
            }else{
                reverse(arr,i,arr.length-1);
            }
        }
        return new String(arr);
    }
    public void reverse(char[] arr, int left, int right){
        while(left < right){
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
    }
}

替换空格

offer 05

class Solution {
    public String replaceSpace(String s) {
        StringBuffer target = new StringBuffer();
        char temp;
        for(int i = 0; i < s.length(); i++){
            temp = s.charAt(i);
            if(temp == ' '){
                target.append("%20");
            }else{
                target.append(temp);
            }
        }
        return new String(target);
    }
}

反转字符串中的单词

151

class Solution {
    public String reverseWords(String s) {
        StringBuffer buffer = new StringBuffer();
        int index = 0;
        while(s.charAt(index)==' '){
            index++;
        }
        for(;index < s.length();index++){
            if(s.charAt(index)!=' '){
                buffer.append(s.charAt(index));
            }else{
                while(index < s.length() && s.charAt(index)==' '){
                    index++;
                }
                if(index < s.length()){
                    buffer.append(' ');
                    buffer.append(s.charAt(index));
                }
            }
        }
        String arr = new String(buffer);
        String[] result = arr.split(" ");
        int left = 0;
        int right = result.length - 1;
        while(left < right){
            String temp = result[left];
            result[left] = result[right];
            result[right] = temp;
            left++;
            right--;
        }
        StringBuffer buffer1 = new StringBuffer();
        for(int a = 0; a < result.length; a++){
            buffer1.append(result[a]);
            if(a < result.length - 1){
                buffer1.append(" ");
            }
            
        }
        return new String(buffer1);
    }
}

左旋转字符串

Offer 58 - II

class Solution {
    public String reverseLeftWords(String s, int n) {
// 先整体反转,在根据k进行部分反转
        char[] str = s.toCharArray();
        reverse(str, 0, str.length - 1);
        reverse(str, 0, str.length - 1 - n);
        reverse(str, str.length - n, str.length - 1);
        return new String(str);
    }
    public void reverse(char[] str, int start, int end){
        while(start < end){
            str[start] ^= str[end];
            str[end] ^= str[start];
            str[start] ^= str[end];
            start++;
            end--;
        }
    }
}

找出字符串中第一个匹配项的下标

KMP字符串匹配:在主串中寻找子串的过程,称为模式匹配
KMP的主要思想是当出现字符串不匹配时,可以知道一部分之前已经匹配的文本内容,可以利用这些信息避免从头再去做匹配了。
前缀表:记录下标i之前(包括i)的字符串中,有多大长度的相同前缀后缀。
28

class Solution {
    public int strStr(String haystack, String needle) {
        int[] arr = kmp(needle);
        for(int i = 0, j = 0; i < haystack.length(); i++){
            while(j > 0 && haystack.charAt(i) != needle.charAt(j)){
                j = arr[j - 1];
            }
            if(haystack.charAt(i) == needle.charAt(j)){
                j++;
            }
            if(j == needle.length()){
                return i - j + 1;
            }
        }
        return -1;
    }
    public int[] kmp(String needle){
        int[] next = new int[needle.length()];
        for(int i = 1, j = 0; i < next.length; i++){
            while(j > 0 && needle.charAt(i) != needle.charAt(j)){
                j = next[j - 1];
            }
            if(needle.charAt(i) == needle.charAt(j)){
                j++;
            }
            next[i] = j;
        }
        return next;
    }
}

重复的子字符串

459

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int[] next = new int[s.length()];
        next[0] = 0;
        for(int i = 1, j = 0; i < s.length(); i++){
            while(j > 0 && s.charAt(i) != s.charAt(j)){
                j = next[j - 1];
            }
            if(s.charAt(i) == s.charAt(j)){
                j++;
            }
            next[i] = j;
        }
        if(next[next.length - 1] != 0 && next.length%(next.length - next[next.length - 1]) == 0){
            return true;
        }
        return false;
    }
}
posted @ 2022-11-13 19:40  novice_programmer_oo  阅读(37)  评论(0)    收藏  举报