0
点赞
收藏
分享

微信扫一扫

LeetCode-2038 如果相邻两个颜色均相同则删除当前颜色

dsysama 2022-03-22 阅读 14

 

 

题解 

/*
 * @lc app=leetcode.cn id=2038 lang=cpp
 *
 * [2038] 如果相邻两个颜色均相同则删除当前颜色
 */

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;


// @lc code=start
class Solution {
public:
    bool winnerOfGame(string colors) {
        int i = 0;
        int A = 0, B = 0;
        int n = colors.length();
        while (i < n) {
            if (colors[i] == 'A') {
                int t = 1;
                i++;
                while(i < n && colors[i] == 'A') {
                    i++;
                    t++;
                }
                if (t >= 3) A += t-2;
                continue;
            }
            if (colors[i] == 'B') {
                int t = 1;
                i++;
                while(i < n && colors[i] == 'B') {
                    i++;
                    t++;
                }
                if (t >= 3) B += t-2;
            }
        }
        if (A > B) return true;
        else return false;
    }
};
举报

相关推荐

0 条评论