Skip to content

Commit 9f30bbd

Browse files
committed
leetcode 6일차 완료
1 parent 5b14da6 commit 9f30bbd

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/leetcode/easy/add_binary.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode.easy;
2+
3+
/**
4+
* https://leetcode.com/problems/add-binary/submissions/
5+
*/
6+
public class add_binary {
7+
public static void main(String[] args) {
8+
String a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101";
9+
String b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011";
10+
System.out.println(addBinary(a, b));
11+
}
12+
13+
static public String addBinary(String a, String b) {
14+
StringBuilder sb = new StringBuilder();
15+
int i = a.length() - 1, j = b.length() - 1, carry = 0;
16+
int sum = 0;
17+
while (i >= 0 || j >= 0) {
18+
sum = carry;
19+
if (i >= 0) sum += a.charAt(i--) + '0';
20+
if (j >= 0) sum += b.charAt(j--) + '0';
21+
sb.append(sum % 2);
22+
carry = sum / 2;
23+
}
24+
if (carry != 0) sb.append(carry);
25+
return sb.reverse().toString();
26+
}
27+
}

src/leetcode/easy/sqrtx.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode.easy;
2+
3+
public class sqrtx {
4+
public static void main(String[] args) {
5+
System.out.println(mySqrt(16));
6+
}
7+
8+
static public int mySqrt(int x) {
9+
long r = x;
10+
while (r * r > x)
11+
r = (r + x / r) / 2;
12+
return (int) r;
13+
}
14+
}

0 commit comments

Comments
 (0)