Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit63a956c

Browse files
author
zongyanqi
committed
add Easy_415_Add_Strings
1 parentfa2edc3 commit63a956c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎Easy_415_Add_Strings.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
3+
* Note:
4+
*
5+
* The length of both num1 and num2 is < 5100.
6+
* Both num1 and num2 contains only digits 0-9.
7+
* Both num1 and num2 does not contain any leading zero.
8+
* You must not use any built-in BigInteger library or convert the inputs to integer directly.
9+
*/
10+
11+
/**
12+
*@param {string} num1
13+
*@param {string} num2
14+
*@return {string}
15+
*/
16+
varaddStrings=function(num1,num2){
17+
varcarry=0;
18+
varlen1=num1.length;
19+
varlen2=num2.length;
20+
varforCount=Math.max(len1,len2);
21+
varnewNum='';
22+
for(vari=0;i<forCount;i++){
23+
vara=parseInt(num1[len1-1-i])||0;
24+
varb=parseInt(num2[len2-1-i])||0;
25+
varc=a+b+carry;
26+
carry=Math.floor(c/10);
27+
vare=c%10;
28+
newNum=e+newNum;
29+
}
30+
31+
if(carry){
32+
newNum=carry+newNum
33+
}
34+
returnnewNum;
35+
};
36+
37+
console.log(addStrings('12','123')=='135');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp