[C++] [Java] Clean Code


  • 13

    C++

    class Solution {
    public:
        string reverseWords(string s) {
            for (int i = 0; i < s.length(); i++) {
                if (s[i] != ' ') {   // when i is a non-space
                    int j = i;
                    for (; j < s.length() && s[j] != ' '; j++) { } // move j to the next space
                    reverse(s.begin() + i, s.begin() + j);
                    i = j - 1;
                }
            }
            
            return s;
        }
    };
    

    Java

    public class Solution {
        public String reverseWords(String s) {
            char[] ca = s.toCharArray();
            for (int i = 0; i < ca.length; i++) {
                if (ca[i] != ' ') {   // when i is a non-space
                    int j = i;
                    while (j + 1 < ca.length && ca[j + 1] != ' ') { j++; } // move j to the end of the word
                    reverse(ca, i, j);
                    i = j;
                }
            }
            return new String(ca);
        }
    
        private void reverse(char[] ca, int i, int j) {
            for (; i < j; i++, j--) {
                char tmp = ca[i];
                ca[i] = ca[j];
                ca[j] = tmp;
            }
        }
    }
    

  • 0
    Z

    @alexander thanks for you solution. maybe we don't have to write " i=j-1; ", " i=j; " also works


  • 0

    @zhangjiamingitisme-outlook.com hah, good point, the space can be skipped.


  • 0
    W

    space characters?


  • 0

    @wangying_love_c- i = j -1 will move i to the end of the reversed word, therefore i++ of the for statement will advance it to the next space. This is actually not necessary, because the space can be skipped.


  • 2
    1. split string by space
    2. reverse each string
    3. append them
        public String reverseWords(String s) {
          
          String[] strs = s.split(" ");
          StringBuilder sb = new StringBuilder();
          String space = "";
          for(String str: strs){
            sb.append(space);
            sb.append(reverse(str));  
            space = " ";
          }
          return sb.toString();
        }
        
        String reverse(String str){
          char[] sc = str.toCharArray();
          int s = 0, e = sc.length - 1;
          while(s < e){
            char t = sc[s];
            sc[s] = sc[e];
            sc[e] = t;
            s++;
            e--;
          }
          return new String(sc);
        }

  • 0
    J

    @alexander Yes. I also think it should be i=j;


  • 0
    M

    Using swap

    keep i be the start of each word, use j to find the white space, k=j-1 and we can use swap to reverse each word.

    public:
        string reverseWords(string s) {
            int i = 0, j = 0;
            while(j<s.length()) {
                while(j<s.length() && s[j]!=' ') j++;
                int k = j-1;
                while(i<k) swap(s[i++], s[k--]);
                i = ++j;
            }
            return s;
        }
    

  • 6

    maybe shorter

        public String reverseWords(String s) {
            String[] str = s.split(" ");
            for (int i = 0; i < str.length; i++) str[i] = new StringBuilder(str[i]).reverse().toString();
            StringBuilder result = new StringBuilder();
            for (String st : str) result.append(st + " ");
            return result.toString().trim();
        } 
    

  • 0
    B

    What's wrong with this? Time limit exceeded.

    #include<string.h>
    char* reverseWords(char* s) {
        long i=0, j=strlen(s)-1;
        for(i=0;i<strlen(s);i++){
            long j=i;
            while(j<strlen(s) && (s[j]!=' ' && s[j]!='\0')) j++;
            long p=i,q=j-1;
            while(p<q){
                char temp = s[p];
                s[p]=s[q];
                s[q]=temp;
                p++;q--;
            }
            i=j;
        }
        return s;
    }
    

  • 0
    A

    @b_s try to put the strlen(s) out of the loops.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.