题解 | #整数转化#

整数转化

https://www.nowcoder.com/practice/c7df20a5a39e4357aecc1071e7fd523c

import java.util.*;

public class Transform {
    public int calcCost(int A, int B) {
        // write code here
	  //异或,位数相同抵消,位数不同值为1,记录1的个数
        int out = A^B;
        int result = 0;
        String s = Integer.toBinaryString(out);   
        for(int i = 0;i < s.length();i++){
            if(s.charAt(i) == '1'){
                result++;
            }
        }   
        return result;
    }
}

全部评论

相关推荐

白火同学:大二有这水平很牛了,可以适当对关键信息加粗一点,比如关键技术、性能指标之类的。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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