1️⃣.字符串相乘
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
class Solution {
//模拟-字符串
public String multiply(String s1, String s2) {
if(s1.equals("0")||s2.equals("0")) return "0";
int len1 = s1.length();
int len2 = s2.length();
int[] temp = new int[len1+len2];
for( int i=len1-1;i>=0;--i){
for(int j=len2-1;j>=0;--j){
int n1 = s1.charAt(i)-'0';
int n2 = s2.charAt(j)-'0';
int sum = n1*n2+temp[i+j+1];
int div = sum/10;
int mod = sum%10;
temp[i+j+1]=mod;
temp[i+j]+=div;
}
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<temp.length;++i){
if(i==0&&temp[i]==0) continue;
sb.append(temp[i]);
}
return sb.toString();
}
}
2️⃣.字符串相加(十进制)
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
class Solution {
//模拟-字符串
public String addStrings(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int i=len1-1;
int j=len2-1;
int add=0;
StringBuffer sb = new StringBuffer();
while(i>=0||j>=0||add!=0){
int n1 =i>=0?s1.charAt(i)-'0':0;
int n2 =j>=0?s2.charAt(j)-'0':0;
int sum = n1+n2+add;
int div = sum/10;
int mod = sum%10;
add = div;
sb.append(mod);
--i;
--j;
}
return sb.reverse().toString();
}
}
3️⃣.字符串相加(三十六进制)
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
class Solution {
//模拟-字符串
public String add36Strings(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int i=len1-1;
int j=len2-1;
int add=0;
StringBuffer sb = new StringBuffer();
while(i>=0||j>=0||add!=0){
int n1 =i>=0?getInt(s1.charAt(i)):0;
int n2 =j>=0?getInt(s2.charAt(j)):0;
int sum = n1+n2+add;
int div = sum/36;
int mod = sum%36;
add = div;
sb.append(getChar(mod));
--i;
--j;
}
return sb.reverse().toString();
}
//字符 转化成 整数
public int getInt(char c){
if ('0' <= c && c <= '9'){//字符是 0-9 则正常 c-'0';
return c-'0';
}else{//字符是 a-z 则正常 c-'a'+10;
return c-'a'+10;
}
}
//整数 转化成 字符
public char getChar(int n){
if(n<=9){
return (char)(n+'0');
}else{
return (char)(n-10+'a');
}
}
}
总结提炼🎯
1