校招算法笔面试 | 校招笔面试真题-倒置字符串

题目## 题目

题目链接

解题思路

这是一个字符串处理问题。需要将句子中的单词顺序倒置,但保持标点符号位置不变。

关键点:

  1. 分割字符串获取单词
  2. 处理标点符号
  3. 倒序重组单词
  4. 保持原有格式

算法步骤:

  1. 分割字符串为单词
  2. 倒序重组单词
  3. 处理标点符号
  4. 输出结果

代码

#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),需要存储分割后的单词

题目链接

解题思路

使用字符串处理方法实现整数倒序输出:

  1. 读入为字符串处理
  2. 分离负号处理
  3. 字符串反转后去除前导零

关键点

  1. 使用string处理输入
  2. 处理负号情况
  3. 去除反转后的前导零

代码

#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),需要存储结果字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值