string Leetcode 字符串算法题

344.反转字符串

API: StringBuffer 内部是 append 实现字符串的改变,不会每次改变字符串都创建一个新对象

java 复制代码
StringBuffer(String.valueOf(s)).reverse().toString()

不调用API, 使用双指针,分别从两端向中间聚拢

java 复制代码
class Solution {
    public void reverseString(char[] s) {
        int l = 0, r = s.length - 1;
        while(l < r){
            char chL = s[l];
            char chR = s[r];
            s[l] = chR;
            s[r] = chL;
            ++l;
            --r;
        }
    }
}

541.反转字符串 II

java 复制代码
class Solution {
    public String reverseStr(String s, int k) {
        char[] str = s.toCharArray();
        int n = str.length;
        int l = 0;
        while(l < n){
            int r = (l-1) + k;
            if(r >= n) r = n - 1;
            int nextl = (l-1)+2*k+1;
            while(l < r){
                char ch = str[l];
                str[l] = str[r];
                str[r] = ch;
                ++l;
                --r;
            }
            l = nextl;
        }
        return new String(str);
    }
}

122.路径加密/替换空格

API:

java 复制代码
path.replaceAll("\\.", " ");
java 复制代码
class Solution {
    public String pathEncryption(String path) {
        char[] p = path.toCharArray();
        for(int i = 0; i < p.length; ++ i){
            if(p[i] == '.'){
                p[i] = ' ';
            }
        }
        return new String(p);
    }
}

182.动态口令/左旋字符串

substring(begin, len)

java 复制代码
class Solution {
    public String dynamicPassword(String password, int target) {
        int n = password.length();
        return password.substring(target, n) + password.substring(0, target);
    }
}

StringBuilder

java 复制代码
class Solution {
    public String dynamicPassword(String password, int target) {
        StringBuilder res = new StringBuilder();
        for(int i = target; i < password.length() + target; ++i){
            res.append(password.charAt(i % password.length()));
        }
        return res.toString();
    }
}

源字符串上操作

28.实现strStr

找第一个匹配子串的下标

KMP

求Next数组:

  1. Next 数组长度为 n + 1,用于获取 nextn,也就是整个字符串相同前后缀的长度
  2. 刚开始 Next0 = -1, j = -1,便于观察当前是否已经退回到下标 0 了
  3. 可以求所有匹配位置,在 while 循环里判断 j 是否到n,然后让 j = nextj,继续匹配
java 复制代码
class Solution {
    public int strStr(String haystack, String needle) {
        int n = needle.length(), m = haystack.length();
        int[] next = new int[n+1];

        next[0] = -1;
        int i = 0, j = -1;
        while(i < n){
            if(j == -1 || needle.charAt(j) == needle.charAt(i)){
                ++j;
                ++i;
                next[i] = j;
            }else{
                j = next[j];
            }
        }

        i = 0;
        j = 0;
        while(i < m && j < n){
            if(j == -1 || haystack.charAt(i) == needle.charAt(j)){
                ++i;
                ++j;
            }else{
                j = next[j];
            }
        }
        if(j == n){
            return i - n;
        }
        return -1;
    }
}

暴力

java 复制代码
public class Solution {
    public int strStr(String haystack, String needle) {
        for(int i = 0; i < haystack.length(); ++i){
            String compare = haystack.substring(i, Math.min(i+needle.length(), haystack.length()));
            if(compare.equals(needle)){
                return i;
            }
        }
        return -1;
    }
}

459.重复的子字符串

求循环节

注意这里需要剔除特殊情况,防止后面的取模一直为0,

  • 避免 nextn = 0,否则变成 n % n == 0
  • 避免 n = 1,否则 n % (n - 0) = 1 % 1 == 0
java 复制代码
class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int n = s.length();
        int[] next = new int[n+1];

        next[0] = -1;
        int i = 0, j = -1;
        while(i < n){
            if(j == -1 || s.charAt(j) == s.charAt(i)){
                ++j;
                ++i;
                next[i] = j;
            }else{
                j = next[j];
            }
        }

        if(next[n] != 0 && n != 1 && n % (n - next[n]) == 0){
            return true;
        }
        return false;
    }
}

93.复原IP地址

细节注意:

  • IP 地址不能超过 3 位
  • 不能前导 0 开头
  • 不能大于 255
  • 只有 4 节:终止条件
java 复制代码
class Solution {
    List<String> res = new ArrayList<>();
    List<String> ans = new ArrayList<>();
    public List<String> restoreIpAddresses(String s) {
        dfs(s, 0, 0);
        return res;
    }

    public void dfs(String s, int beg, int num){
        if(num == 4 && beg == s.length()){
            String ip = "";
            for(int i = 0; i < ans.size(); ++ i){
                ip += ans.get(i) + ((i < ans.size() - 1) ? "." : "");
            }
            res.add(ip);
            return;
        }
        if(num > 4){
            return;
        }

        for(int i = beg; i < beg + 3 && i < s.length(); ++ i){
            String str = s.substring(beg, i+1);
            if(str.length() > 1 && str.charAt(0) == '0') continue;
            int strnum = Integer.parseInt(str);
            if(strnum < 0 || strnum > 255) continue;
            ans.add(str);
            dfs(s, i + 1, num+1);
            ans.remove(ans.size() - 1);
        }
    }

}

可以用 StringBuilder 来累加节约空间

更方便的写法:

java 复制代码
String.join(".", ans);
相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark1 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her11 天前
并查集-听课笔记
笔记·算法·并查集
码流子1 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven1 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark1 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218611 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法