Skip to content

Commit 47e88aa

Browse files
committed
20190225
1 parent 990149d commit 47e88aa

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

code/lc190.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package code;
2+
3+
/*
4+
* 190. Reverse Bits
5+
* 题意:转换为二进制,反转,再输出
6+
* 难度:Easy
7+
* 分类:Bit Manipulation
8+
* 思路:做位移操作,自己没想起来,好好看看
9+
* Tips:注意 >>> 无符号位移 它不会将所处理的值的最高位视为正负符号,所以作位移处理时,会直接在空出的高位填入0
10+
*/
11+
public class lc190 {
12+
public static void main(String[] args) {
13+
System.out.println(reverseBits(10));
14+
}
15+
// you need treat n as an unsigned value
16+
public static int reverseBits(int n) {
17+
int result = 0;
18+
for (int i = 0; i < 32 ; i++) {
19+
result += n&1;
20+
n >>>= 1; //无视符号右移
21+
if(i<31) result<<=1;
22+
}
23+
return result;
24+
}
25+
}

code/lc191.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package code;
2+
/*
3+
* 191. Number of 1 Bits
4+
* 题意:统计二进制数中1的个数
5+
* 难度:Easy
6+
* 分类:Bit Manipulation
7+
* 思路:每次移一位,与运算
8+
* Tips:
9+
*/
10+
public class lc191 {
11+
// you need to treat n as an unsigned value
12+
public int hammingWeight(int n) {
13+
int sum = 0;
14+
int a = 1;
15+
while(a!=0){
16+
int b = n&a;
17+
if(b!=0) sum += 1;
18+
a <<= 1;
19+
}
20+
return sum;
21+
}
22+
}

code/lc202.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package code;
2+
/*
3+
* 202. Happy Number
4+
* 题意:各位置上的数字循环求平方和,如果可以等于1,则为Happy Number
5+
* 难度:Easy
6+
* 分类:Hash Table, Math
7+
* 思路:如果不会等于1,则会产生循环,用hashset记录之前是否出现过
8+
* 也可利用判断链表是否有环的思路,一个计算一步,一个计算两步,看是会相等来判断
9+
* Tips:
10+
*/
11+
import java.util.HashSet;
12+
13+
public class lc202 {
14+
public boolean isHappy(int n) {
15+
HashSet hs = new HashSet<Integer>();
16+
hs.add(n);
17+
int sum = 0;
18+
while(true){
19+
sum = 0;
20+
while(n!=0){
21+
sum += Math.pow(n%10,2);
22+
n = n/10;
23+
}
24+
if(sum==1) return true;
25+
else if(hs.contains(sum)) break;
26+
hs.add(sum);
27+
n = sum;
28+
System.out.println(sum);
29+
}
30+
return false;
31+
}
32+
}

code/lc204.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package code;
2+
/*
3+
* 204. Count Primes
4+
* 题意:计算小于n的素数的个数
5+
* 难度:Easy
6+
* 分类:Hash Table, Math
7+
* 思路:两个循环,相乘得到的数不是素数。
8+
* Tips:
9+
*/
10+
public class lc204 {
11+
public int countPrimes(int n) {
12+
if(n<3) return 0;
13+
boolean[] flags = new boolean[n];
14+
int sum = 0;
15+
for(int i=2; i<n; i++){
16+
if(flags[i]==false) sum++; // 内循环已经计算过了
17+
for(int j=2; i*j<n; j++){ // i*j<n
18+
flags[i*j] = true;
19+
}
20+
}
21+
return sum;
22+
}
23+
}

code/lc217.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package code;
2+
3+
import java.util.HashSet;
4+
/*
5+
* 217. Contains Duplicate
6+
* 题意:数组中是否有重复的数字
7+
* 难度:Easy
8+
* 分类:Array, Hash Table
9+
* 思路:用Hashset记录是否有重复
10+
* Tips:
11+
*/
12+
public class lc217 {
13+
public boolean containsDuplicate(int[] nums) {
14+
HashSet hs = new HashSet<Integer>();
15+
for(int i=0; i<nums.length; i++){
16+
if(hs.contains(nums[i])) return true;
17+
hs.add(nums[i]);
18+
}
19+
return false;
20+
}
21+
}

0 commit comments

Comments
 (0)