leetcode1529 灯泡开关 贪心

2021/5/30 10:25:58

本文主要是介绍leetcode1529 灯泡开关 贪心,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  贪心 JAVA:

    public final int minFlips(String target) {
        int len = target.length(), num = 0;
        for (int i = 0; i < len; i++) {
            char curr = num % 2 == 0 ? '0' : '1';
            if (target.charAt(i) == curr) continue;
            num++;
        }
        return num;
    }

  贪心 JS:

/**
 * @param {string} target
 * @return {number}
 */
var minFlips = function (target) {
    let len = target.length, num = 0;
    for (let i = 0; i < len; i++) {
        let current = num % 2 == 0 ? 0 : 1;
        if (target.charAt(i) == current) continue;
        num++;
    }
    return num;
};

 



这篇关于leetcode1529 灯泡开关 贪心的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程