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 res="";
        //char[] tmp = new char[s.length()];
        int i = 0;
        int start = 0;
        int end = 0;
        while(i < s.length()){
            while( i < s.length() && s.charAt(i) == ' '){
                i ++;
            }
            start = i;
            while(i < s.length() && s.charAt(i) != ' '){
                i ++;
            }
            end = i - 1;
            if(end >= start){//start~end就是一个word
                res = s.substring(start,end+1) + " " + res;
            }
            i++;
        }
        return res.trim();
    }
}

Runtime: 516 ms


public class Solution {
    public String reverseWords(String s) {
        String res="";
        String[] tmp = s.split(" ");
        for(int i = tmp.length - 1 ; i > -1 ; i --){
            if(!tmp[i].equals("")){//注意不能使用==
                res = res + " " + tmp[i] ;
            }
        }
        return res.trim();
    }
}
Runtime:  716 ms

stringbuffer

public class Solution {
    public String reverseWords(String s) {
        int start=0;//单词的开头
        int end=s.length()-1;//最后一个单词的结尾
        while(start<=s.length()-1&&s.charAt(start)==' '){//跳过开头的空格
            start++;
        }
        while(end>=0&&s.charAt(end)==' '){//跳过结尾的空格
            end--;
        }
        if(start>end) return "";
        StringBuffer result= new StringBuffer();
        while(start<=end){
            StringBuffer word=new StringBuffer();
        	while(start <= end && s.charAt(start)!=' '){
        		word.append(s.charAt(start));
        		start++;
        	}
        	if(!word.equals("")) {
        	    if(result.length() == 1){
        	        result.insert(0,word);
        	    }else{
        	        result.insert(0,word+" ");
        	    }
        	}
        	while(start <= end && s.charAt(start)==' ') start++;
        }
        
        return result.toString().trim();
    }
}

Runtime:  444 ms

对于经常性修改字符串来说,可以考虑StringBuilder/StringBuffer

java.lang.StringBuilder

增加

append(char[])

append(char[],offset,len)

append(v)  //将基本类型追加到stringbuffer之后

insert(offset,char[])

    insert(offset,string)

delete(startIndex,endIndex)

deletecharAt(index)

setCharAt(index,c)

replace(startIndex,endIndex,string)//用string替换startIndex~endIndex-1

charAt(index)

substring(startIndex)//注意是小写的s

substring(start,end)//返回start到end-1

其他

length()

setLength()

String

比较

equals(string) //比较相等的时候

equalsIgnoreCase(string)

compareTo(string)//返回大于0或小于0或等于0

compareToIgnoreCase(string)

startsWith(string)

endsWith(string)

字符串长度,字符以及组合

length()

charAt(index)

concat(String)//连接

子串

substring(beginIndex) 获取beginIndex开始的子串

substring(beginIndex,endIndex) //返回begin~end-1之间的子串

替换,转换,分割

toLowerCase()

toUpperCase()

trim()

replace(oldchar,newchar)//用newchar替换所有的oldchar

replaceFirst(oldstring,newstring)//新字符串替换这个字符串中第一个匹配的子串

replaceAll(oldstring,newstring)//替换所有的

split()

matches(regix)

注意:以上的替换函数中的老字符串都可以使用正则表达式来表示

如:“a+b$#c”.replaceAll("[$+#]","NNN");

找出某个字符或者子串

indexOf(char)

indexOf(char,fromIndex)

indexOf(string)

indexOf(string,fromIndex)

lastIndexOf(char)

类似indexOf的重载函数

字符数值替换成字符串(静态方法)

valueOf(char)

valueOf(char[])

valueOf(double)

valueOf(float)

valueOf(int)

valueOf(long)

valueOf(boolean)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值