LeetCode刷题Day08——字符串(字符串交换)

简介: 一、反转字符串

一、反转字符串

题目链接:344. 反转字符串

/**
 * <pre>
 * 双指针指向头和尾,不断交换并向中间靠拢 
 * </pre>
 *
 * @author <a href="https://github.com/Ken-Chy129">Ken-Chy129</a>
 * @date 2023/1/9 11:06
 */
public class 反转字符串344 {
    public void reverseString(char[] s) {
        int len = s.length;
        char tmp;
        for (int i=0; i<len/2; i++) {
            tmp = s[len-i-1];
            s[len-i-1] = s[i];
            s[i] = tmp;
        }
    }
}

二、反转字符串II

题目链接:541. 反转字符串 II

/**
 * <pre>
 * 每隔k个反转k个,末尾不够k个时全部反转
 * </pre>
 *
 * @author <a href="https://github.com/Ken-Chy129">Ken-Chy129</a>
 * @date 2023/1/9 11:16
 */
public class 反转字符串II541 {
    public String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        for (int i=0; i<s.length(); i+=2*k) {
            reserve(chars, i, Math.min(i + k, s.length()));
        }
        return new String(chars);
    }
    public void reserve(char[] chars, int begin, int end) {
        int len = end - begin;
        char tmp;
        for (int i=0; i<len/2; i++) {
            tmp = chars[end-i-1];
            chars[end-i-1] = chars[begin+i];
            chars[begin+i] = tmp;
        }
    }
}

三、替换空格

题目链接:05. 替换空格

public class 替换空格5 {
    public String replaceSpace(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<s.length(); i++) {
            if (s.charAt(i) == ' ') {
                sb.append('%');
                sb.append('2');
                sb.append('0');
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }
}

四、翻转字符串里的单词

题目链接:151. 反转字符串中的单词

/**
 * <pre>
 * 1.用栈维护
 * 2.调用api
 * </pre>
 *
 * @author <a href="https://github.com/Ken-Chy129">Ken-Chy129</a>
 * @date 2023/1/9 13:57
 */
public class 反转字符串中的单词151 {
    // 用栈维护
    public String reverseWords(String s) {
        int i = 0, len = s.length();
        Stack<String> stack = new Stack<>();
        while (i<len) {
            while (i < len && s.charAt(i) == ' ') {
                i++;
            }
            if (i == len) {
                break;
            }
            StringBuilder sb = new StringBuilder();
            while (i < len && s.charAt(i) != ' ') {
                sb.append(s.charAt(i++));
            }
            stack.add(sb.toString());
        }
        StringBuilder res = new StringBuilder();
        while (stack.size() > 1) {
            res.append(stack.pop());
            res.append(' ');
        }
        res.append(stack.pop());
        return res.toString();
    }
    // 调用api
    public String reverseWords2(String s) {
        // 除去开头和末尾的空白字符
        s = s.trim();
        // 正则匹配连续的空白字符作为分隔符分割
        List<String> wordList = Arrays.asList(s.split("\\s+"));
        Collections.reverse(wordList);
        return String.join(" ", wordList);
    }
}
相关文章
|
5天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
5天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
12 0
|
5天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
5天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
27 5
【刷题】 leetcode 面试题 01.06 字符串压缩
|
5天前
|
存储 算法 测试技术
|
5天前
|
算法 C语言 C++
|
5天前
|
存储 算法 C语言
C语言刷题~Leetcode与牛客网简单题
C语言刷题~Leetcode与牛客网简单题
|
5天前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
15 0
|
5天前
|
Java
刷题之Leetcode19题(超级详细)
刷题之Leetcode19题(超级详细)
15 0