Skip to content

Commit e6c3be3

Browse files
committed
add class 05
1 parent e6028d0 commit e6c3be3

File tree

5 files changed

+400
-0
lines changed

5 files changed

+400
-0
lines changed

src/class05/Code01_BitMap1.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package class05;
2+
3+
import java.util.HashSet;
4+
5+
// 这个类的实现是错误的
6+
// 请问为什么?
7+
public class Code01_BitMap1 {
8+
9+
public static class BitMap {
10+
11+
private long[] bits;
12+
13+
public BitMap(int max) {
14+
bits = new long[(max + 64) >> 6];
15+
}
16+
17+
public void add(int num) {
18+
bits[num >> 6] |= (1 << (num & 63));
19+
}
20+
21+
public void delete(int num) {
22+
bits[num >> 6] &= ~(1 << (num & 63));
23+
}
24+
25+
public boolean contains(int num) {
26+
return (bits[num >> 6] & (1 << (num & 63))) != 0;
27+
}
28+
29+
}
30+
31+
public static void main(String[] args) {
32+
System.out.println("测试开始!");
33+
int max = 10000;
34+
BitMap bitMap = new BitMap(max);
35+
HashSet<Integer> set = new HashSet<>();
36+
int testTime = 10000000;
37+
for (int i = 0; i < testTime; i++) {
38+
int num = (int) (Math.random() * (max + 1));
39+
double decide = Math.random();
40+
if (decide < 0.333) {
41+
bitMap.add(num);
42+
set.add(num);
43+
} else if (decide < 0.666) {
44+
bitMap.delete(num);
45+
set.remove(num);
46+
} else {
47+
if (bitMap.contains(num) != set.contains(num)) {
48+
System.out.println("Oops!");
49+
break;
50+
}
51+
}
52+
}
53+
for (int num = 0; num <= max; num++) {
54+
if (bitMap.contains(num) != set.contains(num)) {
55+
System.out.println("Oops!");
56+
}
57+
}
58+
System.out.println("测试结束!");
59+
}
60+
61+
}

src/class05/Code02_BitMap2.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package class05;
2+
3+
import java.util.HashSet;
4+
5+
public class Code02_BitMap2 {
6+
7+
// 这个类的实现是正确的
8+
public static class BitMap {
9+
10+
private long[] bits;
11+
12+
public BitMap(int max) {
13+
bits = new long[(max + 64) >> 6];
14+
}
15+
16+
public void add(int num) {
17+
bits[num >> 6] |= (1L << (num & 63));
18+
}
19+
20+
public void delete(int num) {
21+
bits[num >> 6] &= ~(1L << (num & 63));
22+
}
23+
24+
public boolean contains(int num) {
25+
return (bits[num >> 6] & (1L << (num & 63))) != 0;
26+
}
27+
28+
}
29+
30+
public static void main(String[] args) {
31+
System.out.println("测试开始!");
32+
int max = 10000;
33+
BitMap bitMap = new BitMap(max);
34+
HashSet<Integer> set = new HashSet<>();
35+
int testTime = 10000000;
36+
for (int i = 0; i < testTime; i++) {
37+
int num = (int) (Math.random() * (max + 1));
38+
double decide = Math.random();
39+
if (decide < 0.333) {
40+
bitMap.add(num);
41+
set.add(num);
42+
} else if (decide < 0.666) {
43+
bitMap.delete(num);
44+
set.remove(num);
45+
} else {
46+
if (bitMap.contains(num) != set.contains(num)) {
47+
System.out.println("Oops!");
48+
break;
49+
}
50+
}
51+
}
52+
for (int num = 0; num <= max; num++) {
53+
if (bitMap.contains(num) != set.contains(num)) {
54+
System.out.println("Oops!");
55+
}
56+
}
57+
System.out.println("测试结束!");
58+
}
59+
60+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package class05;
2+
3+
// 测试链接:https://leetcode.com/problems/divide-two-integers
4+
public class Code03_BitAddMinusMultiDiv {
5+
6+
public static int add(int a, int b) {
7+
int sum = a;
8+
while (b != 0) {
9+
sum = a ^ b;
10+
b = (a & b) << 1;
11+
a = sum;
12+
}
13+
return sum;
14+
}
15+
16+
public static int negNum(int n) {
17+
return add(~n, 1);
18+
}
19+
20+
public static int minus(int a, int b) {
21+
return add(a, negNum(b));
22+
}
23+
24+
public static int multi(int a, int b) {
25+
int res = 0;
26+
while (b != 0) {
27+
if ((b & 1) != 0) {
28+
res = add(res, a);
29+
}
30+
a <<= 1;
31+
b >>>= 1;
32+
}
33+
return res;
34+
}
35+
36+
public static boolean isNeg(int n) {
37+
return n < 0;
38+
}
39+
40+
public static int div(int a, int b) {
41+
int x = isNeg(a) ? negNum(a) : a;
42+
int y = isNeg(b) ? negNum(b) : b;
43+
int res = 0;
44+
for (int i = 31; i > negNum(1); i = minus(i, 1)) {
45+
if ((x >> i) >= y) {
46+
res |= (1 << i);
47+
x = minus(x, y << i);
48+
}
49+
}
50+
return isNeg(a) ^ isNeg(b) ? negNum(res) : res;
51+
}
52+
53+
public static int divide(int dividend, int divisor) {
54+
if (divisor == Integer.MIN_VALUE) {
55+
return dividend == Integer.MIN_VALUE ? 1 : 0;
56+
}
57+
if (dividend == Integer.MIN_VALUE) {
58+
if (divisor == negNum(1)) {
59+
return Integer.MAX_VALUE;
60+
}
61+
int res = div(add(dividend, 1), divisor);
62+
return add(res, div(minus(dividend, multi(res, divisor)), divisor));
63+
}
64+
return div(dividend, divisor);
65+
}
66+
67+
}

src/class05/Code04_GetMax.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package class05;
2+
3+
public class Code04_GetMax {
4+
5+
public static int flip(int n) {
6+
return n ^ 1;
7+
}
8+
9+
public static int sign(int n) {
10+
return flip((n >> 31) & 1);
11+
}
12+
13+
public static int getMax1(int a, int b) {
14+
int c = a - b;
15+
int scA = sign(c);
16+
int scB = flip(scA);
17+
return a * scA + b * scB;
18+
}
19+
20+
public static int getMax2(int a, int b) {
21+
int c = a - b;
22+
int sa = sign(a);
23+
int sb = sign(b);
24+
int sc = sign(c);
25+
int difSab = sa ^ sb;
26+
int sameSab = flip(difSab);
27+
int returnA = difSab * sa + sameSab * sc;
28+
int returnB = flip(returnA);
29+
return a * returnA + b * returnB;
30+
}
31+
32+
public static void main(String[] args) {
33+
int a = -16;
34+
int b = 1;
35+
System.out.println(getMax1(a, b));
36+
System.out.println(getMax2(a, b));
37+
a = 2147483647;
38+
b = -2147480000;
39+
System.out.println(getMax1(a, b)); // wrong answer because of overflow
40+
System.out.println(getMax2(a, b));
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)