讨论/《画解剑指 Offer》 - 剑指 Offer 58 - I. 翻转单词顺序/
《画解剑指 Offer》 - 剑指 Offer 58 - I. 翻转单词顺序
共 14 个回复
class Solution {
public:
    string reverseWords(string s) {
        string ans;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == ' ') continue;
            string temp;
            while (i < s.size() and s[i] != ' ') {
                temp += s[i];
                ++i;
            }
            if (ans.empty()) {
                ans += temp;
            } else {
                ans = ' ' + ans;
                ans = temp + ans;
            }
        }
        return ans;
    }
};
2

java打卡

public String reverseWords(String s) {
    char[] array = new char[s.length()];

    int idx = 0, j = array.length - 1;
    char c;
    while (j >= 0) {
        c = s.charAt(j);
        if (c != ' ') {
            int end = j;
            if (idx > 0) {
                array[idx++] = ' ';
            }
            do {
                array[idx++] = c;
                j--;
            } while (j >= 0 && (c = s.charAt(j)) != ' ');
            // 反转[idx-(end-j),idx-1],其中end-j是长度
            int p = idx-end+j, q = idx-1;
            while(p < q) {
                char temp = array[p];
                array[p] = array[q];
                array[q] = temp;
                p++;
                q--;
            }
        }
        j--;
    }
    return new String(array, 0, idx);
}
1

C++用stringstream偷懒

class Solution {
public:
    string reverseWords(string s) {
        int i = 0 , j = s.length() - 1;
        stringstream ss(s);
        vector < string > v;
        string t;
        while(ss >> t)v.push_back(t);
        string ans = "";
        int cnt = 0;
        for(int i = v.size() - 1 ; i >= 0 ; i--) {
            if(cnt > 0) ans += " ";
            cnt++;
            ans += v[i];
        }
        return ans;
    }
};
1

做过一次又忘了

1

java

 public String reverseWords(String s) {
         String[] strings = s.split(" ");
        List<String> list = Arrays.asList(strings);
        StringBuilder sb = new StringBuilder();
        for (int i = list.size()-1; i >=0; i--) {
            if (!list.get(i).equals("")){
                sb.append(list.get(i));
                sb.append(" ");
            }
        }
        return sb.toString().trim();

    }

image.png

class Solution {
    public String reverseWords(String s) {
        String[] strs = s.trim().split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = strs.length-1; i >= 0 ; i--){
            String word = strs[i];
            if (word.equals("")) continue;
            sb.append(word);
            if (i>0){
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}

image.png

JavaScript

var reverseWords = function(s) {
    return s.trim().split(' ').filter(item => item != '').reverse().join(' ');
}

这题比原站难一些。先做了原站的题,复制过来屡屡出错。原站上的题可以为“”,可以没有单词,只是“ ”,可以全是符号。

js最爽:

var reverseWords = function(s) {
    return s.trim().split(/\s+/).reverse().join(' ');
};

1