题号:43 标题:Multiply Strings 难度:Medium
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
我的程序码
class Solution {
public String multiply(String num1, String num2) {
String temp2 = new String();
String temp3 = new String();
String result = new String();
int a,b,c=0,len1,len2,i,j,x=0;
len1 = num1.length();
len2 = num2.length();
ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
if(num1.equals("0")||num2.equals("0")){
String temp4 = new String();
temp4 = temp4+'0';
return temp4;
}
for(i=len1-1;i>=0;i--){
String temp = new String();
a = num1.charAt(i)-48;
c=0;
for(j=len2-1;j>=0;j--){
b = num2.charAt(j)-48;
//System.out.println("a,b:" +a +","+ b);
c = a*b + c;
//System.out.println("c:" +c);
if(c > 9){
temp = temp + Integer.toString(c%10);
c = c/10;
}else{
temp = temp + Integer.toString(c);
c = 0;
}
}
if(c>0){
temp = temp + Integer.toString(c);
}
for(j=i+1;j<len1;j++){
temp = '0' + temp;
}
System.out.println(temp);
temp3 = "";
x = 0;
if(i!=len1-1){
System.out.println(temp + ":" + temp2);
for(j=0;j<temp.length();j++){
if(temp2.length()>j){
x = (temp.charAt(j)-48) + (temp2.charAt(j)-48)+x;
}else{
x= (temp.charAt(j)-48)+x;
}
if(x>9){
temp3 = temp3 + Integer.toString(x%10);
x = x/10;
}else{
temp3 = temp3 + Integer.toString(x);
x = 0;
}
}
if(x>0){
temp3 = temp3 + Integer.toString(x);
}
//System.out.println("temp3" + ":" + temp3);
temp2 = "";
temp2 = temp3;
}else{
temp2 = temp;
}
}
for(i=temp2.length()-1;i>=0;i--){
result = result + temp2.charAt(i);
}
return result;
}
}
花比较久的时间
不能用BigInteger library,然後长度会超过long的长度,所以查了一下这种问题怎麽办,我这题是用乘法直试逻辑写得,ex三位数abc乘以二位数de,abc x de=e x abc + adc x d x 10
但因为有字串转数字数字转字串,所以超级麻烦,不过很有趣就是了,我写得很开心
DAY23心得
今天心情不错呢
<<: [Day24]程序菜鸟自学C++资料结构演算法 – 选择排序法(Selection Sort)和谢尔排序法(Shell Sort)
今天重开一了个新的 Vue CLI 专案 因为这次想要练习将 component 引入主 App.v...
昨天加班加的凶, 到现在为止都还在挑灯夜战, 不过,在不景气的日子人就要想着变通, 才会有机会的到来...
来加一个游戏状态好了 本来是要弄回合时间限制 但发现怎样算开始呢? 集满两胜好像也还没有说谁获胜了。...
所谓参数,是在ML里面最关键也最麻烦的部分,俗话说garbage in garbage out,输入...
04 - Create PR with default template 在 Github 多人开发...