LeetCode 917. 仅仅反转字母

水题,注意vector和isalpha的使用。


LeetCode 917. 仅仅反转字母_第1张图片

c++代码如下:

class Solution {
public:
    string reverseOnlyLetters(string S) {
        string s(S);
        int len = S.length();
        int j=len-1;
        for(int i=0; i<len; i++){
            if(isalpha(s[i])){
                for(; j>=0; j--){
                    if(isalpha(S[j])){
                        break;
                    }
                }
                s[i]=S[j];
                j--;
            }
        }
        return s;
    }
};

你可能感兴趣的:(算法)