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 ...
随机推荐
- 容斥原理:HDU-4135Co-prime
容斥原理公式:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5). 求的是多个重合区间的里面的数字个数. 解题心得: 1.一 ...
- utf8和utf8mb4区别
原文链接 一.简介 MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在utf8mb4是utf8的超集,除了将编 ...
- 笔记-python-__new__()
笔记-python-__new__() 1. __new__() __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前. 验证代码: class Person(obj ...
- 等比例适配所有屏幕---css3 rem用法
1,rem的定义 rem(font size of the root element)是指相对于根元素的字体大小的单位.rem是一个相对单位.和em非常相似.em(font size of the e ...
- Understanding on 'Error to Origin (50x)' , 'Internal CDN Error (50x)' and 'External Error (50x)' in Chartron
Overview This document explains about definition of these values on OUI Chartron. Definition of Erro ...
- Leetcode 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
- Leetcode 552.学生出勤记录II
学生出勤记录II 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值. 学生出勤记录是只包含以下三个字符的字符串: ' ...
- mysql安装 以及跳过密码登录重设
修改MySQL的登录设置: vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/mysql ...
- springboot集成pagehelper插件
1.在pom.xml中引入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifact ...
- ubuntu 下openoffice安装
openoffice官网建议的安装步骤:http://www.openoffice.org/download/index.html 前提条件 如果你希望Java集成,你要确保你有安装最新的JRE.它的 ...