137. Single Number II

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

记录32个bit每个bit出现的次数不能被3整除的几位加起来。
 
201. Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.

Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.

public class Solution {
public int rangeBitwiseAnd(int m, int n) {
int x = 0x40000000;
//find the first binary from where m is different from n
int i = 1;
for(i = 1; i < 32; i++){
int a = m & x;
int b = n & x;
if(a != b){
break;
}
x = x >> 1;
}
i--;
//i is the last binary where m is the same as n
int y = 0x80000000;
y = y >> i;
int result = m & y;
return result;
}
}

九章的代码更短

268. Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

public class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int sum = nums[0];
for (int i = 1; i < n; i++) {
sum = sum ^ nums[i];
}
int sum2 = 0;
for (int i = 1; i <= n; i++) {
sum = sum ^ i;
}
return sum2 ^ sum;
}
}

how to decide if a number is power of 2?

(num&-num) == num

[leetcode]题型整理之用bit统计个数的更多相关文章

  1. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  2. [leetcode] 题型整理之二叉树

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  3. [leetcode] 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  4. [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余

    需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...

  5. [leetcode] 题型整理之cycle

    找到环的起点. 一快一慢相遇初,从头再走再相逢.

  6. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  7. [leetcode] 题型整理之查找

    1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...

  8. [leetcode] 题型整理之排序

    75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...

  9. [leetcode] 题型整理之字符串处理

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = &q ...

随机推荐

  1. C数组下标越界

    之前总听说C语言的各种毛病,今天算是遇到一个:数组下标越界 事情经过 两段完成不相干的代码,一段是测温度的,一段是测转速的.两段代码单独运行都没有问题,但是若运行测转速的代码,测温度的数据就会发生错误 ...

  2. ThinkPHP的URL访问

    url访问 http://www.kancloud.cn/manual/thinkphp5/118012 ThinkPHP5.0在没有启用路由的情况下典型的URL访问规则是: http://serve ...

  3. C# 反射研究

    概念 反射这东西,对于我这种小白,听起来总是觉得好大上的. 当初理解它费了一点时间,后来看了一句话,突然恍然大悟,“反射就跟B超一样,我们在不剖开人体的情况下想看清楚内部情况, 我们就通过发射超声波, ...

  4. javascript数组array

    注意:1.array的length不是只读的.可以从数组的末尾移出项或者向数组中添加新项.看下面例子: var colors = ["red","yellow" ...

  5. nio

    1.I/O 输入输出流 (1) 指的是计算机与外界,或者程序与计算机之间数据交换的接口. (2) 在java编程中,使用 流(Stream) 的方式完成I/O , 所有的I/O都被视为单个字节的移动. ...

  6. sss

    function handleTouchEvent(event) {    if (event.touches.length == 1) {        var output = document. ...

  7. Volley框架使用笔记

    1.初始化请求队列 RequestQueue RequestQueue queue= Volley.newRequestQueue(context); 2.StringRequest 网络请求 Get ...

  8. Picard报错“MAPQ should be 0 for unmapped read”的解决方法

    picard对bwa生成的sam文件进行reorder时,报错如下: Getting Help Exception in thread "main" htsjdk.samtools ...

  9. vmare centos 6.8 minimal 无法上网

    主机:win7 工具:vmare 11.0.0 系统:http://mirrors.163.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-minimal.i ...

  10. MyBK