Multiply Strings
来源:互联网 发布:淘宝总显示网络异常 编辑:程序博客网 时间:2024/06/16 13:38
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.
class Solution {public: string multiply(string num1, string num2) { string result; int m = num1.length(); int n = num2.length(); if (num1.compare("0") == 0 || num2.compare("0") == 0) { return "0"; } int offset = 0; for (int i = m-1; i >= 0; i--) { string curValue; int c = 0; for (int j = n-1; j >= 0; j--) { int temp = (num2[j]-'0') * (num1[i]-'0') + c; int val = temp % 10; c = temp / 10; curValue.append(1, val+'0'); } if (c > 0) { curValue.append(1, c+'0'); } if (result.length() == 0) { result = curValue; continue; } offset++; int len1 = result.length(); int len2 = curValue.length(); int p = offset; int q = 0; c = 0; while (p < len1 && q < len2) { int temp = (result[p]-'0') + (curValue[q]-'0') + c; int val = temp % 10; c = temp / 10; result[p] = val + '0'; p++; q++; } while (q < len2) { int temp = (curValue[q]-'0') + c; int val = temp % 10; c = temp / 10; result.append(1, val+'0'); q++; } if (c > 0) { result.append(1, c+'0'); } } int left = 0; int right = result.length()-1; while (left < right) { swap(result[left], result[right]); left++; right--; } return result; }};
0 0
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- Multiply Strings
- 千变万化的viewpager
- STL中deque,queue,stack,list的学习
- LayoutInflater的inflate函数用法详解
- 用HTML5/CSS3/JS开发Android/IOS应用
- java中的关键字
- Multiply Strings
- 程序员能力矩阵(Programmer Competency Matrix)
- UIKit框架-高级控件Swift版本: 5.UITextView方法/属性详解
- andorid性能优化《渲染、运算、内存和电量》
- 即时通讯:socket 使用
- css中的大于号的作用
- Java程序运行内容加载过程
- linux共享库位置配置(LD_LIBRARY_PATH环境变量 或者 更改/etc/ld.so.conf 或者 使用-R选项)
- Machine Learning---梯度下降