题解 | #牛群的编码统计#

牛群的编码统计

https://www.nowcoder.com/practice/89500cbfd12a4c9f893aafb3c308baa2

知识点

位运算

解题思路

循环32次,每次让n右移i为来&1,如果为0了,说明n的倒数第i位为0,那么ans加一,统计出n有多少个0。

Java题解

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return int整型
     */
    public int countZeros (int n) {
        // write code here
        int ans = 0;
        for(int i = 0; i < 32; i++){
            ans += (n >> i & 1) == 0 ? 1 : 0;
        }
        return ans;
    }
}

全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务