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