-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the leetcode and singlelinkedlist changes
- Loading branch information
1 parent
0239d78
commit 0f88e99
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.math.BigInteger; | ||
|
||
public class BinarySum { | ||
|
||
/* | ||
* Given two binary strings a and b, return their sum as a binary string. | ||
Example 1: | ||
Input: a = "11", b = "1" | ||
Output: "100" | ||
Example 2: | ||
Input: a = "1010", b = "1011" | ||
Output: "10101" | ||
* | ||
*/ | ||
public static void main(String[] args) { | ||
var classname = new BinarySum(); | ||
System.out.println(classname.addBinary("1010", "1011")); | ||
} | ||
|
||
public String addBinary(String a, String b) { | ||
BigInteger x = new BigInteger(a, 2); | ||
BigInteger y = new BigInteger(b, 2); | ||
BigInteger sum = x.add(y); | ||
return sum.toString(2); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.math.BigInteger; | ||
|
||
public class FindSqrt { | ||
|
||
|
||
public static void main(String[] args) { | ||
|
||
|
||
} | ||
|
||
public int mySqrt(int x) { | ||
BigInteger c = BigInteger.valueOf(x); | ||
return c.sqrt().intValue(); | ||
} | ||
|
||
public int mySqrt2(int x) { | ||
if(x == 0){ | ||
return 0; | ||
} | ||
int start = 1; | ||
int end = x; | ||
int ans = 0; | ||
while(start <= end){ | ||
int mid = start + (end-start)/2; | ||
//instead of mid*mid we are giving x/mid to tackle overflow of integer range when multiplying with bigger numbers | ||
if(mid <= x/mid){ | ||
ans = mid; | ||
start = mid + 1; | ||
}else{ | ||
end = mid -1; | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters