2000.反转单词前缀
链接:2000.反转单词前缀
难度:Easy
标签:双指针、字符串
简介:给你一个下标从 0 开始的字符串 word 和一个字符 ch 。找出 ch 第一次出现的下标 i ,反转 word 中从下标 0 开始、直到下标 i 结束(含下标 i )的那段字符。如果 word 中不存在字符 ch ,则无需进行任何操作。
题解 1 - cpp
- 编辑时间:2022-02-03
- 内存消耗:6.1MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
   public:
    string reversePrefix(string word, char ch) {
        int idx = 0;
        while (idx < word.size() && word[idx] != ch) idx++;
        if (idx == word.size()) return word;
        string ans = word.substr(0, idx + 1);
        reverse(ans.begin(), ans.end());
        ans += word.substr(idx + 1, word.size() - idx - 1);
        return ans;
    }
};
题解 2 - cpp
- 编辑时间:2022-02-03
- 内存消耗:6.2MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
   public:
    string reversePrefix(string word, char ch) {
        int idx = 0;
        while (idx < word.size() && word[idx] != ch) idx++;
        if (idx == word.size()) return word;
        string ans = "";
        for (int i = idx; i >= 0; i--) ans += word[i];
        for (int i = idx + 1; i < word.size(); i++) ans += word[i];
        return ans;
    }
};