Repository navigation
Expand file tree
/
Copy pathMultiplyStrings.java
More file actions
107 lines (95 loc) · 2.13 KB
/
Copy pathMultiplyStrings.java
File metadata and controls
107 lines (95 loc) · 2.13 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package leetcode;
public class MultiplyStrings {
public String multiply(String num1, String num2) {
if (num1.length() > num2.length()) {
String tmp = num1;
num1 = num2;
num2 = tmp;
}
// (num1.length <= num2.length) always true
String result = "";
return result;
}
public static void main(String[] args) {
System.out.println(new MultiplyStrings().multiply("0", "0"));
}
}
//public class MultiplyStrings {
//
// public String multiply(String num1, String num2) {
// if (num1.length() > num2.length()) {
// String tmp = num1;
// num1 = num2;
// num2 = tmp;
// }
// // (num1.length <= num2.length) always true
//
// num1 = new StringBuffer(num1).reverse().toString();
//
// String result = "";
//
// for(int i = 0; i < num1.length(); i++){
// String tmp = subMult(num2, num1.charAt(num1.length() - i - 1));
//
// result = plus(tmp, result+"0");
// }
//
// return result;
// }
//
// String subMult(String num1, char num2){
// String result = "0";
//
// for(int i = 0; i < num2-'0'; i++){
// result = plus(result, num1);
// }
//
// return result;
// }
//
// String plus(String num1, String num2) {
// StringBuilder sb = new StringBuilder();
//
// num1 = new StringBuffer(num1).reverse().toString();
// num2 = new StringBuffer(num2).reverse().toString();
//
// int carry = 0;
//
// if (num1.length() > num2.length()) {
// String tmp = num1;
// num1 = num2;
// num2 = tmp;
// }
// // (num1.length <= num2.length) always true
//
// for (int i = 0; i < num1.length(); i++) {
// int sum = (num1.charAt(i) - '0') + (num2.charAt(i) - '0') + carry;
//
// carry = sum / 10;
// int reminder = sum % 10;
// sb.insert(0, (char)(reminder + '0'));
// }
//
// for(int i = num1.length(); i < num2.length(); i++){
// int sum = num2.charAt(i) - '0' + carry;
//
// carry = sum / 10;
// int reminder = sum % 10;
// sb.insert(0, (char)(reminder + '0'));
// }
//
// if(carry == 1){
// sb.insert(0, 1);
// }
//
//
// return sb.toString();
// }
//
// public static void main(String[] args) {
// System.out.println(new MultiplyStrings().multiply("0", "0"));
//
//
// }
//
//}