leetcode 中 java 版本的代码 怎么提交

2025-04-10 16:03:40
推荐回答(1个)
回答1:

不能写main函数,你需要的是按照class Solution给的接口来实现它的一个成员函数

给一个参考答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include
using namespace std;

class Solution {
public:
void reverseWords(string &s) {
string ans = "", temp;
stringstream sin(s);
while(sin >> temp) {
if(ans != "") {
ans = temp + " " + ans;
} else {
ans = temp;
}
}
s = ans;
}
};