# leetcode415_字符串运算

415. 字符串相加

（1）注意对其最低项
（2）注意进位

 public String addStrings(String num1, String num2) {
int i = num1.length() - 1;
int j = num2.length() - 1;
StringBuilder sb = new StringBuilder();
while(i >= 0 || j >= 0 || add != 0) {
int x = i >= 0? num1.charAt(i) - '0' : 0;
int y = j >= 0? num2.charAt(j) - '0' : 0;
int sum = x + y + add;
i --;
j --;
sb.append(sum % 10);
}
return sb.reverse().toString();
}


43.字符串相乘

 public String multiply(String num1, String num2) {
// 考虑这一种极端的情况
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
List<String> list = new ArrayList<>();
String les;
String lon;
for(int i = num2.length() - 1; i >= 0; i --) {
String result = mul(num1,"" + num2.charAt(i));
StringBuilder sb = new StringBuilder();
for (int k = 0; k < list.size(); k ++) {
sb.append(0);
}
result += sb.toString();
}
String result = list.get(0);
for (int i = 1; i < list.size(); i ++){
}
return result;
}

public String mul (String num1, String num) {
int i = num1.length() - 1;
StringBuilder sb = new StringBuilder();
int y = num.charAt(0) - '0';
while(i >= 0 || add != 0) {
int x = i >= 0? num1.charAt(i) - '0' : 0;
int sum = x * y + add;
sb.append(sum % 10);
i --;
}
return sb.reverse().toString();
}

public String add (String num1, String num2) {
int i = num1.length() - 1;
int j = num2.length() - 1;
StringBuilder sb = new StringBuilder();
while(i >= 0 || j >= 0 || add != 0) {
int x = i >= 0? num1.charAt(i) - '0' : 0;
int y = j >=0? num2.charAt(j) - '0' : 0;
int sum = x + y + add;
sb.append(sum % 10);
i --;
j --;
}
return sb.reverse().toString();
}


    public String multiply(String num1, String num2) {
// 考虑这一种极端的情况
StringBuilder sb = new StringBuilder();
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
int l1 = num1.length() - 1;
int l2 = num2.length() - 1;
int [] ans = new int [l1 + l2 + 2];
for (int i = l2; i >= 0; i --) {
int start = num2.length() - i - 1;
String result = mul(num1,"" + num2.charAt(i));
for (int j = result.length() - 1; j >= 0; j --) {
ans[start] += (result.charAt(j) - '0');
start ++;
}
}
for (int i = 1; i < ans.length; i ++) {
ans[i] += (ans[i - 1] / 10);
ans[i - 1] = ans[i - 1] % 10;
}
// 去0
boolean flag = true;
for (int i = ans.length - 1; i >= 0; i --) {
if (flag && ans[i] == 0){continue;}
else if (flag) {
flag = false;
}
sb.append(ans[i]);
}
return sb.toString();
}

public String mul (String num1, String num) {
int i = num1.length() - 1;
StringBuilder sb = new StringBuilder();
int y = num.charAt(0) - '0';
while(i >= 0 || add != 0) {
int x = i >= 0? num1.charAt(i) - '0' : 0;
int sum = x * y + add;
sb.append(sum % 10);
i --;
}
return sb.reverse().toString();
}


if (num1.equals("0") || num2.equals("0")) {
return "0";
}
int m = num1.length(), n = num2.length();
int[] ansArr = new int[m + n];
for (int i = m - 1; i >= 0; i--) {
int x = num1.charAt(i) - '0';
for (int j = n - 1; j >= 0; j--) {
int y = num2.charAt(j) - '0';
ansArr[(m - i - 1) + (n -j - 1)] += x * y;
}
}
// 进位操作
for (int i = 1; i < ansArr.length; i ++){
ansArr[i] += ansArr[i - 1] / 10;
ansArr[i - 1] = ansArr[i - 1] % 10;
}
boolean flag = true;
StringBuilder sb = new StringBuilder();
// 避免前导0
for (int i = m + n - 1; i >= 0; i --) {
if (ansArr[i] == 0 && flag) {continue;}
else if (flag) {
flag = false;
}
sb.append(ansArr[i]);
}
return sb.toString();
}

posted @ 2022-08-18 19:10  Kyara  阅读(17)  评论(0编辑  收藏  举报