File tree 2 files changed +162
-0
lines changed
competitive programming/leetcode
2 files changed +162
-0
lines changed Original file line number Diff line number Diff line change
1
+ Given two binary strings, return their sum (also a binary string).
2
+
3
+ The input strings are both non-empty and contains only characters 1 or 0.
4
+
5
+ Example 1:
6
+
7
+ Input: a = "11", b = "1"
8
+ Output: "100"
9
+ Example 2:
10
+
11
+ Input: a = "1010", b = "1011"
12
+ Output: "10101"
13
+
14
+
15
+ Constraints:
16
+
17
+ Each string consists only of '0' or '1' characters.
18
+ 1 <= a.length, b.length <= 10^4
19
+ Each string is either "0" or doesn't contain any leading zero.
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+ class Solution {
28
+ public:
29
+ string addBinary (string a, string b) {
30
+ int carry=0 ;
31
+ int alen=a.length (), blen=b.length ();
32
+ string res=" " ;
33
+ while (alen && blen){
34
+ int tmp=a[alen-1 ]-' 0' +b[blen-1 ]-' 0' +carry;
35
+
36
+ if (tmp==3 ){
37
+ carry=1 ;
38
+ tmp=1 ;
39
+ } else if (tmp==2 ){
40
+ carry=1 ;
41
+ tmp=0 ;
42
+ }
43
+ else carry=0 ;
44
+
45
+ res.push_back (tmp+' 0' );
46
+ alen--;
47
+ blen--;
48
+ }
49
+ while (alen){
50
+ int tmp=a[alen-1 ]-' 0' +carry;
51
+ if (tmp==3 ){
52
+ carry=1 ;
53
+ tmp=1 ;
54
+ } else if (tmp==2 ){
55
+ carry=1 ;
56
+ tmp=0 ;
57
+ }
58
+ else carry=0 ;
59
+ res.push_back (tmp+' 0' );
60
+ alen--;
61
+ }
62
+ while (blen){
63
+ int tmp=b[blen-1 ]-' 0' +carry;
64
+ if (tmp==3 ){
65
+ carry=1 ;
66
+ tmp=1 ;
67
+ } else if (tmp==2 ){
68
+ carry=1 ;
69
+ tmp=0 ;
70
+ }
71
+ else carry=0 ;
72
+ res.push_back (tmp+' 0' );
73
+ blen--;
74
+ }
75
+
76
+ if (carry) res.push_back (' 1' );
77
+
78
+ reverse (res.begin (), res.end ());
79
+ return res;
80
+ }
81
+ };
Original file line number Diff line number Diff line change
1
+ Given two binary strings, return their sum (also a binary string).
2
+
3
+ The input strings are both non-empty and contains only characters 1 or 0.
4
+
5
+ Example 1:
6
+
7
+ Input: a = "11", b = "1"
8
+ Output: "100"
9
+ Example 2:
10
+
11
+ Input: a = "1010", b = "1011"
12
+ Output: "10101"
13
+
14
+
15
+ Constraints:
16
+
17
+ Each string consists only of '0' or '1' characters.
18
+ 1 <= a.length, b.length <= 10^4
19
+ Each string is either "0" or doesn't contain any leading zero.
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+ class Solution {
28
+ public:
29
+ string addBinary (string a, string b) {
30
+ int carry=0 ;
31
+ int alen=a.length (), blen=b.length ();
32
+ string res=" " ;
33
+ while (alen && blen){
34
+ int tmp=a[alen-1 ]-' 0' +b[blen-1 ]-' 0' +carry;
35
+
36
+ if (tmp==3 ){
37
+ carry=1 ;
38
+ tmp=1 ;
39
+ } else if (tmp==2 ){
40
+ carry=1 ;
41
+ tmp=0 ;
42
+ }
43
+ else carry=0 ;
44
+
45
+ res.push_back (tmp+' 0' );
46
+ alen--;
47
+ blen--;
48
+ }
49
+ while (alen){
50
+ int tmp=a[alen-1 ]-' 0' +carry;
51
+ if (tmp==3 ){
52
+ carry=1 ;
53
+ tmp=1 ;
54
+ } else if (tmp==2 ){
55
+ carry=1 ;
56
+ tmp=0 ;
57
+ }
58
+ else carry=0 ;
59
+ res.push_back (tmp+' 0' );
60
+ alen--;
61
+ }
62
+ while (blen){
63
+ int tmp=b[blen-1 ]-' 0' +carry;
64
+ if (tmp==3 ){
65
+ carry=1 ;
66
+ tmp=1 ;
67
+ } else if (tmp==2 ){
68
+ carry=1 ;
69
+ tmp=0 ;
70
+ }
71
+ else carry=0 ;
72
+ res.push_back (tmp+' 0' );
73
+ blen--;
74
+ }
75
+
76
+ if (carry) res.push_back (' 1' );
77
+
78
+ reverse (res.begin (), res.end ());
79
+ return res;
80
+ }
81
+ };
You can’t perform that action at this time.
0 commit comments