Given numbers represented as strings, return multiplication of the numbers as a string.
Note:the numbers can be arbitrarily large and is non-negative.
public class Solution {//Analog hand multiplication public String multiply (string num1, String num2) {int n = num2.length (); string[] AddS = new String[n]; for (int i = 0; i < n; i++) {Adds[i] = Multiplychar (NUM1, Num2.charat (i), n-1-i); The String res = SUM (AddS); int p = 0; while (P < res.length () && Res.charat (p) = = ' 0 ') p++; if (p = = Res.length ()) return "0"; else return res.substring (p); } public string Multiplychar (string num, char c, int digits) {int n = num.length (); char[] Res = new Char[n + 1]; int add = 0; for (int i = n; I >= 0; i--) {int b = 0; if (i-1 >= 0) b = Num.charat (i-1)-' 0 '; int cur = b* (c ' 0 ') +add; add = CUR/10; Cur%= 10; Res[i] = (char) (cur+ ' 0 '); } int p = 0; while (P <= n && res[p] = = ' 0 ') p++; if (p = = n + 1) return "0"; else{stringbuffer sb = new StringBuffer (); for (int i = 0; i < digits; i++) {sb.append (' 0 '); } return new String (res, p, n-p+1) + sb.tostring (); }} public String sum (string[] s) {StringBuffer res= new StringBuffer (); int p = 0; int cur = 0; int add = 0; int maxLength = 0; for (int i = 0; i < s.length; i++) {maxLength = Math.max (MaxLength, S[i].length ()); } do{cur = 0; for (int i = 0; i < s.length; i++) {if (P < s[i].length ()) cur + = S[i].charat (s[i].l Ength () -1-p)-' 0 '; } cur + = add; Res.append (CUR%10); add = CUR/10; p++; } while (cur! = 0 | | p < maxLength); return new String (Res.reverse ()); }}
Leetcode:multiply Strings. Java