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. 美团HD(2)-设置导航栏内容

    DJHomeViewController.m #import "DJHomeViewController.h" #import "DJConstantValue.h&qu ...

  2. iOS多线程-GCD之常用函数

    延迟执行任务函数dispatch_after(.....) -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEve ...

  3. NetBean 8 创建EJB

    一. 介绍 百度了一下关于在NetBean开发环境里创建EJB的教程,没有找到好的例子,2天的调试过程,写下来帮助后人. EJB (Enterprise Java Bean) 是一套高扩展性的开发企业 ...

  4. CSS基础总结

    CSS基础总结链接地址:http://segmentfault.com/a/1190000002773955

  5. .NET中的异步

    .NET中4种异步方式? ThreadPool.QueueUserworkItem实现 APM模式(就是BeginXXX和EndXXX成对出现.) EAP模式(就是Event based, 准确说来就 ...

  6. 有时打开myeclipse,部署报错解决方案

    1.首先关闭MyEclipse工作空间. 2.然后删除工作空间下的 "/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.g ...

  7. 久违的phpstorm

    原文:http://www.cnblogs.com/buyucoder/p/5291771.html 原文:http://idea.lanyus.com/

  8. js_闭包

    先从闭包特点解释,应该更好理解.闭包的两个特点:1.作为一个函数变量的一个引用 - 当函数返回时,其处于激活状态.2.一个闭包就是当一个函数返回时,一个没有释放资源的栈区.其实上面两点可以合成一点,就 ...

  9. Latex中画出函数文件的调用关系拓扑图

    流程图,思维导图,拓扑图通常能把我们遇到的一些复杂的关系结构用图形的方式展现出来.在Latex中要想画这样的拓扑图,有一个很好用的绘图工具包 pgf/tikz . 1.pgf/tikz的安装:pgf/ ...

  10. EF jsonignore

    页面单独指定不循环引用 [JsonIgnore] Newtonsoft.Json.JsonSerializerSettings jsSettings = new Newtonsoft.Json.Jso ...