Single Number II
2015-09-17 03:30
281 查看
Given an array of integers, every element appears three times
except for one. Find that single one.
http://shmilyaw-hotmail-com.iteye.com/blog/2153924
except for one. Find that single one.
public int singleNumber(int[] A) { if (A == null || A.length == 0) return 0; int[] a = new int[32]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < 32; j++) { //1<<j == 1向左移动j位 //A[i] & (1 << j)可以判断A[i]二进制中有哪几位为1 if ((A[i] & (1 << j)) != 0) //[j]这是倒过来的 //e.g A[i] = 11 = 1011 ==> //a[j] = 11010000... //+1 %3后,所有出现3次的数变为0 a[j] = (a[j] + 1) % 3; } } int result = 0; for (int i = 0; i < 32; i++) { if (a[i] > 0) //a[i] << i将反过来的结果正过来 result |= (a[i] << i); } return result; }
http://shmilyaw-hotmail-com.iteye.com/blog/2153924
相关文章推荐
- 从Activity的启动细窥BinderIPC(1)
- 课下参考四 配置并使用ssh登录LINUX的主机--windows篇
- 「LintCode」java字符串練習四──anagrams
- CSS样式小总结
- 《女士品茶》与统计检验
- 浅谈Linux中的信号处理机制(一)
- [CareerCup] 9.1 Climbing Staircase 爬楼梯
- cf#320 Div.2 Problem D "Or" Game
- 二叉树重建及遍历
- *LeetCode-Remove Linked List Elements
- git-cola
- Codeforces #320(div2)
- ubuntu彻底清除Apache+Mysql+PHP
- HTML&CSS Learning Notes 5
- form 提交
- Leetcode Binary Tree Paths
- cf#320 Div.2 Problem C A Problem about Polyline
- cf#320 Div.2 Problem A Raising Bacteria
- Scala actor 简单示例
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round]