LeetCode【557. 反转字符串中的单词 III】

这道题可以使用字符串的split函数,把几个单词切开存入数组中,然后再对数组中每个单词反转,反转后的字符串存入新的字符串中,然后,把每个反转单词存入这个新的字符串中。

class Solution {
    public String reverseWords(String s) {
        String s1 = "";
        int i,j;
        int c = s.length();
        int t;
        String[] str = s.split(" ");
        int co = str.length;
        for(i = 0;i <= co-1;i++)
        {
            int c1 = str[i].length();
            for(j = c1-1;j >= 0;j--)
            {
                t = str[i].charAt(j);
                s1 = s1 + (char)t;
            }
            if(i != co-1)
            {
                s1 = s1 + " ";
            }
        }
        return s1;
    }
}

唯一缺点就是,字符串多了,就超过时间了。

原文地址:https://www.cnblogs.com/wzwi/p/11022864.html