Skip to content

Commit

Permalink
add 371 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 13, 2024
1 parent 4782faf commit f17cb61
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@
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)
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)
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*
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)
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)
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)
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)
401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [Python](./leetcode_python/Bit_Manipulation/binary-watch.py) | _O(1)_ | _O(1)_ | Easy | | OK*
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240313: 338,347
20240313: 338,347,371
20240312: 297(again),300,322(again),323
20240311: 261(again!!!),268,269(again),271,295(again)
20240310: 213,217,226,230,235,238,242,252,253
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-05-07 -> ['338,347']
2024-05-07 -> ['338,347,371']
2024-05-06 -> ['297(again),300,322(again),323']
2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)']
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
Expand All @@ -12,7 +12,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-16 -> ['338,347']
2024-04-16 -> ['338,347,371']
2024-04-15 -> ['297(again),300,322(again),323']
2024-04-14 -> ['261(again!!!),268,269(again),271,295(again)']
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
Expand All @@ -23,27 +23,27 @@
2024-04-08 -> ['100,102,104,105(again)']
2024-04-07 -> ['73,76,79(again),91,25']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['338,347', '39,48(again),49,53,54']
2024-04-03 -> ['338,347,371', '39,48(again),49,53,54']
2024-04-02 -> ['297(again),300,322(again),323', '20,21,23,33(again)']
2024-04-01 -> ['261(again!!!),268,269(again),271,295(again)', '1,3,5,4,19']
2024-03-31 -> ['213,217,226,230,235,238,242,252,253']
2024-03-30 -> ['207(again!!!),208,211(again),212']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['338,347', '100,102,104,105(again)']
2024-03-26 -> ['338,347,371', '100,102,104,105(again)']
2024-03-25 -> ['297(again),300,322(again),323', '73,76,79(again),91,25']
2024-03-24 -> ['261(again!!!),268,269(again),271,295(again)', '55(again),56,62,70']
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['338,347', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-21 -> ['338,347,371', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['297(again),300,322(again),323', '139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['261(again!!!),268,269(again),271,295(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['338,347', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-18 -> ['338,347,371', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-17 -> ['297(again),300,322(again),323', '207(again!!!),208,211(again),212', '73,76,79(again),91,25']
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']
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)']
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)']
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']
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)']
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)']
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']
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)']
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']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package LeetCodeJava.BitManipulation;

// https://leetcode.com/problems/sum-of-two-integers/description/

public class SumOfTwoIntegers {

// V0
// IDEA : BIT OP
// TODO : fix below
// public int getSum(int a, int b) {
//
// if (a == 0 && b == 0){
// return 0;
// }
//
// if (a == 0 || b == 0){
// if (a == 0){
// return b;
// }
// return a;
// }
//
// // bit op
// String aBin = Integer.toBinaryString(a);
// String bBin = Integer.toBinaryString(b);
//
// String res = addBinary(aBin, bBin);
// return Integer.parseInt(res, 2);
// }
//
// public static String addBinary(String a, String b) {
// StringBuilder sb = new StringBuilder();
// int i = a.length() - 1, j = b.length() - 1, carry = 0;
// while (i >= 0 || j >= 0 || carry > 0) {
// int sum = carry;
// if (i >= 0) sum += a.charAt(i--) - '0';
// if (j >= 0) sum += b.charAt(j--) - '0';
// sb.append(sum % 2);
// carry = sum / 2;
// }
// return sb.reverse().toString();
// }
//
//// private int add(String x, String y){
////
//// int len = Math.max(x.length(), y.length());
//// if (len > x.length()){
//// x = (multiplyStr("0", len - x.length()) + x);
//// }else{
//// y = (multiplyStr("0", len - x.length()) + y);
//// }
////
//// System.out.println("x = " + x + " y = " + y);
////
//// String res = "";
////
//// int plus = 0;
//// for (int j = x.length()-1; j >= 0; j --){
//// System.out.println("x.indexOf(j) = " + x.indexOf(j));
//// int cur = Integer.parseInt(String.valueOf(x.indexOf(j))) + Integer.parseInt(String.valueOf(y.indexOf(j))) + plus;
//// System.out.println("cur = " + cur);
//// if (cur > 1){
//// cur -= 2;
//// plus = 1;
//// res += cur;
//// }else{
//// res += cur;
//// }
//// }
////
//// if (plus != 0){
//// res = "1" + res;
//// }
////
//// // convert a Binary String to a base 10 integer in Java
//// // https://stackoverflow.com/questions/10178980/how-to-convert-a-binary-string-to-a-base-10-integer-in-java
////
//// System.out.println("res = " + res);
//// return Integer.parseInt(res, 2);
//// }
//
// // https://stackoverflow.com/questions/2255500/can-i-multiply-strings-in-java-to-repeat-sequences
// private static String multiplyStr(String s, int multiply){
// StringBuilder r = new StringBuilder();
// for (int i = 0; i < multiply; i++){
// r.append(s);
// }
// return r.toString();
// }

// V1
// IDEA : BIT OP
// https://leetcode.com/problems/sum-of-two-integers/solutions/4623531/java-best-easy-solution-100-beats-0ms/
public int getSum_1(int a, int b) {
while(b!=0)
{
int carry=a&b;
a=a ^ b;
b=carry<<1;
}
return a;
}

// V2
// https://leetcode.com/problems/sum-of-two-integers/solutions/84290/java-simple-easy-understand-solution-with-explanation/
// Iterative
public int getSum_2(int a, int b) {
if (a == 0) return b;
if (b == 0) return a;

while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}

return a;
}

// V3
// https://leetcode.com/problems/sum-of-two-integers/solutions/84290/java-simple-easy-understand-solution-with-explanation/
// Iterative
public int getSubtract_3(int a, int b) {
while (b != 0) {
int borrow = (~a) & b;
a = a ^ b;
b = borrow << 1;
}

return a;
}

}

0 comments on commit f17cb61

Please sign in to comment.