Quarter's blog Quarter's blog
首页
  • Java

    • 基础概念于知识
  • 数据库

    • 高性能MySQL读后感
    • Oracle索引入门
  • JVM

    • 深入学习JVM虚拟机
  • Vue
  • leetcode打卡
  • 虚拟机集群搭建
  • 工作中的Oracle
关于
收藏
  • 分类
  • 标签
  • 归档

Quarter

无限进步
首页
  • Java

    • 基础概念于知识
  • 数据库

    • 高性能MySQL读后感
    • Oracle索引入门
  • JVM

    • 深入学习JVM虚拟机
  • Vue
  • leetcode打卡
  • 虚拟机集群搭建
  • 工作中的Oracle
关于
收藏
  • 分类
  • 标签
  • 归档
  • CICD

  • 算法

    • 训练营Day1-704. 二分查找&27. 移除元素
    • 训练营Day2
    • 训练营Day3
    • 训练营Day4
    • 训练营Day6
    • 训练营Day7
    • 训练营Day8
      • 344.反转字符串
      • 541.反转字符串Ⅱ
      • 剑指Offer05.替换空格
      • 151.反转字符串中的单词
      • 剑指Offer58-Ⅱ.左旋转字符串
    • 训练营Day10
    • 训练营Day11
    • 训练营Day13
  • 技术
  • 算法
Quarter
2023-06-19
目录

训练营Day8

啊,今天怎么有5道题

# 344.反转字符串 (opens new window)

简单题,双指针

class Solution {
    public void reverseString(char[] s) {
        int left = 0,right =s.length-1;
        char temp;
        while(left<right){
            temp = s[right];
            s[right]=s[left];
            s[left] =temp;
            left++;
            right--;
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 541.反转字符串Ⅱ (opens new window)

也是简单题,之前写着的太繁琐了

class Solution {
    public String reverseStr(String s, int k) {
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i+=2*k) {
            int start = i;
            // 盘面段尾数够不够K个来决定end指针的位置
            int end = Math.min(ch.length-1,start+k-1);
            
            while (start < end){
                char temp = ch[start];
                ch[start] = ch[end];
                ch[end] = temp;
                
                start++;
                end--;
            }
        }
        return new String(ch);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 剑指Offer05.替换空格 (opens new window)

也是双指针,补全空格所需要的空间,从后往前填充

//请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 
//
// 
//
// 示例 1: 
//
// 输入:s = "We are happy."
//输出:"We%20are%20happy." 
//
// 
//
// 限制: 
//
// 0 <= s 的长度 <= 10000 
//
// Related Topics 字符串 👍 500 👎 0


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String replaceSpace(String s) {
        //双指针算法
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' '){
                sb.append("  ");
            }
        }

        //如果是空,就返回
        if (sb.toString().length()==0){
            return s;
        }

        int left = s.length()-1;
        s += sb.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);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

# 151.反转字符串中的单词 (opens new window)

//给你一个字符串 s ,请你反转字符串中 单词 的顺序。 
//
// 单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。 
//
// 返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。 
//
// 注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。 
//
// 
//
// 示例 1: 
//
// 
//输入:s = "the sky is blue"
//输出:"blue is sky the"
// 
//
// 示例 2: 
//
// 
//输入:s = "  hello world  "
//输出:"world hello"
//解释:反转后的字符串中不能存在前导空格和尾随空格。
// 
//
// 示例 3: 
//
// 
//输入:s = "a good   example"
//输出:"example good a"
//解释:如果两个单词间有多余的空格,反转后的字符串需要将单词间的空格减少到仅有一个。
// 
//
// 
//
// 提示: 
//
// 
// 1 <= s.length <= 10⁴ 
// s 包含英文大小写字母、数字和空格 ' ' 
// s 中 至少存在一个 单词 
// 
//
// 
// 
//
// 
//
// 进阶:如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 O(1) 额外空间复杂度的 原地 解法。 
//
// Related Topics 双指针 字符串 👍 892 👎 0


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String reverseWords(String s) {
        if (s.length()==1){
            return s;
        }

        //移除空格
        char[] chars = removeSpace(s);

        //反转整个字符串
        reverse(chars,0, chars.length-1);

        //反转单词

        reverseEachWord(chars);

        return new String(chars);

    }

    public void reverseEachWord(char[] chars){
        //这里使用快慢指针
        int start = 0;
        for (int end = 0; end <= chars.length; end++) {
            if (end == chars.length || chars[end] == ' ') {
                reverse(chars, start, end - 1);
                start = end + 1;
            }
        }


    }

    public char[] removeSpace(String s){
        //转为数组
        char[] chars = s.toCharArray();

        // 快慢双指针 移除空格
        int slow = 0;
        for (int fast = 0; fast < chars.length; fast++) {
            if (chars[fast]!=' '){


                //除了第一个单词,其他单词后面需要加空格
                if (slow>0){
                    chars[slow++] = ' ';
                }

                //移除空格
                while (fast<chars.length && chars[fast]!=' '){
                    chars[slow++] = chars[fast++];
                }
            }
        }
        // 截取数组,返回新的
        return Arrays.copyOfRange(chars, 0, slow);
    }

    // 定义翻转函数
    public void reverse(char[] ch, int i, int j) {
        for (; i < j; i++, j--) {
            char temp  = ch[i];
            ch[i] = ch[j];
            ch[j] = temp;
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

# 剑指Offer58-Ⅱ.左旋转字符串 (opens new window)

这个原地修改的写法,确实没见过

//字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数
//将返回左旋转两位得到的结果"cdefgab"。 
//
// 
//
// 示例 1: 
//
// 输入: s = "abcdefg", k = 2
//输出: "cdefgab"
// 
//
// 示例 2: 
//
// 输入: s = "lrloseumgh", k = 6
//输出: "umghlrlose"
// 
//
// 
//
// 限制: 
//
// 
// 1 <= k < s.length <= 10000 
// 
//
// Related Topics 数学 双指针 字符串 👍 409 👎 0


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String reverseLeftWords(String s, int n) {
        char[] chars = s.toCharArray();
        reverse(chars,0,chars.length-1);
        reverse(chars,0,chars.length-1-n);
        reverse(chars,chars.length-n,chars.length-1);
        return new String(chars);
    }

    public void reverse(char[] chars, int left, int right) {
        while (left < right) {
            chars[left] ^= chars[right];
            chars[right] ^= chars[left];
            chars[left] ^= chars[right];
            left++;
            right--;
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
上次更新: 2023/06/19, 18:58:48
训练营Day7
训练营Day10

← 训练营Day7 训练营Day10→

最近更新
01
训练营Day13
06-23
02
训练营Day11
06-22
03
训练营Day10
06-22
更多文章>
Theme by Vdoing | Copyright © 2023-2023 Quarter | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式