File tree 4 files changed +161
-0
lines changed
solution/0000-0099/0067.Add Binary
4 files changed +161
-0
lines changed Original file line number Diff line number Diff line change @@ -160,6 +160,63 @@ func addBinary(a string, b string) string {
160
160
}
161
161
```
162
162
163
+ ### ** TypeScript**
164
+
165
+ ``` ts
166
+ function addBinary(a : string , b : string ): string {
167
+ const res = [];
168
+ let i = a .length - 1 ;
169
+ let j = b .length - 1 ;
170
+ let isOver = false ;
171
+ while (i >= 0 || j >= 0 || isOver ) {
172
+ let sum = Number (a [i ] || 0 ) + Number (b [j ] || 0 ) + (isOver ? 1 : 0 );
173
+ isOver = false ;
174
+ if (sum > 1 ) {
175
+ isOver = true ;
176
+ sum -= 2 ;
177
+ }
178
+ res .push (sum );
179
+ i -- ;
180
+ j -- ;
181
+ }
182
+ return res .reverse ().join (' ' );
183
+ }
184
+ ```
185
+
186
+ ### ** Rust**
187
+
188
+ ``` rust
189
+ impl Solution {
190
+ pub fn add_binary (a : String , b : String ) -> String {
191
+ let mut res = String :: new ();
192
+ let a = a . as_bytes ();
193
+ let b = b . as_bytes ();
194
+ let mut i = a . len ();
195
+ let mut j = b . len ();
196
+ let mut is_over = false ;
197
+ while i != 0 || j != 0 || is_over {
198
+ let mut sum = if is_over { b '1' } else { b '0' };
199
+ if i != 0 {
200
+ sum += a [i - 1 ] - b '0' ;
201
+ i -= 1 ;
202
+ }
203
+ if j != 0 {
204
+ sum += b [j - 1 ] - b '0' ;
205
+ j -= 1 ;
206
+ }
207
+ is_over = if sum > b '1' {
208
+ sum -= b '2' - b '0' ;
209
+ true
210
+ } else {
211
+ false
212
+ };
213
+ res . push (char :: from (sum ));
214
+ }
215
+ res . chars (). rev (). collect ()
216
+ }
217
+ }
218
+ ```
219
+
163
220
### ** ...**
164
221
165
222
```
Original file line number Diff line number Diff line change @@ -145,6 +145,63 @@ func addBinary(a string, b string) string {
145
145
}
146
146
```
147
147
148
+ ### ** TypeScript**
149
+
150
+ ``` ts
151
+ function addBinary(a : string , b : string ): string {
152
+ const res = [];
153
+ let i = a .length - 1 ;
154
+ let j = b .length - 1 ;
155
+ let isOver = false ;
156
+ while (i >= 0 || j >= 0 || isOver ) {
157
+ let sum = Number (a [i ] || 0 ) + Number (b [j ] || 0 ) + (isOver ? 1 : 0 );
158
+ isOver = false ;
159
+ if (sum > 1 ) {
160
+ isOver = true ;
161
+ sum -= 2 ;
162
+ }
163
+ res .push (sum );
164
+ i -- ;
165
+ j -- ;
166
+ }
167
+ return res .reverse ().join (' ' );
168
+ }
169
+ ```
170
+
171
+ ### ** Rust**
172
+
173
+ ``` rust
174
+ impl Solution {
175
+ pub fn add_binary (a : String , b : String ) -> String {
176
+ let mut res = String :: new ();
177
+ let a = a . as_bytes ();
178
+ let b = b . as_bytes ();
179
+ let mut i = a . len ();
180
+ let mut j = b . len ();
181
+ let mut is_over = false ;
182
+ while i != 0 || j != 0 || is_over {
183
+ let mut sum = if is_over { b '1' } else { b '0' };
184
+ if i != 0 {
185
+ sum += a [i - 1 ] - b '0' ;
186
+ i -= 1 ;
187
+ }
188
+ if j != 0 {
189
+ sum += b [j - 1 ] - b '0' ;
190
+ j -= 1 ;
191
+ }
192
+ is_over = if sum > b '1' {
193
+ sum -= b '2' - b '0' ;
194
+ true
195
+ } else {
196
+ false
197
+ };
198
+ res . push (char :: from (sum ));
199
+ }
200
+ res . chars (). rev (). collect ()
201
+ }
202
+ }
203
+ ```
204
+
148
205
### ** ...**
149
206
150
207
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn add_binary ( a : String , b : String ) -> String {
3
+ let mut res = String :: new ( ) ;
4
+ let a = a. as_bytes ( ) ;
5
+ let b = b. as_bytes ( ) ;
6
+ let mut i = a. len ( ) ;
7
+ let mut j = b. len ( ) ;
8
+ let mut is_over = false ;
9
+ while i != 0 || j != 0 || is_over {
10
+ let mut sum = if is_over { b'1' } else { b'0' } ;
11
+ if i != 0 {
12
+ sum += a[ i - 1 ] - b'0' ;
13
+ i -= 1 ;
14
+ }
15
+ if j != 0 {
16
+ sum += b[ j - 1 ] - b'0' ;
17
+ j -= 1 ;
18
+ }
19
+ is_over = if sum > b'1' {
20
+ sum -= b'2' - b'0' ;
21
+ true
22
+ } else {
23
+ false
24
+ } ;
25
+ res. push ( char:: from ( sum) ) ;
26
+ }
27
+ res. chars ( ) . rev ( ) . collect ( )
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ function addBinary ( a : string , b : string ) : string {
2
+ const res = [ ] ;
3
+ let i = a . length - 1 ;
4
+ let j = b . length - 1 ;
5
+ let isOver = false ;
6
+ while ( i >= 0 || j >= 0 || isOver ) {
7
+ let sum = Number ( a [ i ] || 0 ) + Number ( b [ j ] || 0 ) + ( isOver ? 1 : 0 ) ;
8
+ isOver = false ;
9
+ if ( sum > 1 ) {
10
+ isOver = true ;
11
+ sum -= 2 ;
12
+ }
13
+ res . push ( sum ) ;
14
+ i -- ;
15
+ j -- ;
16
+ }
17
+ return res . reverse ( ) . join ( '' ) ;
18
+ }
You can’t perform that action at this time.
0 commit comments