力扣
public int rangeBitwiseAnd(int m, int n) {
int shift = 0;
while (m < n) {
m >>= 1;
n >>= 1;
shift++;
}
return m <<= shift;
}
力扣
class Solution {
public int[] countBits(int n) {
int[] res = new int[n + 1];
for (int i = 0; i <= n; i++) {
res[i] = Integer.bitCount(i);
}
return res;
}
}
##############自己利用位运算实现bitCount方法##################
class Solution {
public int[] countBits(int n) {
int[] res = new int[n + 1];
for (int i = 0; i <= n; i++) {
res[i] = bitCount(i);
}
return res;
}
public int bitCount(int i) {
int res = 0;
while (i > 0) {
i &= i - 1;
res++;
}
return res;
}
}
####################动态规划思想加位运算####################
class Solution {
public int[] countBits(int n) {
int[] res = new int[n + 1];
for (int i = 1; i <= n; i++) {
res[i] = res[i & (i - 1)] + 1;
}
return res;
}
}
力扣
public int insertBits(int N, int M, int i, int j) {
int mask = ((1 << (j - i + 1)) - 1) << i;
mask = ~mask;
N &= mask;
M <<= i;
return N | M;
}
力扣
class Solution {
public String printBin(double num) {
StringBuilder sb = new StringBuilder("0.");
for (int i = 1; i < 31 && num > 0; i++) {
double n = Math.pow(0.5, i);
if (num >= n) {
num -= n;
sb.append(1);
} else sb.append(0);
}
return num == 0 ? sb.toString() : "ERROR";
}
}
力扣
class Solution {
public int reverseBits(int num) {
if (num == -1) return 32;
int cnt1 = 1;
int pos = -1;
int result = 0;
for (int i = 0; i < 33; i++) {
if ((num & 1) == 1) {
cnt1++;
} else {
result = Math.max(result, cnt1);
cnt1 = i - pos;
pos = i;
}
num >>>= 1;
}
return result;
}
}
力扣
class Solution {
public int convertInteger(int A, int B) {
return Integer.bitCount(A^B);
}
}
力扣
class Solution {
public int exchangeBits(int num) {
int odd = num & 0x55555555;
int even = num & 0xaaaaaaaa;
odd <<= 1;even >>>= 1;
return odd | even;
}
}
力扣
class Solution {
public int waysToStep(int n) {
if (n < 3) return n;
int[] dp = new int[n + 1];
dp[0] = 1;dp[1] = 1;dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = ((dp[i - 1] + dp[i - 2]) % 1000000007 + dp[i - 3]) % 1000000007;
}
return dp[n];
}
}
class Solution {
public int waysToStep(int n) {
int a = 1, b = 2, c = 4;
int MAX = 1000000007;
if (n == 1) return a;
if (n == 2) return b;
if (n == 3) return c;
for (int i = 3; i < n; i++) {
int temp = ((a + b) % 1000000007 + c) % 1000000007;
a = b;
b = c;
c = temp;
}
return c;
}
}
力扣
class Solution {
public int multiply(int A, int B) {
int min = Math.min(A, B);
int max = Math.max(A, B);
int ans = 0;
for (int i = 0; min != 0; i++) {
if ((min & 1) == 1) {
ans += max << i;
}
min >>= 1;
}
return ans;
}
}
力扣
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[] arr = {0};
System.out.println(solution.missingNumber(arr));
}
//数学方法
public int missingNumber(int[] nums) {
int n = nums.length;
Long sum = 0L;
for (int i = 0; i < n; i++) {
sum += nums[i];
}
return (int) (n * (n + 1) / 2 - sum);
}
//空间换时间
public int missingNumber2(int[] nums) {
boolean[] a=new boolean[nums.length+1];
for(int i=0;i<nums.length;i++){
a[nums[i]]=true;
}
for(int i=0;i<a.length;i++){
if(a[i] == false){
return i;
}
}
return 0;
}
//异或运算a ^b ^ b = a, a ^ b ^a = b;
public int missingNumber3(int[] nums) {
int n = nums.length;
int ans = 0;
for (int i = 1; i <= n; i++) {
ans ^= i;
ans ^= nums[i - 1];
}
return ans;
}
}
力扣
class Solution {
public int add(int a, int b) {
int m = a ^ b; //不进位的结果
int n = (a & b) << 1; //进位
while (n != 0) {
int temp = m ^ n;
n = (m & n) << 1;
m = temp;
}
return m;
}
}