剑指 Offer 56 - II. 数组中数字出现的次数 II(javascript)

一、题目地址

https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/comments/

二、具体代码

/**
 * @param {number[]} nums
 * @return {number}
 */
// 位运算,通用模版
// 时间复杂度:O(N)
// 空间复杂度:O(1)
 var singleNumber = function(nums) {
    //  正确做法,需要加上每一位填充0的细节
    let counts = new Array(32).fill(0);
    /* 
        错误做法,理由见打印结果:
        let counts = new Array(32);
        console.log(counts[1]);//undefind
     */
    for(let num of nums) {
        for(let j=0; j<32; j++) {
            counts[j] += (num & 1);
            num >>>= 1;
        }
    }
    let res = 0, m = 3;
    for(let i=0; i<32; i++) {
        res <<= 1;
        res |= (counts[31-i] % m);
    }
    return res;
};

三、补充链接

https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/solution/mian-shi-ti-56-ii-shu-zu-zhong-shu-zi-chu-xian-d-4/

四、补充部分

关注公众号:【深漂程序员小庄】:
内含丰富的学习资源和面试经验(不限前端、java),还有学习交流群可加,并且还有各大厂大佬可一起交流学习,一起进步~添加小庄微信,回复【加群】,可加入互联网技术交流群:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值