Multiply Strings leetcode
2016-01-11 21:17
459 查看
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.
Subscribe to see which companies asked this question
ACM竞赛常考的大数相乘算法,利用字符串来表示大数字进行计算。
Note: The numbers can be arbitrarily large and are non-negative.
Subscribe to see which companies asked this question
ACM竞赛常考的大数相乘算法,利用字符串来表示大数字进行计算。
string multiply(string num1, string num2) { string num(num1.size() + num2.size(), '0'); for (int i = num1.size() - 1; i >= 0; --i) { int carry = 0; for (int j = num2.size() - 1; j >= 0; --j) { int tmp = num[i + j + 1] - '0' + (num1[i] - '0') * (num2[j] - '0') + carry; num[i + j + 1] = tmp % 10 + '0'; carry = tmp / 10; } num[i] += carry; } size_t startpos = num.find_first_not_of('0'); if (startpos != string::npos) return num.substr(startpos); return "0"; }
相关文章推荐
- [译] --- OpenGL ES 2.0 for iPhone Tutorial Part 2: Textures
- 05.Django表单的使用
- 2016-01-11 工作笔记9
- bt5安装虚拟机工具报错unable to create symlink "etc/cups/ppd/VMware_Virtual_Printer.ppd" pointing to file "/u
- JAVA中的String类--String的最大长度
- C++中预处理(文件包含include)
- Android提供的LruCache类简介
- 前端工程师与SEO搜索引擎优化(笔记总结)
- 心电图动画效果
- socket网络编程----addrinfo结构体和getaddrinfo函数介绍
- 1
- 计算机基础知识
- TOast显示小火箭
- jquery 显示/ 隐藏 页面中部分内容
- MATLAB加快程序运行的方法:逻辑数组与向量化
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- 实现国际化的选项菜单
- Dagger2 生成代码学习
- jquery 显示/ 隐藏 页面中部分内容
- 大数取余/求商