zoukankan      html  css  js  c++  java
  • leetcode--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".

    click to show clarification.

     

    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();
        }
    }
    

      

  • 相关阅读:
    Docker入门
    服务配置中心
    zuul网关
    git2
    git1
    git
    shiro授权、注解式开发
    shiro认证-SSM
    Shiro入门
    Springmvc之文件上传
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3591481.html
Copyright © 2011-2022 走看看