字符串相关问题

1. 反转字符串

For example,
Given s = “the sky is blue”,
return “blue is sky the”.
注意空格的处理

public class Solution {
    public String reverseWords(String s) {
        if(s == null || s.length() == 0)
            return s;
        String[] stringArray = s.split(" ");
        int len = stringArray.length;
        StringBuilder sb = new StringBuilder();
        for(int i = len-1; i >= 0; i--) {
            if(!stringArray[i].isEmpty())
                sb.append(stringArray[i]).append(" ");
        }
        return sb.length() == 0 ? "" : sb.substring(0, sb.length()-1);
    }

}

你可能感兴趣的:(字符串)