Skip to content

Commit bd19f16

Browse files
committed
add SingleNumberTwo --find three time number except one
1 parent 50d69f2 commit bd19f16

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

bin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/com/

src/com/jie/SingleNumberTwo.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.jie;
2+
3+
public class SingleNumberTwo {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
public int singleNumber(int[] nums) {
11+
int [] count = new int[32];
12+
int result=0;
13+
for(int i=0;i<32;i++){
14+
for(int j=0;j<nums.length;j++){
15+
//循环区得每一位之和 使用无符号向右移
16+
count[i]+=(nums[j]>>>i)&1;
17+
}
18+
//获取那一个只出现了一次的数 因为除了一个每一个数都出现了三次 所以我们出现的数的
19+
//和肯定是三的倍数 最后一个没有出现的数 要不然是0要不然是1 所以%3即可
20+
count[i]=count[i]%3==0?0:1;
21+
}
22+
//构造最后的结果
23+
for(int i=0;i<32;i++){
24+
25+
result|=count[i]<<i;
26+
}
27+
return result;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)