1 public class Solution { 2 public int findComplement(int num) { 3 int index = 0; 4 int ans = 0; 5 while (num > 0) { 6 ans += ((num % 2) ^ 1) << index; 7 num /= 2; 8 index++; 9 } 10 return ans; 11 } 12 }
注意这里num要%2再与1异或
1 public class Solution { 2 public int findComplement(int num) { 3 int index = 0; 4 int ans = 0; 5 while (num > 0) { 6 ans += ((num % 2) ^ 1) << index; 7 num /= 2; 8 index++; 9 } 10 return ans; 11 } 12 }
注意这里num要%2再与1异或
转载于:https://www.cnblogs.com/yingzhongwen/p/6433882.html