Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-11-29 #436

Open Zheaoli opened 1 year ago

Zheaoli commented 1 year ago

2022-11-29

dreamhunter2333 commented 1 year ago
#include <iostream>
#include <unordered_map>
using namespace std;
/*
 * @lc app=leetcode.cn id=1758 lang=cpp
 *
 * [1758] 生成交替二进制字符串的最少操作数
 */

// @lc code=start
class Solution
{
public:
    int minOperations(string s)
    {
        unordered_map<char, char> bit_map;
        bit_map['1'] = '0';
        bit_map['0'] = '1';
        char bit = '0';
        int count_0 = 0;
        int count_1 = 0;
        for (auto &&c : s)
        {
            if (bit == c)
                count_0++;
            else
                count_1++;
            bit = bit_map[bit];
        }
        return min(count_0, count_1);
    }
};
// @lc code=end

微信id: 而我撑伞 来自 vscode 插件