【问题描述】
给定一个字符串,逐个翻转字符串中的每个单词。示例 1:
输入: "the sky is blue"
输出: "blue is sky the"
【解答思路】
1. 双指针
- 倒序遍历字符串
- 确定边界添加单词
- 拼接字符串 返回
时间复杂度:O(N) 空间复杂度:O(N)
class Solution {public String reverseWords(String s) {s = s.trim(); // 删除首尾空格int j = s.length() - 1, i = j;StringBuilder res = new StringBuilder();while(i >= 0) {while(i >= 0 && s.charAt(i) != ' ') i--; // 搜索首个空格res.append(s.substring(i + 1, j + 1) + " "); // 添加单词while(i >= 0 && s.charAt(i) == ' ') i--; // 跳过单词间空格j = i; // j 指向下个单词的尾字符}return res.toString().trim(); // 转化为字符串并返回}
}作者:jyd
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/solution/151-fan-zhuan-zi-fu-chuan-li-de-dan-ci-shuang-zh-2/
2. 分割 + 倒序
时间复杂度:O(N) 空间复杂度:O(N)
利用 “字符串分割”、“列表倒序” 的内置函数 (面试时不建议使用)
class Solution {public String reverseWords(String s) {String[] strs = s.trim().split(" "); // 删除首尾空格,分割字符串StringBuilder res = new StringBuilder();for(int i = strs.length - 1; i >= 0; i--) { // 倒序遍历单词列表if(strs[i].equals("")) continue; // 遇到空单词则跳过 "a good example" res.append(strs[i] + " "); // 将单词拼接至 StringBuilder}return res.toString().trim(); // 转化为字符串,删除尾部空格,并返回}
}作者:jyd
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/solution/151-fan-zhuan-zi-fu-chuan-li-de-dan-ci-shuang-zh-2/
【总结】
1.字符串双指针思想
2.字符串常见函数 反转分割替换截取
3.反转分割替换截取
反转 4种
Java中String没有reverse方法,StringBuilder有这个方法
String str = "12345";第一种,用了StringBuilder()中字符append连接,
字符串中函数toCharArray();将字符串变成字符数组,然后反向遍历@Test
public void Test() {StringBuilder stringBuilder = new StringBuilder();char[] chars = str.toCharArray();System.out.println("字符串的长度是" + chars.length);int m = 1;for (int i = chars.length; i >= 1; i--) {stringBuilder = stringBuilder.append(chars[i - 1]);System.out.println("the" + m + "char is--" + chars[i - 1]);m++;}System.out.println(stringBuilder.toString());
}第二种,直接用了StringBuilder()中reverse方法并且链接在首位
@Test
public void Test2() {StringBuilder stringBuilder = new StringBuilder(str);System.out.println(stringBuilder.reverse().toString());
}
第三种,字符串中函数toCharArray();方法,反向将字符串连接@Test
public void Test3() {char[] chars = str.toCharArray();String str_reverse="";for (int i = chars.length - 1; i >= 0; i--) {str_reverse += chars[i];}System.out.println(str_reverse);
}第四种,字符串中的函数.charAt(i)方法,获取指定位置的字符,后面的字符依次遍历
@Test
public void Test4() {String str_reverse = "";int length = str.length();for (int i = 0; i < length; i++) {str_reverse = str.charAt(i) + str_reverse;}System.out.println(str_reverse);
}
分割
字符串分割,可以根据指定字符,指定字符串,指定正则表达式进行分割,分几段也可以限制
public String[] split(String regex, int limit)
- regex – 正则表达式分隔符。
- limit – 分割的份数。
public class TestString2 {String str = "bb1a2a3a4a5a";@Testpublic void Test() {String[] str2 = str.split("a");for (int i = 0; i < str2.length; i++) {System.out.println(str2[i]);}}分三段@Testpublic void Test2() {String[] str2 = str.split("a",3);for (int i = 0; i < str2.length; i++) {System.out.println(str2[i]);}}指定正则表达式@Testpublic void Test3() {String[] str2 = str.split("\\d");for (int i = 0; i < str2.length; i++) {System.out.println(str2[i]);}}@Testpublic void Test4() {String[] str2 = str.split("\\d",3);for (int i = 0; i < str2.length; i++) {System.out.println(str2[i]);}}
}
替换
- public String replace(char oldChar, char newChar)
- public String replaceAll(String regex, String replacement)
public class TestString3 {String str = "a1a2a3a4a5a";@Testpublic void Test() {String str1= str.replace("a","b");System.out.println(str1);String str2= str.replace("a4","bE");System.out.println(str2);String str3= str.replaceAll("\\d","E");System.out.println(str3);}}
截取
- public String substring(int beginIndex)
- public String substring(int beginIndex, int endIndex)
public class TestString4 {@Testpublic void TestSubString() {String str= "123456789";System.out.println(str.substring(0, 7));System.out.println(str.substring(1, 7));System.out.println(str.substring(4));System.out.println(str.substring(0));}
}
参考链接:
1.https://www.cnblogs.com/qianjinyan/p/10218906.html
2.https://leetcode-cn.com/problems/reverse-words-in-a-string/solution/151ti-fan-zhuan-zi-fu-chuan-li-de-dan-ci-by-iceblo/