字符串专项练习 LeetCode

344. 反转字符串
image-20221001081932091

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

541. 反转字符串 II
image-20221001084754835

字符串与字符数组转换次数不要超过1次 会出现错误 卡了一下

class Solution {
        public String reverseStr(String s, int k) {
            int len = s.length();
            int l = 0, r = 0;
            char[] ss = s.toCharArray();
            while (true) {
                if (r + k * 2 < len) {
                    r += k * 2;
                } else {
                    r = len - 1;
                }
                int left = l, right = Math.min(r,l + k - 1);
                while (left < right) {
                    char temp = ss[right];
                    ss[right] = ss[left];
                    ss[left] = temp;
                    left++;
                    right--;
                }
                l += 2 * k;
                if(r == len - 1){
                    break;
                }
            }
            return new String(ss);
        }
    }

剑指 Offer 05. 替换空格
image-20221001085234579

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

151. 反转字符串中的单词
image-20221001091211025

用正则表达式去除多余空格

class Solution {
        public String reverseWords(String s) {
            s = s.trim();
            String[] ss = s.split("\\s+");
            int len = ss.length;
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = len-1; i >=0; i--) {
                if (i != len-1 ) {
                    stringBuffer.append(" ");
                }
                stringBuffer.append(ss[i]);
            }
            return stringBuffer.toString();
        }
    }

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

class Solution {
        public String reverseLeftWords(String s, int n) {
            String ans = s.substring(n,s.length());
            ans += s.substring(0,n);
            return ans;
        }
    }

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

image-20221002092024981

KMP算法

class Solution {
        public void getNext(int[] next, String s) {
            int j = -1;
            next[0] = j;
            for (int i = 1; i < s.length(); i++) {
                while (j >= 0 && s.charAt(i) != s.charAt(j+1)) {
                    j = next[j];
                }
                if (s.charAt(i) == s.charAt(j + 1)) {
                    j++;
                }
                next[i] = j;
            }
        }

        public int strStr(String haystack, String needle) {
            if (needle.length() == 0) {
                return 0;
            }
            int[] next = new int[needle.length()];
            getNext(next, needle);
            int j = -1;
            for (int i = 0; i < haystack.length(); i++) {
                while (j >= 0 && haystack.charAt(i) != needle.charAt(j+1)) {
                    j = next[j];
                }
                if (haystack.charAt(i) == needle.charAt(j + 1)) {
                    j++;
                }
                //匹配到第一个完整的needle就退出
                if (j == needle.length() - 1) {
                    return (i - needle.length() + 1);
                }
            }
            return -1;
        }
    }

459. 重复的子字符串

image-20221002094911008

class Solution {
        public boolean repeatedSubstringPattern(String s) {
            if (s.equals("")) {
                return false;
            }
            int j = -1;
            int len = s.length();
            int[] next = new int[len];
            next[0] = j;
            for (int i = 1; i < len; i++) {
                while (j >= 0 && s.charAt(i) != s.charAt(j + 1)) {
                    j = next[j];
                }
                if (s.charAt(i) == s.charAt(j + 1)) {
                    j++;
                }
                next[i] = j;
            }
            System.out.println(Arrays.toString(next));
            if (next[len - 1] >= 0 && len % (len - next[len - 1] - 1) == 0) {
                return true;
            }
            return false;
        }
    }

字符串结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楠风丶北枝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值