zoukankan      html  css  js  c++  java
  • [leedcode 151] 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".

    Update (2015-02-12):
    For C programmers: Try to solve it in-place in O(1) space.

    public class Solution {
        public String reverseWords(String s) {
            //利用java中StringBuilder的insert(0,String)函数,注意第一个字符是空格的情况
            //因此需要检查word的长度
            if(s==null||s.length()<=0) return s;
            StringBuilder word=new StringBuilder();
            StringBuilder res=new StringBuilder();
            for(int i=0;i<s.length();i++){
                if(s.charAt(i)!=' ') word.append(s.charAt(i));
                else if(s.charAt(i)==' '){
                    if(word.length()>0){
                        res.insert(0,word+" ");
                        word=new StringBuilder();
                    }else{
                        continue;
                    }
                }
            }
            if(word.length()>0) res.insert(0,word+" ");
            return res.toString().trim();
        }
    }
  • 相关阅读:
    Samba服务器配置
    Showdoc
    wkhtmltoimage(网页剪切功能)
    GTID数据库备份
    awstats日志分析
    docker桥接
    php的opcache缓存扩展(php页面代码刷新速度)
    Pureftp SSL/TLS配置
    ssh-keygen配置
    systemctl使用
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4694704.html
Copyright © 2011-2022 走看看