1. 程式人生 > >LeetCode 917. 僅僅反轉字母

LeetCode 917. 僅僅反轉字母

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


題目

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; } };