43. 字串相乘
我的思路是用num1的每一位去乘num2,產生num1長度個字串,再把k個字串相加。
classsolution
int o = 0
;
int pro = 0
;
int n = 0
;
for(int j=l2-1;j>=0;j--)
if(o>0) temp+= (o+'0'
);
//reverse(temp.begin(),temp.end());
products.push_back(temp);
//cout
//把得到的字串相加
string result = ""
;
int presum = 0
;
int nowsum = 0
;
for(int i=0;i)
}nowsum = nowsum +presum;
//if(nowsum>0||i==0)
result += '
0' + nowsum%10
; presum = nowsum/10
;
//cout
while(presum>0
)
int i = result.size()-1
;
while(*(result.end()-1)=='
0'&&(result.end()-1)!=result.begin())result.erase(result.end()-1
);
//for()
reverse(result.begin(),result.end());
return
result;
}};/*
數字過長,無法用普通資料型別表示。
用num1的每一位去乘num2,產生x1個字串,再把k個字串相加。
官方題解的思路更優雅
43 字串相乘
給定兩個以字串形式表示的非負整數 num1 和 num2,返回 num1 和 num2 的乘積,它們的乘積也表示為字串形式。示例 1 輸入 num1 2 num2 3 輸出 6 示例 2 輸入 num1 123 num2 456 輸出 56088 說明 num1 和 num2 的長度小於110。nu...
43 字串相乘
題目描述 給定兩個以字串形式表示的非負整數 num1 和 num2,返回 num1 和 num2 的乘積,它們的乘積也表示為字串形式。示例 1 輸入 num1 2 num2 3 輸出 6 示例 2 輸入 num1 123 num2 456 輸出 56088 說明 num1 和 num2 的長度小於1...
43 字串相乘
小聲bb,直接int 多好 既然題目要求不能直接用int 我只好開動我的大腦開始想有什麼解決方案。然後首先想到的就是小學時候老師教乘法時用乘式寫法。1 2 3 4 5 6 1 5 4 9 2 5 5 3 51 2 3 4 5 1 51 0 0 5 0 6 1 5 1 20 8 0 4 0 5 5 3...