0
点赞
收藏
分享

微信扫一扫

[LeetCode 2000] 反转单词前缀

猫er聆听没落的旋律 2022-02-02 阅读 75

题目链接

https://leetcode-cn.com/problems/reverse-prefix-of-word/solution/fan-zhuan-dan-ci-qian-zhui-by-leetcode-s-ruaj/

题解

rt

class Solution {
public:
    string reversePrefix(string word, char ch) {
        string s = "";
        int i = 0;
        for(;i<word.length();i++)
        {
            s.push_back(word[i]);
            if(word[i] == ch)   break;
        }
        if(i == word.length())  return s;
        reverse(s.begin(), s.end());
        return s + word.substr(i+1, word.length() - (i+1));
    }
};
举报

相关推荐

0 条评论