-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
123 lines (116 loc) · 2.96 KB
/
Solution.java
File metadata and controls
123 lines (116 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package STRINGS.addbinary.attempt;
/**
* 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"
*
*
* Constraints:
*
* 1 <= a.length, b.length <= 104
* a and b consist only of '0' or '1' characters.
* Each string does not contain leading zeros except for the zero itself.
*/
class Solution {
/**
* Runtime
* 8 ms
* Beats
* 8.29%
* Memory
* 43.1 MB
* Beats
* 18.67%
* @param a
* @param b
* @return
*/
String addBinary(String a, String b) {
String[] num1 = a.split("");
String[] num2 = b.split("");
int first = num1.length-1, second = num2.length-1;
int carry = 0, sum = 0;
StringBuilder sb = new StringBuilder();
while(first >= 0 && second >= 0) {
// if both bits are 1
if(num1[first].equals("1") && num2[second].equals("1")) {
//int oldCarry = carry;
if(carry == 0) {
sum = 0;
carry = 1;
}
else {
sum = 1;
carry = 1;
}
sb.append(sum);
first--;
second--;
}
else {
sum = Integer.parseInt(num1[first]) + Integer.parseInt(num2[second]);
if(carry == 1) {
if(sum == 1) {
sum = 0;
carry = 1;
}
else {
sum = sum + carry;
carry = 0;
}
sb.append(sum);
first--;
second--;
}
else {
sb.append(sum);
first--;
second--;
}
}
}
while(first >= 0) {
if(num1[first].equals("1") && carry == 1) {
sum = 0;
carry = 1;
}
else {
sum = Integer.parseInt(num1[first]) + carry;
carry = 0;
}
sb.append(sum);
first--;
}
while(second >= 0) {
if(num2[second].equals("1") && carry == 1) {
sum = 0;
carry = 1;
}
else {
sum = Integer.parseInt(num2[second]) + carry;
carry = 0;
}
sb.append(sum);
second--;
}
if(carry == 1) {
sb.append(carry);
carry = 0;
}
return sb.reverse().toString();
}
public static void main(String[] args) {
String a = "1";
String b = "111";
System.out.println(new Solution().addBinary(a, b));
}
}