概念
打基础的时候,不要太迷恋于库函数。
哈哈,卡哥的这一句太经典了!!今天的题目没别的,就是用来锻炼编码能力的!
作业题
344. 反转字符串
package jjn.carl.string; import java.util.Arrays; /** * @author Jjn * @since 2023/7/5 11:39 */ public class LeetCode344 { public void reverseString(char[] s) { int left = 0, right = s.length - 1; while (left < right) { char temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } } public static void main(String[] args) { String s = "aabbcc"; char[] input = s.toCharArray(); new LeetCode344().reverseString(input); System.out.println("input = " + Arrays.toString(input)); } }
541. 反转字符串 II
package jjn.carl.string; /** * @author Jjn * @since 2023/7/5 16:41 */ public class LeetCode541 { // 自己写的比较笨的方法,运行需要4ms,只能超过百分之十几的 public String reverseStr(String s, int k) { StringBuilder stringBuilder = new StringBuilder(); int left = 0, right = 0; while (left < s.length() && right < s.length()) { right = left + k - 1; for (int i = Math.min(right, s.length() - 1); i >= left; i--) { stringBuilder.append(s.charAt(i)); } left = right + 1; right = left + k - 1; for (int i = left; i <= Math.min(right, s.length() - 1); i++) { stringBuilder.append(s.charAt(i)); } left = right + 1; } return stringBuilder.toString(); } public static void main(String[] args) { // 如果剩余字符少于 k 个,则将剩余字符全部反转。 // 如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。 // bacdfeg System.out.println("new LeetCode541().reverseStr(\"abcdefg\", 2) = " + new LeetCode541().reverseStr("abcdefg", 2)); // bacd System.out.println("new LeetCode541().reverseStr(\"abcd\", 2) = " + new LeetCode541().reverseStr("abcd", 2)); } }
package jjn.carl.string; /** * @author Jjn * @since 2023/7/5 16:41 */ public class LeetCode541_2 { public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); for (int i = 0; i < s.length(); i = i + 2 * k) { int end = Math.min(i + k - 1, s.length() - 1); reverse(chars, i, end); } return new String(chars); } private void reverse(char[] chars, int start, int end) { while (start < end) { char temp = chars[start]; chars[start] = chars[end]; chars[end] = temp; start++; end--; } } public static void main(String[] args) { // 如果剩余字符少于 k 个,则将剩余字符全部反转。 // 如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。 // bacdfeg System.out.println("new LeetCode541().reverseStr(\"abcdefg\", 2) = " + new LeetCode541_2().reverseStr("abcdefg", 2)); // bacd System.out.println("new LeetCode541().reverseStr(\"abcd\", 2) = " + new LeetCode541_2().reverseStr("abcd", 2)); } }
剑指 Offer 05. 替换空格
package jjn.carl.string; /** * @author Jiang Jining * @since 2023-07-05 23:31 */ public class Offer5 { public String replaceSpace(String s) { StringBuilder sb = new StringBuilder(); for (char c : s.toCharArray()) { if (c == ' ') { sb.append("%20"); } else { sb.append(c); } } return sb.toString(); } public static void main(String[] args) { String replacedSpace = new Offer5().replaceSpace("We are happy."); System.out.println("replacedSpace = " + replacedSpace); } }
151. 反转字符串中的单词
package jjn.carl.string; /** * @author Jiang Jining * @since 2023-07-05 23:33 */ public class LeetCode151 { public String reverseWords(String s) { // 每个单词的开始和结束索引(左闭右开) int start, end; StringBuilder sb = new StringBuilder(); for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) == ' ') { //跳过空格 continue; } //找到结束索引 end = i + 1; while (i >= 0 && s.charAt(i) != ' ') { //跳过空格 i--; } //找到开始索引 start = i + 1; for (int j = start; j < end; j++) { //将每个单词按开始结束索引赋值到StringBuilder sb.append(s.charAt(j)); } //加上单词间的空格 sb.append(' '); } //删掉最后一个多余的空格 sb.deleteCharAt(sb.length() - 1); return sb.toString(); } public static void main(String[] args) { System.out.println("new LeetCode151().reverseWords(\"the sky is blue\") = " + new LeetCode151().reverseWords("the sky is blue")); System.out.println("new LeetCode151().reverseWords(\" hello world \") = " + new LeetCode151().reverseWords(" hello world ")); System.out.println("new LeetCode151().reverseWords(\"a good example\") = " + new LeetCode151().reverseWords("a good example")); } }