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".

 

public class Solution {
    public String reverseWords(String s) {
        String re = "";
        List<String> words = new ArrayList<String>();
        String word = "";
        int flag = 0;
        for(int i=0;i<s.length();i++) {
            if(s.charAt(i)!=' ') {
                word = word+s.charAt(i);
                flag = 1;
            }
            if(flag==1&&(s.charAt(i)==' '||i==s.length()-1)) {
                words.add(word);
                word = "";
                flag = 0;
            }
        }
        
        for(int j=0;j<words.size();j++) {
            re = words.get(j)+" "+re;
        }
        
        return re.trim();
        
       
    }
}

 

posted @ 2015-02-01 15:15  mrpod2g  阅读(88)  评论(0编辑  收藏  举报