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语言位域

    转载自 http://tonybai.com/2013/05/21/talk-about-bitfield-in-c-again/ 再谈C语言位域 五 21 bigwhite技术志 bitfield, ...

  2. tp中ueditor编辑器的使用

    1/引入三个文件 <script type="text/javascript" charset="utf-8" src="{$Think.con ...

  3. Linux下安装tensorflow

  4. 消除左递归c语言文法

    <程序> -〉 <外部声明> | <函数定义><外部声明> -〉<头文件> | <变量> | <结构体> <头 ...

  5. js中Window 对象及其的方法

    window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 wind ...

  6. bzoj 4695: 最假女选手

    ……一道丧病线段树膜板题…… 被常数卡的死去活来……QAQ 学到了些奇技淫巧:把取min标记 和 区间最小值 合并 可以快很多…… #include <bits/stdc++.h> #de ...

  7. em与px换算关系以及常用列表

    1.任意浏览器的默认字体大小都是16px.2.所有未经调整的浏览器都符合: 1em=16px 12px=0.75em 10px=0.625em3.为了简化font-size的换算,在body选择器中声 ...

  8. linux回退到上次访问目录

    cd / cd ..  回到上级目录 cd - 回到上次访问目录

  9. nginx windows负载均衡入门

    前言 做了几年开发,都是只管码代码,没有参与过项目的部署,为了知识体系更加完整,于是开始学习一下负载均衡.查了一下资料,觉得用nginx +iis 比较简单,于是小试牛刀. 步骤 准备工作 下载ngi ...

  10. Bing Test -必应每日壁纸自动换

    今天向大家推荐一个桌面美化类的工具,没错就是自动更换壁纸,而且是精美的必应每日壁纸哦!绿色小巧,开机自启动,设置后每日自动更新你的桌面~ 软件名称:Bing Test 链接: http://pan.b ...