Leetcode Multiply Strings
2014-06-06 19:37
246 查看
题目链接:https://oj.leetcode.com/problems/multiply-strings/
Total Accepted: 8084 Total
Submissions: 40447My Submissions
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
Have you been asked this question in an interview?
模拟大整数乘法,用经典的算法即可解决问题。
Multiply Strings
Total Accepted: 8084 TotalSubmissions: 40447My Submissions
Given two numbers represented as strings, return multiplication of the numbers as a string.
Note: The numbers can be arbitrarily large and are non-negative.
Have you been asked this question in an interview?
模拟大整数乘法,用经典的算法即可解决问题。
class Solution { public: string multiply(string num1, string num2) { if(num1=="0"||num2=="0") return "0"; int l1=num1.size(),l2=num2.size(); int *n1=new int [l1],*n2=new int [l2],*res=new int [l1+l2]; memset(res,0,sizeof(int)*(l1+l2)); for(int i=0;i<l1;i++) n1[i]=num1[i]-'0'; for(int i=0;i<l2;i++) n2[i]=num2[i]-'0'; for(int i=0;i<l1;i++) for(int j=0;j<l2;j++) res[i+j+1]+=n1[i]*n2[j]; string ans=""; for(int k=l1+l2-1;k>=0;k--){ if(k>0) res[k-1]+=res[k]/10; res[k]%=10; ans=char(res[k]+'0')+ans; } ans=ans[0]=='0'?ans.substr(1):ans; delete []n1; delete []n2; delete []res; return ans; } };
相关文章推荐
- Leetcode: Multiply Strings
- [leetcode] Multiply Strings
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
- 【leetcode】Multiply Strings
- LeetCode: Multiply Strings [042]
- LeetCode Multiply Strings
- Leetcode43 Multiply Strings
- LeetCode Multiply Strings
- [LeetCode]Multiply Strings
- [Leetcode]Multiply Strings
- leetcode--Multiply Strings
- [LeetCode] Multiply Strings
- LeetCode Multiply Strings
- LeetCode Multiply Strings
- LeetCode -- Multiply Strings
- [Leetcode] #43 Multiply Strings
- 【LeetCode】Multiply Strings
- 52 leetcode - Multiply Strings
- [Leetcode 90] 43 Multiply Strings
- [LeetCode] 大数问题,相加和相乘,题 Multiply Strings