首页 > 其他 > 详细

leetcode--Reverse Words in a String

时间:2014-03-11 05:45:21      阅读:378      评论:0      收藏:0      [点我收藏+]

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

click to show clarification.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Solution {
    public String reverseWords(String s) {
        StringBuffer reverse = new StringBuffer();
        int len = s.length();
        if(len > 0){
            int end = 0;
            int start = 0;
            int length = 0;
            while(start < len){
                if(s.charAt(start) == 32){
                    ++start;
                        ++end;
                }
                else {
                    if(end < len && s.charAt(end) != 32)
                        ++end;
                    else{
                        reverse.insert(0, " "+ s.substring(start, end));
                        start = end;
                        length += (end - start + 1);
                    }
                }          
            }  
            if(length > 0)
                reverse = reverse.deleteCharAt(0);
        }  
        return reverse.toString();
    }
}

  

leetcode--Reverse Words in a String,布布扣,bubuko.com

leetcode--Reverse Words in a String

原文:http://www.cnblogs.com/averillzheng/p/3591481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!