题目## 题目
解题思路
这是一个字符串处理问题。需要将句子中的单词顺序倒置,但保持标点符号位置不变。
关键点:
- 分割字符串获取单词
- 处理标点符号
- 倒序重组单词
- 保持原有格式
算法步骤:
- 分割字符串为单词
- 倒序重组单词
- 处理标点符号
- 输出结果
代码
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string solve(string s) {
vector<string> words;
string word;
// 分割单词
for (char c : s) {
if (c == ' ') {
if (!word.empty()) {
words.push_back(word);
word.clear();
}
} else {
word += c;
}
}
if (!word.empty()) {
words.push_back(word);
}
// 倒序重组
string result;
for (int i = words.size() - 1; i >= 0; i--) {
result += words[i];
if (i > 0) result += " ";
}
return result;
}
};
int main() {
string s;
while (getline(cin, s)) {
Solution solution;
cout << solution.solve(s) << endl;
}
return 0;
}
import java.util.*;
public class Main {
static class Solution {
public String solve(String s) {
// 分割单词
String[] words = s.split(" ");
// 倒序重组
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]);
if (i > 0) result.append(" ");
}
return result.toString();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String s = sc.nextLine();
Solution solution = new Solution();
System.out.println(solution.solve(s));
}
sc.close();
}
}
class Solution:
def solve(self, s):
# 分割单词并倒序
words = s.split()
words.reverse()
# 重组句子
return ' '.join(words)
# 持续读取输入直到EOF
while True:
try:
s = input()
solution = Solution()
print(solution.solve(s))
except EOFError:
break
算法及复杂度
- 算法:字符串处理
- 时间复杂度: O ( n ) \mathcal{O(n)} O(n),其中 n n n 是字符串长度
- 空间复杂度: O ( n ) \mathcal{O(n)} O(n),需要存储分割后的单词
解题思路
使用字符串处理方法实现整数倒序输出:
- 读入为字符串处理
- 分离负号处理
- 字符串反转后去除前导零
关键点
- 使用string处理输入
- 处理负号情况
- 去除反转后的前导零
代码
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
// 处理负号
if (s[0] == '-') {
cout << "-";
s = s.substr(1);
}
// 反转字符串
reverse(s.begin(), s.end());
// 去除前导零
string result;
bool found = false;
for (char c : s) {
if (!found && c == '0') continue;
found = true;
result += c;
}
// 处理全零的情况
if (result.empty()) result = "0";
cout << result << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while (t--) {
solve();
}
return 0;
}
import java.util.*;
public class Main {
static void solve(Scanner sc) {
String s = sc.next();
// 处理负号
if (s.charAt(0) == '-') {
System.out.print("-");
s = s.substring(1);
}
// 反转字符串
StringBuilder sb = new StringBuilder(s);
sb.reverse();
s = sb.toString();
// 去除前导零
StringBuilder result = new StringBuilder();
boolean found = false;
for (char c : s.toCharArray()) {
if (!found && c == '0') continue;
found = true;
result.append(c);
}
// 处理全零的情况
if (result.length() == 0) result.append("0");
System.out.println(result);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 1;
while (t-- > 0) {
solve(sc);
}
sc.close();
}
}
def solve():
s = input()
# 处理负号
if s[0] == '-':
print('-', end='')
s = s[1:]
# 反转字符串并去除前导零
s = s[::-1]
result = s.lstrip('0')
# 处理全零的情况
if not result:
result = '0'
print(result)
def main():
t = 1
while t:
solve()
t -= 1
if __name__ == "__main__":
main()
算法及复杂度
- 算法:字符串处理
- 时间复杂度: O ( n ) \mathcal{O}(n) O(n), n n n 为字符串长度
- 空间复杂度: O ( n ) \mathcal{O}(n) O(n),需要存储结果字符串
1570

被折叠的 条评论
为什么被折叠?



