https://leetcode.com/problems/multiply-strings/
https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation
https://blog.csdn.net/mine_song/article/details/70482898
X. https://blog.csdn.net/NK_test/article/details/49623191
string multiply(string num1, string num2) {
string s(1000,'0');
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
for(int i=0;i<num1.length();i++)
for(int j=0;j<num2.length();j++)
{
int temp=(num1[i]-'0')*(num2[j]-'0');
s[i+j+1]=s[i+j+1]-'0'+(s[i+j]-'0'+temp)/10+'0';
s[i+j]=(s[i+j]-'0'+temp)%10+'0';
}
reverse(s.begin(),s.end());
if(s.find_first_not_of('0')==string::npos)
return "0";
else
return s.substr(s.find_first_not_of('0'));
}
https://leetcode.com/problems/multiply-strings/discuss/17667/Java-reverse-stringbuilder
第二轮做internal search的白人小哥哥,让输出一个数的factorial结果,其实就是批了一层皮的大卫数乘法在string上操作,很久以前做过但是写的略慢,就写了这一题。
Given two non-negative integers
num1
and num2
represented as strings, return the product of num1
and num2
, also represented as a string.
Example 1:
Input: num1 = "2", num2 = "3" Output: "6"
Example 2:
Input: num1 = "123", num2 = "456" Output: "56088"
Note:
- The length of both
num1
andnum2
is < 110. - Both
num1
andnum2
contain only digits0-9
. - Both
num1
andnum2
do not contain any leading zero, except the number 0 itself. - You must not use any built-in BigInteger library or convert the inputs to integer directly
https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation
int p1 = i + j, p2 = i + j + 1;
int sum = mul + pos[p2];
We only need
Remember how we do multiplication?p1
and p2
here because 9 * 9 + 9 < 100
https://blog.csdn.net/mine_song/article/details/70482898
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0"))
return "0";
int n1 = num1.length();
int n2 = num2.length();
int[] products = new int[n1 + n2];
for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
products[i + j + 1] += ((int) num1.charAt(i) - '0') * ((int) num2.charAt(j) - '0');
}
}
int carry = 0;
StringBuilder sb = new StringBuilder();
for (int i = n1 + n2 - 1; i >= 0; i--) {
int tmp = products[i] + carry;
sb.append(tmp % 10);
carry = tmp / 10;
}
sb.reverse();
if (sb.charAt(0) == '0')
sb.deleteCharAt(0);
return sb.toString();
}
https://leetcode.com/problems/multiply-strings/discuss/17608/AC-solution-in-Java-with-explanation
If we break it into steps, it will have the following steps. 1. compute products from each pair of digits from num1 and num2. 2. carry each element over. 3. output the solution.
Things to note:
- The product of two numbers cannot exceed the sum of the two lengths. (e.g. 99 * 99 cannot be five digit)
int d1 = num1.charAt(i) - '0';
int d2 = num2.charAt(j) - '0';
products[i + j + 1] += d1 * d2;
public String multiply(String num1, String num2) {
int n1 = num1.length(), n2 = num2.length();
int[] products = new int[n1 + n2];
for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
int d1 = num1.charAt(i) - '0';
int d2 = num2.charAt(j) - '0';
products[i + j + 1] += d1 * d2;
}
}
int carry = 0;
for (int i = products.length - 1; i >= 0; i--) {
int tmp = (products[i] + carry) % 10;
carry = (products[i] + carry) / 10;
products[i] = tmp;
}
StringBuilder sb = new StringBuilder();
for (int num : products) sb.append(num);
while (sb.length() != 0 && sb.charAt(0) == '0') sb.deleteCharAt(0);
return sb.length() == 0 ? "0" : sb.toString();
}
string multiply(string num1, string num2) {
string s(1000,'0');
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
for(int i=0;i<num1.length();i++)
for(int j=0;j<num2.length();j++)
{
int temp=(num1[i]-'0')*(num2[j]-'0');
s[i+j+1]=s[i+j+1]-'0'+(s[i+j]-'0'+temp)/10+'0';
s[i+j]=(s[i+j]-'0'+temp)%10+'0';
}
reverse(s.begin(),s.end());
if(s.find_first_not_of('0')==string::npos)
return "0";
else
return s.substr(s.find_first_not_of('0'));
}
https://leetcode.com/problems/multiply-strings/discuss/17667/Java-reverse-stringbuilder
public String multiply(String left, String right) {
if(left.equals("0") || right.equals("0"))
return "0";
// use p to accumulate sum of prod.
int[] p = new int[left.length() + right.length()] ;
left = new StringBuilder(left).reverse().toString();
right = new StringBuilder(right).reverse().toString();
for(int i =0; i < left.length(); i ++){
for(int j =0; j < right.length(); j ++){
int val = (left.charAt(i) -'0') * (right.charAt(j) -'0');
p[i+j] += val;
}
}
// convet p into string
int carry =0;
StringBuilder sb = new StringBuilder();
for(int v: p){
v += carry;
carry = v/10;
v %= 10;
sb.append(v);
}
String ret = sb.reverse().toString();
return ret.charAt(0) == '0' ? ret.substring(1) : ret;
}
https://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=451849第二轮做internal search的白人小哥哥,让输出一个数的factorial结果,其实就是批了一层皮的大卫数乘法在string上操作,很久以前做过但是写的略慢,就写了这一题。