43 Multiply Strings
43 Multiply Strings https://www.youtube.com/watch?v=q3vpdwWR0ag&t=1s https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation class Solution { public String multiply(String nums1, String nums2) { int n = nums1.length(); int m = nums2.length(); int[] result = new int[n + m]; for(int i = n -1; i >= 0; i--){ for(int j = m - 1; j >= 0; j--){ int mul = (nums1.charAt(i) - '0') * (nums2.charAt(j) - '0'); //Integer.valueOf(nums1.charAt(i)) * Integer.valueOf(nums2.charAt(j)); int p1 = i + j; int p2 = i + j + 1; int sum = mul + result[p2]; result[p2] = sum % 10; result[p1] += sum / 10; } } StringBuilder sb = new StringBuilder(); / for(int i = 0; i < result.length; i++){ if(sb.length() == 0 && result[i] == 0){ continue; }else{ sb.append(result[i]); } } if(sb.length() == 0){ return "0"; }else{ return sb.toString(); } } } 1. number in the form of a string for example String nums = "123" is a string represents the number 123 if we want to get a single number 2 from this string we can do (nums.charAt(1) - '0') = 2 2. when the int[] result = 00123 we want only 123 so we need to skip 00 how? okay , so when the stringbuilder.length() == 0 and the current number is 0 we continue 3. in the example 123 * 45 when 3 * 5 3's index is 2 (index 0 is 1) 5's index is 1 (index 0 is 4) so their position in the result is 2+1, 2+1+1 4. its easier with the graph 2 5 0 8 --- p1 p2 suppose we get 08 from 4 * 2 mul = 8 // int sum = mul + pos[p2]; 8 + 5 = 13 = sum // pos[p2] = (sum) % 10; 13 % 10 = 3 is for p2 position // pos[p1] += sum / 10; 13 / 10 + 2 = 1 + 2 = 3 is for p1 position
use int[] array , instead of use stringbuilder
Because we need to accumulate digits on the same index
And the result length is no bigger than n + m
the two for loops is from the last index to index 0
Because its the last index of a , * , the last index of b, the second last index of b, the third last index of b, .. so on until the index 0 of b,
And its the second last index of a , * , the last index of b, the second last index of b, the third last index of b, .. so on until the index 0 of b,
And the third last index of a, *, the last index of b, the second last index of b, the third last index of b, .. so on until the index 0 of b
.
.
.
The index 0 of a, *, the last index of b, the second last index of b, the third last index of b, .. so on until the index 0 of b,
if you multiply the index i of a, and the index j of b, then their product is in the new index of
I +j and I + j + 1.
Int sum = a[I] * b[j];
int p1 = i + j;
int p2 = i + j + 1;
So the order of p1 and p2 is like this: p1 p2
to add the sum into the result has a process like this:
sum = Sum + result[p2]
This is the new sum ,
And the digit for p2 is sum % 10
And the carry is sum / 10
And we need to add the carry into result[p1]
===
So the output should be a string, and now we have an int[] array
So we need to convert this int[] array into a string
One thing to keep in mind is that we might not be able to occupy all the spaces in the
Int array, so the unoccupied space is 0 by default
When converting the int[] array into the string, we skip the leading 0,
we know its leading 0 when result[I] == 0 and sb.length() == 0, (we haven’t append anything into the stringbuilder ) ,use continue to the next I
if the result is 0, 12 * 0 = 0, our multiplication for 0 works , then when we append the result int[] array into the stringbuilder, we append nothing , and the stringbuilder .length== 0, then return 0, else return stringbuilder
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
num1andnum2is < 110. - Both
num1andnum2contain only digits0-9. - Both
num1andnum2do 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.
posted on 2018-08-09 18:55 猪猪🐷 阅读(104) 评论(0) 收藏 举报
浙公网安备 33010602011771号