[力扣] 算法 1758 (C#)

1758. 生成交替二进制字符串的最少操作数

public class Solution {
    public int MinOperations(string s) {
        var (turn0, turn1) = (0, 0);
        (s[0] == '1' ? ref turn0 : ref turn1) = 1;
        foreach (var ch in s.AsSpan(1))
        {
            (turn0, turn1) = (turn1, turn0);
            (ch == '1' ? ref turn0 : ref turn1)++;
        }
        return Math.Min(turn0, turn1);
    }
}
一图流

发表评论