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

Commit81d4199

Browse files
refactor 415
1 parentcff44e3 commit81d4199

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
packagecom.fishercoder.solutions;
22

33
/**
4+
* 415. Add Strings
5+
*
46
* Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.
57
68
Note:
79
8-
The length of both num1 and num2 is < 5100.
9-
Both num1 and num2 contains only digits 0-9.
10-
Both num1 and num2 does not contain any leading zero.
11-
You must not use any built-in BigInteger library or convert the inputs to integer directly.
10+
1.The length of both num1 and num2 is < 5100.
11+
2.Both num1 and num2 contains only digits 0-9.
12+
3.Both num1 and num2 does not contain any leading zero.
13+
4.You must not use any built-in BigInteger library or convert the inputs to integer directly.
1214
*/
1315
publicclass_415 {
1416

15-
publicstaticStringaddStrings(Stringnum1,Stringnum2) {
16-
if (num1 ==null ||num1.length() ==0) {
17-
returnnum2;
18-
}elseif (num2 ==null ||num2.length() ==0) {
19-
returnnum1;
20-
}
17+
publicstaticclassSolution1 {
18+
publicStringaddStrings(Stringnum1,Stringnum2) {
19+
if (num1 ==null ||num1.length() ==0) {
20+
returnnum2;
21+
}elseif (num2 ==null ||num2.length() ==0) {
22+
returnnum1;
23+
}
2124

22-
inti =num1.length() -1;
23-
intj =num2.length() -1;
24-
longcarry =0;
25-
longsum =0;
26-
StringBuildersb =newStringBuilder();
27-
char[]char1 =num1.toCharArray();
28-
char[]char2 =num2.toCharArray();
29-
while (i >=0 ||j >=0) {
30-
sum =carry;
31-
if (i >=0) {
32-
sum +=Character.getNumericValue(char1[i--]);
25+
inti =num1.length() -1;
26+
intj =num2.length() -1;
27+
longcarry =0;
28+
longsum =0;
29+
StringBuildersb =newStringBuilder();
30+
char[]char1 =num1.toCharArray();
31+
char[]char2 =num2.toCharArray();
32+
while (i >=0 ||j >=0) {
33+
sum =carry;
34+
if (i >=0) {
35+
sum +=Character.getNumericValue(char1[i--]);
36+
}
37+
if (j >=0) {
38+
sum +=Character.getNumericValue(char2[j--]);
39+
}
40+
carry =sum /10;
41+
sb.append(sum %10);
3342
}
34-
if (j >=0) {
35-
sum +=Character.getNumericValue(char2[j--]);
43+
if (carry !=0) {
44+
sb.append(carry);
3645
}
37-
carry =sum /10;
38-
sb.append(sum %10);
39-
}
40-
if (carry !=0) {
41-
sb.append(carry);
42-
}
4346

44-
returnsb.reverse().toString();
47+
returnsb.reverse().toString();
48+
}
4549
}
4650

4751
}

‎src/test/java/com/fishercoder/_415Test.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77

88
importstaticjunit.framework.Assert.assertEquals;
99

10-
/**
11-
* Created by fishercoder on 1/8/17.
12-
*/
1310
publicclass_415Test {
14-
privatestatic_415test;
11+
privatestatic_415.Solution1solution1;
1512
privatestaticStringexpected;
1613
privatestaticStringactual;
1714
privatestaticStringnum1;
1815
privatestaticStringnum2;
1916

2017
@BeforeClass
2118
publicstaticvoidsetup() {
22-
test =new_415();
19+
solution1 =new_415.Solution1();
2320
expected =newString();
2421
actual =newString();
2522
num1 =newString();
@@ -40,7 +37,7 @@ public void test1() {
4037
num1 ="123";
4138
num2 ="34567";
4239
expected ="34690";
43-
actual =test.addStrings(num1,num2);
40+
actual =solution1.addStrings(num1,num2);
4441
assertEquals(expected,actual);
4542

4643
}
@@ -51,7 +48,7 @@ public void test2() {
5148
num1 ="1";
5249
num2 ="9";
5350
expected ="10";
54-
actual =test.addStrings(num1,num2);
51+
actual =solution1.addStrings(num1,num2);
5552
assertEquals(expected,actual);
5653

5754
}
@@ -62,7 +59,7 @@ public void test3() {
6259
num1 ="9";
6360
num2 ="99";
6461
expected ="108";
65-
actual =test.addStrings(num1,num2);
62+
actual =solution1.addStrings(num1,num2);
6663
assertEquals(expected,actual);
6764

6865
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp