讨论/《画解剑指 Offer》 - 剑指 Offer 58 - I. 翻转单词顺序/
《画解剑指 Offer》 - 剑指 Offer 58 - I. 翻转单词顺序
共 16 个回复
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

js最爽:

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

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

go 一开始的想法

func reverseWords(s string) string {
    out := ""
	top := -1
	stack := []string{}
	for i := 0; i < len(s);  {
		wordT := ""
		for i < len(s) && s[i] != ' ' {
			wordT += string(s[i])
			i++
		}
		if wordT != "" {
			stack = append(stack, wordT)
			top++
		}

		for i < len(s) && s[i] == ' ' {
			i++
		}
	}
	for top > -1 {
		out += stack[top]
		if top != 0 {
			out += " "
		}
		top--
	}
	return out
}

麻烦的解法。。

class Solution {
    public String reverseWords(String s) {
    ArrayList<String> all = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    int i=0;
    while(i<s.length()){
        StringBuilder word = new StringBuilder();
        while(i<s.length()&&s.charAt(i)==' '){
            i++;
        }
        if(i>=s.length())
            break;
        while(i<s.length()&&s.charAt(i)!=' '){
            word.append(s.charAt(i));
            i++;
        }
            all.add(word.toString());
    }
   for(int j=all.size()-1;j>=0;j--){
       if(j!=0){
          sb.append(all.get(j));
          sb.append(" ");
        }else{
            sb.append(all.get(j));
        }
        
   }

    return sb.toString();
    }
}

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(' ');
}