FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Algorithm_LeetCode/src/string_handle/AddBinary_67.java at master · minuk8932/Algorithm_LeetCode · GitHub
minuk8932
/
Algorithm_LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Algorithm_LeetCode
/
src
/
string_handle
/
AddBinary_67.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (32 loc) · 1.21 KB
Breadcrumbs
Algorithm_LeetCode
/
src
/
string_handle
/
AddBinary_67.java
Copy path
File metadata and controls
39 lines (32 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
string_handle
;
/**
*
* @author minchoba
* LeetCode 67번 : Add Binary
*
* @see https://leetcode.com/problems/add-binary/description/
*
*/
public
class
AddBinary_67
{
public
String
addBinary
(
String
a
,
String
b
) {
StringBuilder
sb
=
new
StringBuilder
();
int
aLen
=
a
.
length
() -
1
;
int
bLen
=
b
.
length
() -
1
;
int
tmp
=
0
;
while
(
aLen
>=
0
||
bLen
>=
0
) {
// 각 길이가 0이 아닐 때
int
sum
=
tmp
;
// sum에 tmp를 담고
if
(
bLen
>=
0
){
// a, b의 값을 정수화하여 뒤에서부터 거꾸로 담아주며 더하고, 각 길이를 줄여나감
sum
+=
b
.
charAt
(
bLen
--) -
'0'
;
}
if
(
aLen
>=
0
){
sum
+=
a
.
charAt
(
aLen
--) -
'0'
;
}
sb
.
append
(
sum
%
2
);
// sum의 나머지를 버퍼에 담고
tmp
=
sum
/
2
;
// 나눈 몫을 다시 tmp 담아서 반복문 지속 실행
}
if
(
tmp
!=
0
){
// 0이면 0이 아니면 계산 결과를 버퍼에 담음
sb
.
append
(
tmp
);
}
return
sb
.
reverse
().
toString
();
// 버퍼에 담은 값들을 역으로 반환
}
}
Back
|
FazBrowse Home
|
New Git URL