LeetCode OJ--Multiply Strings **
https://oj.leetcode.com/problems/multiply-strings/
用字符串实现大数乘法,细节题,细节很多
class Solution {
public:
string multiply(string num1, string num2) {
if(num1.empty() || num1.size() == || num2.empty() || num2.size() == )
return "";
//
if(num1 == "" || num2 == "")
return ""; //keep num2 the smaller
if(num1.size() < num2.size())
{
//exchange
string t = num1; num1 = num2; num2 = t;
}
string ans = "";
ans.resize(num1.size()); //initialize
for(size_t i = ; i < num1.size(); i++)
ans[i] = ''; int pos = ;
int times = ;
int sum = ; for(int index2 = num2.size() - ; index2 >= ; index2--)
{
pos = ans.size() - - times;
times++;
int carry = ;
for(int index1 = num1.size() - ; index1 >=; index1--)
{
sum = carry + (num1[index1] - '')*(num2[index2] - '') + ans[pos] - '';
carry = sum/;
ans[pos] = sum% + '';
pos--;
// ans has been the beginning while index1 not, and ans has been the beginning and here are carry
if(pos == - &&(carry > || index1 != ))
{
string t = "";
t[] = carry + '';
ans = t + ans;
carry = ;
pos = ;
}
}
}
return ans;
}
};
LeetCode OJ--Multiply Strings **的更多相关文章
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- leetcode:Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
随机推荐
- 常用自写函数[更新ing]
int gcd (int x, int y)//最大公约数 { return y == 0 ? x : gcd( y , x % y ); } int lcm(int x, int y)//最小公倍数 ...
- Admin站点
使用admin站点 a.在settings.py中设置语言和时区 LANGUAGE_CODE = 'zh-hans' # 使用中国语言 TIME_ZONE = 'Asia/Shanghai' # 使用 ...
- 简单聊聊四层/七层模型到HTTP
本文内容借鉴这篇博客:https://blog.csdn.net/agzhchren/article/details/79173491 但是这篇博客也是该博主转载的,文章具体出处我没有找到,如果有人知 ...
- Hive 分析函数lead、lag实例应用
Hive的分析函数又叫窗口函数,在oracle中就有这样的分析函数,主要用来做数据统计分析的. Lag和Lead分析函数可以在同一次查询中取出同一字段的前N行的数据(Lag)和后N行的数据(Lead) ...
- MySQL之索引(二)
高性能的索引策略 正确地创建和使用索引是实现高性能查询的基础.在MySQL之索引(一)这一章中我们介绍了各种类型的索引及其对应的优缺点.现在我们一起来看看如何真正地发挥这些索引的优势. 独立的列 我们 ...
- 极简配置phpstorm+xdebug进行断点调试
以前调试的时候各种var_dump()就能得到结果,现在入手别人开发的工作,由于不了解业务和代码逻辑,又要去修改bug,就造成了修改bug效率低,所以又拾起来了xdbug,顺便总结了一下phpstor ...
- Spring Boot 学习系列(03)—jar or war,做出你的选择
此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 两种打包方式 采用Spring Boot框架来构建项目,我们对项目的打包有两种方式可供选择,一种仍保持原有的 ...
- Leetcode 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
- Leetcode 493.翻转对
翻转对 给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对. 你需要返回给定数组中的重要翻转对的数量. 示例 ...
- Sina微博OAuth2框架解密
自从sina微博oauth2出来以后, 第三方集成开发简单了很多. Oauth2不像oauth1一样需要后台httpclient请求那么麻烦, 一切都可以在前台使用ajax实现了. 很多人觉得蹊跷, ...