Skip to content

Commit f17cb61

Browse files
committed
add 371 java, update progress
1 parent 4782faf commit f17cb61

File tree

4 files changed

+144
-11
lines changed

4 files changed

+144
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@
546546
268| [Missing Number](https://leetcode.com/problems/missing-number/) | [Python](./leetcode_python/Bit_Manipulation/missing-number.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BitManipulation/MissingNumber.java) | _O(n)_ | _O(1)_ | Medium | Curated Top 75, `xor`,`amazon`,`fb`| OK* (2)
547547
318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [Python](./leetcode_python/Bit_Manipulation/maximum-product-of-word-lengths.py) | _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium |Bit Manipulation, Counting Sort, Pruning| AGAIN (not start)
548548
342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [Python](./leetcode_python/Bit_Manipulation/power-of-four.py) | _O(1)_ | _O(1)_ |Easy |`power of num`| OK*
549-
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Python](./leetcode_python/Bit_Manipulation/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Medium | Curated Top 75, `good basic`, `fb`| AGAIN********* (5) (bit op not start)
549+
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [Python](./leetcode_python/Bit_Manipulation/sum-of-two-integers.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BitManipulation/SumOfTwoIntegers.java) | _O(1)_ | _O(1)_ | Medium | Curated Top 75, `good basic`, `fb`| AGAIN********* (5) (bit op not start)
550550
389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [Python](./leetcode_python/Bit_Manipulation/find-the-difference.py) | _O(n)_ | _O(1)_ | Easy |`amazon`| OK (2)
551551
393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) |[Python](./leetcode_python/Bit_Manipulation/utf-8-validation.py) | _O(n)_ | _O(1)_ | Medium | `utf-8 encoding`, `google`, `fb`| AGAIN**** (not start*) (5)
552552
401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [Python](./leetcode_python/Bit_Manipulation/binary-watch.py) | _O(1)_ | _O(1)_ | Easy | | OK*

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20240313: 338,347
1+
20240313: 338,347,371
22
20240312: 297(again),300,322(again),323
33
20240311: 261(again!!!),268,269(again),271,295(again)
44
20240310: 213,217,226,230,235,238,242,252,253

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2024-05-07 -> ['338,347']
1+
2024-05-07 -> ['338,347,371']
22
2024-05-06 -> ['297(again),300,322(again),323']
33
2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)']
44
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
@@ -12,7 +12,7 @@
1212
2024-04-24 -> ['39,48(again),49,53,54']
1313
2024-04-23 -> ['20,21,23,33(again)']
1414
2024-04-22 -> ['1,3,5,4,19']
15-
2024-04-16 -> ['338,347']
15+
2024-04-16 -> ['338,347,371']
1616
2024-04-15 -> ['297(again),300,322(again),323']
1717
2024-04-14 -> ['261(again!!!),268,269(again),271,295(again)']
1818
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
@@ -23,27 +23,27 @@
2323
2024-04-08 -> ['100,102,104,105(again)']
2424
2024-04-07 -> ['73,76,79(again),91,25']
2525
2024-04-06 -> ['55(again),56,62,70']
26-
2024-04-03 -> ['338,347', '39,48(again),49,53,54']
26+
2024-04-03 -> ['338,347,371', '39,48(again),49,53,54']
2727
2024-04-02 -> ['297(again),300,322(again),323', '20,21,23,33(again)']
2828
2024-04-01 -> ['261(again!!!),268,269(again),271,295(again)', '1,3,5,4,19']
2929
2024-03-31 -> ['213,217,226,230,235,238,242,252,253']
3030
2024-03-30 -> ['207(again!!!),208,211(again),212']
3131
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
3232
2024-03-28 -> ['139(again),141,143(again)']
3333
2024-03-27 -> ['121,124(again),125,128,133(again)']
34-
2024-03-26 -> ['338,347', '100,102,104,105(again)']
34+
2024-03-26 -> ['338,347,371', '100,102,104,105(again)']
3535
2024-03-25 -> ['297(again),300,322(again),323', '73,76,79(again),91,25']
3636
2024-03-24 -> ['261(again!!!),268,269(again),271,295(again)', '55(again),56,62,70']
3737
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
3838
2024-03-22 -> ['207(again!!!),208,211(again),212']
39-
2024-03-21 -> ['338,347', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
39+
2024-03-21 -> ['338,347,371', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
4040
2024-03-20 -> ['297(again),300,322(again),323', '139(again),141,143(again)', '20,21,23,33(again)']
4141
2024-03-19 -> ['261(again!!!),268,269(again),271,295(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19']
42-
2024-03-18 -> ['338,347', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
42+
2024-03-18 -> ['338,347,371', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
4343
2024-03-17 -> ['297(again),300,322(again),323', '207(again!!!),208,211(again),212', '73,76,79(again),91,25']
44-
2024-03-16 -> ['338,347', '261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
45-
2024-03-15 -> ['338,347', '297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
46-
2024-03-14 -> ['338,347', '297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
44+
2024-03-16 -> ['338,347,371', '261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
45+
2024-03-15 -> ['338,347,371', '297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
46+
2024-03-14 -> ['338,347,371', '297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
4747
2024-03-13 -> ['297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
4848
2024-03-12 -> ['261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
4949
2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package LeetCodeJava.BitManipulation;
2+
3+
// https://leetcode.com/problems/sum-of-two-integers/description/
4+
5+
public class SumOfTwoIntegers {
6+
7+
// V0
8+
// IDEA : BIT OP
9+
// TODO : fix below
10+
// public int getSum(int a, int b) {
11+
//
12+
// if (a == 0 && b == 0){
13+
// return 0;
14+
// }
15+
//
16+
// if (a == 0 || b == 0){
17+
// if (a == 0){
18+
// return b;
19+
// }
20+
// return a;
21+
// }
22+
//
23+
// // bit op
24+
// String aBin = Integer.toBinaryString(a);
25+
// String bBin = Integer.toBinaryString(b);
26+
//
27+
// String res = addBinary(aBin, bBin);
28+
// return Integer.parseInt(res, 2);
29+
// }
30+
//
31+
// public static String addBinary(String a, String b) {
32+
// StringBuilder sb = new StringBuilder();
33+
// int i = a.length() - 1, j = b.length() - 1, carry = 0;
34+
// while (i >= 0 || j >= 0 || carry > 0) {
35+
// int sum = carry;
36+
// if (i >= 0) sum += a.charAt(i--) - '0';
37+
// if (j >= 0) sum += b.charAt(j--) - '0';
38+
// sb.append(sum % 2);
39+
// carry = sum / 2;
40+
// }
41+
// return sb.reverse().toString();
42+
// }
43+
//
44+
//// private int add(String x, String y){
45+
////
46+
//// int len = Math.max(x.length(), y.length());
47+
//// if (len > x.length()){
48+
//// x = (multiplyStr("0", len - x.length()) + x);
49+
//// }else{
50+
//// y = (multiplyStr("0", len - x.length()) + y);
51+
//// }
52+
////
53+
//// System.out.println("x = " + x + " y = " + y);
54+
////
55+
//// String res = "";
56+
////
57+
//// int plus = 0;
58+
//// for (int j = x.length()-1; j >= 0; j --){
59+
//// System.out.println("x.indexOf(j) = " + x.indexOf(j));
60+
//// int cur = Integer.parseInt(String.valueOf(x.indexOf(j))) + Integer.parseInt(String.valueOf(y.indexOf(j))) + plus;
61+
//// System.out.println("cur = " + cur);
62+
//// if (cur > 1){
63+
//// cur -= 2;
64+
//// plus = 1;
65+
//// res += cur;
66+
//// }else{
67+
//// res += cur;
68+
//// }
69+
//// }
70+
////
71+
//// if (plus != 0){
72+
//// res = "1" + res;
73+
//// }
74+
////
75+
//// // convert a Binary String to a base 10 integer in Java
76+
//// // https://stackoverflow.com/questions/10178980/how-to-convert-a-binary-string-to-a-base-10-integer-in-java
77+
////
78+
//// System.out.println("res = " + res);
79+
//// return Integer.parseInt(res, 2);
80+
//// }
81+
//
82+
// // https://stackoverflow.com/questions/2255500/can-i-multiply-strings-in-java-to-repeat-sequences
83+
// private static String multiplyStr(String s, int multiply){
84+
// StringBuilder r = new StringBuilder();
85+
// for (int i = 0; i < multiply; i++){
86+
// r.append(s);
87+
// }
88+
// return r.toString();
89+
// }
90+
91+
// V1
92+
// IDEA : BIT OP
93+
// https://leetcode.com/problems/sum-of-two-integers/solutions/4623531/java-best-easy-solution-100-beats-0ms/
94+
public int getSum_1(int a, int b) {
95+
while(b!=0)
96+
{
97+
int carry=a&b;
98+
a=a ^ b;
99+
b=carry<<1;
100+
}
101+
return a;
102+
}
103+
104+
// V2
105+
// https://leetcode.com/problems/sum-of-two-integers/solutions/84290/java-simple-easy-understand-solution-with-explanation/
106+
// Iterative
107+
public int getSum_2(int a, int b) {
108+
if (a == 0) return b;
109+
if (b == 0) return a;
110+
111+
while (b != 0) {
112+
int carry = a & b;
113+
a = a ^ b;
114+
b = carry << 1;
115+
}
116+
117+
return a;
118+
}
119+
120+
// V3
121+
// https://leetcode.com/problems/sum-of-two-integers/solutions/84290/java-simple-easy-understand-solution-with-explanation/
122+
// Iterative
123+
public int getSubtract_3(int a, int b) {
124+
while (b != 0) {
125+
int borrow = (~a) & b;
126+
a = a ^ b;
127+
b = borrow << 1;
128+
}
129+
130+
return a;
131+
}
132+
133+
}

0 commit comments

Comments
 (0)