leetcode - Multiply Strings
2014-10-24 23:26
429 查看
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.
Note: The numbers can be arbitrarily large and are non-negative.
class Solution { public: std::string multiply(std::string num1, std::string num2) { if(num1 == "0" || num2 == "0") return "0"; int l1 = num1.size(),l2 = num2.size(); std::vector<int> vec1(l1,0); std::vector<int> vec2(l2,0); std::vector<int> res(l1+l2,0); for (int i = 0; i < l1; i++) { vec1[i] = num1[i] - '0'; } for (int i = 0; i < l2; i++) { vec2[i] = num2[i] - '0'; } for (int i = 0; i < l1; i++) { for (int j = 0; j < l2; j++) { res[i+j+1] += vec1[i] * vec2[j]; } } std::string s; for(int i = l1 + l2 - 1; i >= 0; i--) { if(res[i] > 0) res[i-1] += res[i] / 10; res[i] %= 10; s = (char)(res[i] + '0') + s; } return s[0] == '0' ? s.substr(1) : s; } };
相关文章推荐
- LeetCode之Multiply Strings
- Multiply Strings[LeetCode]
- LeetCode - 43. Multiply Strings
- 【LeetCode】Multiply Strings
- 【leetcode】Multiply Strings
- Leetcode: Multiply Strings
- LeetCode 43 Multiply Strings
- [leetcode 43] Multiply Strings
- LeetCode--Multiply Strings 字符串相乘
- LeetCode_OJ【43】Multiply Strings
- [leetcode] Multiply Strings
- 【Leetcode】之Multiply Strings
- leetcode Multiply Strings 字符串的乘积问题
- Multiply Strings leetcode java
- leetcode:Multiply Strings
- leetcode -- Multiply Strings -- python容易处理,需要看其他解法
- LeetCode 43. Multiply Strings
- Leetcode127: Multiply Strings
- LeetCode Online Judge 题目C# 练习 - Multiply Strings
- Multiply Strings ---leetcode