LeetCode 151. Reverse Words in a String (Java版; Medium)

客官°小女子只卖身不卖艺 2023-07-04 06:20 126阅读 0赞

welcome to my blog

LeetCode 151. Reverse Words in a String (Java版; Medium)

题目描述

  1. Given an input string, reverse the string word by word.
  2. Example 1:
  3. Input: "the sky is blue"
  4. Output: "blue is sky the"
  5. Example 2:
  6. Input: " hello world! "
  7. Output: "world! hello"
  8. Explanation: Your reversed string should not contain leading or trailing spaces.
  9. Example 3:
  10. Input: "a good example"
  11. Output: "example good a"
  12. Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
  13. Note:
  14. A word is defined as a sequence of non-space characters.
  15. Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
  16. You need to reduce multiple spaces between two words to a single space in the reversed string.
  17. Follow up:
  18. For C programmers, try to solve it in-place in O(1) extra space.

第一次做; 从后往前, 注意单独处理开头

  1. class Solution {
  2. public String reverseWords(String s) {
  3. StringBuilder sb = new StringBuilder();
  4. s = s.trim();
  5. int n=s.length(), i=n-1, j=n;
  6. while(i>=0){
  7. if(s.charAt(i)==' '){
  8. sb.append(s.substring(i+1,j)).append(" ");
  9. while(s.charAt(i)==' ')
  10. i--;
  11. j=i+1;
  12. }
  13. else
  14. i--;
  15. }
  16. //单独处理开头
  17. sb.append(s.substring(0,j));
  18. return sb.toString();
  19. }
  20. }

第一次做; 逐个字符逐个字符扫描, 碰到非空字符就取出以该字符开始的单词, 配合; 从前往后, 还可以从后往前, 这样就不用tmp这个StringBuilder了

  1. class Solution {
  2. public String reverseWords(String s) {
  3. StringBuilder sb = new StringBuilder();
  4. int l = 0;
  5. while(l<s.length()){
  6. char ch = s.charAt(l);
  7. if(ch==' '){
  8. l++;
  9. continue;
  10. }
  11. StringBuilder tmp = new StringBuilder();
  12. int r = l+1;
  13. tmp.append(ch);
  14. while(r<s.length() && s.charAt(r)!=' '){
  15. tmp.append(s.charAt(r));
  16. r++;
  17. }
  18. tmp.append(" ");
  19. sb.insert(0, tmp.toString());
  20. l = r+1;
  21. }
  22. return sb.toString().trim();
  23. }
  24. }

LeetCode优秀题解

https://leetcode.com/problems/reverse-words-in-a-string/discuss/47720/Clean-Java-two-pointers-solution-(no-trim(-)-no-split(-)-no-StringBuilder)

  1. public class Solution {
  2. public String reverseWords(String s) {
  3. if (s == null) return null;
  4. char[] a = s.toCharArray();
  5. int n = a.length;
  6. // step 1. reverse the whole string
  7. reverse(a, 0, n - 1);
  8. // step 2. reverse each word
  9. reverseWords(a, n);
  10. // step 3. clean up spaces
  11. return cleanSpaces(a, n);
  12. }
  13. void reverseWords(char[] a, int n) {
  14. int i = 0, j = 0;
  15. while (i < n) {
  16. while (i < j || i < n && a[i] == ' ') i++; // skip spaces
  17. while (j < i || j < n && a[j] != ' ') j++; // skip non spaces
  18. reverse(a, i, j - 1); // reverse the word
  19. }
  20. }
  21. // trim leading, trailing and multiple spaces
  22. String cleanSpaces(char[] a, int n) {
  23. int i = 0, j = 0;
  24. while (j < n) {
  25. while (j < n && a[j] == ' ') j++; // skip spaces
  26. while (j < n && a[j] != ' ') a[i++] = a[j++]; // keep non spaces
  27. while (j < n && a[j] == ' ') j++; // skip spaces
  28. if (j < n) a[i++] = ' '; // keep only one space
  29. }
  30. return new String(a).substring(0, i);
  31. }
  32. // reverse a[] from a[i] to a[j]
  33. private void reverse(char[] a, int i, int j) {
  34. while (i < j) {
  35. char t = a[i];
  36. a[i++] = a[j];
  37. a[j--] = t;
  38. }
  39. }
  40. }

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读