题解 | #懂二进制#

懂二进制

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

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param m int整型
     * @param n int整型
     * @return int整型
     */
    public int countBitDiff (int m, int n) {
        // write code here
        string strMB = Convert.ToString(m, 2).PadLeft(32, '0');
        string strNB = Convert.ToString(n, 2).PadLeft(32, '0');
        int nMinLen = strMB.Length > strNB.Length ? strNB.Length : strMB.Length;
        int nMaxLen = strMB.Length > strNB.Length ? strMB.Length : strNB.Length;
        int nCount = 0;
        for (int i = 0; i < nMinLen; i++) {
            if (strMB[i] == strNB[i])
                continue;
            nCount++;
        }
        return nCount + nMaxLen - nMinLen;
    }
}

全部评论

相关推荐

这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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