Sunday, March 9, 2014

Leetcode - Reverse Words in a String

Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Clarification:
  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

Solution:

public class Solution {
    public String reverseWords(String s) {
        if(s.isEmpty() || s.length() == 0)   return s;
        
        StringBuffer res = new StringBuffer();
        
        int t, h;
        for(int i = s.length() - 1; i >= 0; i--) {
            while(i >= 0 && s.charAt(i) == ' ') i--;
            
            // set tail pointer
            if(i < 0) break;
            t = i;
            h = t;
            
            // set head pointer
            while(i >= 0 && s.charAt(i) != ' ') { h = i; i--; }
            
            // append this word (append a space if find more than two words)
            if(h <= t && res.length() > 0) res.append(' ');
            for(int j = h; j <= t; j++) {
                res.append(s.charAt(j));
            }
        }
        
        return res.toString();
    }
}


-------------------------------------
OK, finally i'm back! second round, go~

No comments:

Post a Comment