FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Leetcode-JavaScript/Add Strings.js at master · parshva45/Leetcode-JavaScript · GitHub
parshva45
/
Leetcode-JavaScript
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
Leetcode-JavaScript
/
Add Strings.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
31 lines (28 loc) · 903 Bytes
Breadcrumbs
Leetcode-JavaScript
/
Add Strings.js
Copy path
File metadata and controls
31 lines (28 loc) · 903 Bytes
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
/*Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.*/
/**
*
@param
{
string
} num1
*
@param
{
string
} num2
*
@return
{
string
}
*/
var
addStrings
=
function
(
num1
,
num2
)
{
b
=
num1
.
length
>=
num2
.
length
?
num1
:
num2
s
=
num1
.
length
<
num2
.
length
?
num1
:
num2
ans
=
""
carry
=
0
for
(
i
=
b
.
length
-
1
,
j
=
s
.
length
-
1
;
i
>=
0
;
i
--
,
j
--
)
{
if
(
s
[
j
]
!=
null
)
sum
=
Number
(
b
[
i
]
)
+
Number
(
s
[
j
]
)
+
carry
else
sum
=
Number
(
b
[
i
]
)
+
carry
ans
=
sum
%
10
+
ans
carry
=
parseInt
(
sum
/
10
)
}
if
(
carry
==
1
)
return
carry
+
ans
return
ans
}
;
Back
|
FazBrowse Home
|
New Git URL