标签:and 注意 number 多少 class markdown UI java IV
Determine the number of bits required to flip if you want to convert integer n to integer m.
Both n and m are 32-bit integers.
Given n = 31
(11111), m = 14
(01110), return 2
.
解题:比较两个整数对应的二进制数,共有多少位不同。注意,负数也包含在内。“>>>”在无符号右移,用0补齐。
1 public class Solution { 2 /** 3 * @param a: An integer 4 * @param b: An integer 5 * @return: An integer 6 */ 7 public int bitSwapRequired(int a, int b) { 8 // write your code here 9 int count = 0; 10 for (int c = a ^ b; c != 0; c = c >>> 1) { 11 count += c & 1; 12 } 13 return count; 14 } 15 }
181. Flip Bits【LintCode, by java】
标签:and 注意 number 多少 class markdown UI java IV
原文地址:https://www.cnblogs.com/phdeblog/p/9200683.html