天天看點

leetcode:第 218 場周賽:設計 Goal 解析器(字元串)

題目:

leetcode:第 218 場周賽:設計 Goal 解析器(字元串)

分析:

一道水題,把最後一個空出來的思路很好。

代碼:

class Solution {
public:
    string interpret(string command) {
        string s="";
        for(int i=0;i<command.size()-1;i++)
        {
            if(command[i]=='G') 
            {
                s+=command[i];
                continue;
            }
            if(command[i]=='(' && command[i+1]==')')
            {
                s+='o';
                i++;
                continue;
            }
            i++;
            while(command[i]!=')')
            {
                s+=command[i];
                i++;
            }
        }
        if(command[command.size()-1]=='G') s+=command[command.size()-1];
        return s;
    }
};