题解 | #懂二进制#

懂二进制

https://www.nowcoder.com/practice/120e406db3fd46f09d55d59093f13dd8

import java.util.*;
public class Solution {

    public int countBitDiff (int m, int n) {
        //将两数的异或值转换为二进制存入字符串
        String x = Integer.toBinaryString(m^n);
        //计算1的个数
        int count = 0;
        
        for(int i=0;i<x.length();i++){
            if(x.charAt(i)=='1'){
                count++;
            }
        }
        return count;
    }
}
全部评论

相关推荐

05-23 19:02
吉林大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
04-25 10:45
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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