题解 | #字符串变形#

字符串变形

https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e

class Solution {
public:
    string trans(string s, int n) {
        // write code here
        stack<char> a1;
        stack<char> a2;
        string s2;
        for(int i=0;i<n;i++){
            if(s[i] == ' '){
                while(!a1.empty()){
                    a2.push(a1.top());
                    a1.pop();
            }
            a2.push(s[i]);
            continue;
        }
        if(s[i]>='A' && s[i] <='Z') s[i] +=32;
        else if (s[i]>='a' && s[i] <= 'z') s[i] -=32;
        a1.push(s[i]);
        }
        while(!a1.empty()){
            a2.push(a1.top());
            a1.pop();
        }
        while(!a2.empty()){
            s2.push_back(a2.top());
            a2.pop();
        }
        return s2;
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务