From 418bbabea87977a7896fc45de8f5700162121f72 Mon Sep 17 00:00:00 2001 From: whai Date: Fri, 5 Jan 2024 14:26:02 +0800 Subject: [PATCH] =?UTF-8?q?##=20=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 常见解决方法 - **逆转** - **双指针** - 首尾双指针:删除空格,要的才给StringBuilder - 双头双指针:移动元素 - | 类型 | 题目 | 解决 | 做题时间 | | -------------------- | ------------------------------------------------------------ | ------------------------------ | -------------- | | 多次逆转 | [右旋字符串](https://www.programmercarl.com/kama55.右旋字符串.html#思路) | 三次逆转 | 2024-1-5 x | | 删除空格、逆转字符串 | [151. 反转字符串中的单词 - 力扣(LeetCode)](https://leetcode.cn/problems/reverse-words-in-a-string/) | 双指针删除空格,
字符逆战 | **2024-1-5 x** | | | | | | --- .../cn/whaifree/leetCode/String/Kama55.java | 57 +++++++++++++ .../whaifree/leetCode/String/LeetCode151.java | 79 +++++++++++++------ .../whaifree/leetCode/String/LeetCode541.java | 5 ++ 3 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/String/Kama55.java diff --git a/src/main/java/cn/whaifree/leetCode/String/Kama55.java b/src/main/java/cn/whaifree/leetCode/String/Kama55.java new file mode 100644 index 0000000..3c7f93f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/String/Kama55.java @@ -0,0 +1,57 @@ +package cn.whaifree.leetCode.String; + +import org.junit.Test; + +/** + * 字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面。给定一个字符串 s 和一个正整数 k,请编写一个函数,将字符串中的后面 k 个字符移到字符串的前面,实现字符串的右旋转操作。 + * + * 例如,对于输入字符串 "abcdefg" 和整数 2,函数应该将其转换为 "fgabcde"。 + * + * 输入:输入共包含两行,第一行为一个正整数 k,代表右旋转的位数。第二行为字符串 s,代表需要旋转的字符串。 + * + * 输出:输出共一行,为进行了右旋转操作后的字符串。 + */ +public class Kama55 { + + @Test + public void test() { + String s = "abcdefg"; + int k = 2; + String result = new Solution().reverse(s, k); + System.out.println(result); + } + + public static void main(String[] args) { + + } + + class Solution{ + + /** + * 逆转 + * abcde 2 + * cbaed 两个区间分别逆转 + * deabc 整体逆转 + * @param s + * @param target + * @return + */ + public String reverse(String s, int target) { + StringBuilder stringBuilder = new StringBuilder(s); + reverseString(stringBuilder, 0, target - 1); + reverseString(stringBuilder, target, stringBuilder.length() - 1); + reverseString(stringBuilder, 0, stringBuilder.length() - 1); + return stringBuilder.toString(); + } + + public void reverseString(StringBuilder stringBuilder,int start, int end) { + while (start < end) { + char tmp = stringBuilder.charAt(start); + stringBuilder.setCharAt(start, stringBuilder.charAt(end)); + stringBuilder.setCharAt(end, tmp); + start++; + end--; + } + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java index c666da5..1462b21 100644 --- a/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode151.java @@ -10,15 +10,14 @@ import java.util.List; public class LeetCode151 { + + // todo @Test public void test() { - String s = " the sky is blue"; + String s = " a b cd ef g "; + System.out.println(new Solution1().reverseWords(s)); - char[] chars = new Solution1().deleteSpace(s.toCharArray()); - for (char aChar : chars) { - System.out.println("=" + aChar + "="); - } } class Solution { @@ -44,36 +43,68 @@ public class LeetCode151 { // 不要使用辅助空间,空间复杂度要求为O(1) class Solution1 { public String reverseWords(String s) { - char[] chars = s.toCharArray(); + StringBuilder stringBuilder = deleteSpace(s, ' '); + reverse(stringBuilder, 0, stringBuilder.length() - 1); + reverseWord(stringBuilder); // 移除多余空格 // 反转char // 反转每个字符 - return null; + return stringBuilder.toString(); } - public char[] deleteSpace(char[] chars) { - - int superFlowSpace = 0; - + public void reverseWord(StringBuilder stringBuilder) { + int start = 0; int index = 0; - System.out.println(chars[index] == ' '); - while (chars[index] == ' ') { - superFlowSpace++; + while (index < stringBuilder.length() + 1) { + if (index == stringBuilder.length() || stringBuilder.charAt(index) == ' ') { + reverse(stringBuilder, start, index - 1); + start = index + 1; + } index++; } - index = 0; - while (index < chars.length - 1 - superFlowSpace) { - chars[index] = chars[index + superFlowSpace]; - if (' ' == chars[index + superFlowSpace] && ' ' == chars[index + superFlowSpace + 1]) { - superFlowSpace++; - } else { - index++; - } - + } + public void reverse(StringBuilder str, int start, int end) { + while (start < end) { + char tmp = str.charAt(start); + str.setCharAt(start, str.charAt(end)); + str.setCharAt(end, tmp); + end--; + start++; } + } - return Arrays.copyOfRange(chars, 0, chars.length - superFlowSpace); + /** + * 双指针移动元素,移除target + * - 参考LeetCode 27 + + * @param target + * @return + */ + public StringBuilder deleteSpace(String s,char target) { + + // 前后指针 + int start = 0; + int end = s.length() - 1; + while (s.charAt(start) == target) start++; + while (s.charAt(end) == target) end--; + // 1. 删除首尾空格 + + StringBuilder stringBuilder = new StringBuilder(); + while (start <= end) { + if (s.charAt(start) !=target){ + stringBuilder.append(s.charAt(start) ); + }else if (s.charAt(start) == target && stringBuilder.charAt(stringBuilder.length() - 1) != target) { + stringBuilder.append(s.charAt(start) ); + } + start++; + } + // 2. 如果遇到空格,判断上一个是不是空格 + // - 不是,入String + // - 是,跳过 + + + return stringBuilder; } } diff --git a/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java b/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java index e122ac5..1bdb79b 100644 --- a/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java +++ b/src/main/java/cn/whaifree/leetCode/String/LeetCode541.java @@ -2,6 +2,11 @@ package cn.whaifree.leetCode.String; import org.junit.Test; +/** + * 反转字符串 II + * 给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。 + * 如果剩余字符少于 k 个,则将剩余字符全部反转。 + */ public class LeetCode541 { @Test