【leetcode】Multiply Strings(middle)
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.
思路:直观思路,就是模拟乘法过程。注意进位。我写的比较繁琐。
string multiply(string num1, string num2) {
vector<int> v1, v2, vmulti;
vector<vector<int>> vvtmp;
int l1 = num1.size();
int l2 = num2.size();
if(l1 == && num1[] == '') return "";
if(l2 == && num2[] == '') return "";
int maxl = ;
//用vector存储两个数字 v[0]是最高位
for(int i = ; i < l1; i++)
v1.push_back(num1[i] - '');
for(int i = ; i < l2; i++)
v2.push_back(num2[i] - '');
//乘步骤
for(int i = l1 - ; i >= ; i--)
{
vector<int> vtmp; //v[0]是最低位
int t = i;
while(t < l1 - ) //后面补错位的0
{
vtmp.push_back();
t++;
}
int c = ; //记录进位
for(int j = l2 - ; j >= ; j--)
{
int cur = v1[i] * v2[j] + c;
vtmp.push_back(cur % );
c = cur / ;
}
if(c != )
vtmp.push_back(c);
vvtmp.push_back(vtmp);
maxl = (vtmp.size() > maxl) ? vtmp.size() : maxl;
}
//加步骤
int c = ; //记录进位
for(int i = ; i < maxl; i++)
{
int cur = c;
for(int j = ; j < vvtmp.size(); j++)
{
if(i >= vvtmp[j].size())
continue;
cur += vvtmp[j][i];
}
vmulti.push_back(cur % );
c = cur / ;
}
if(c != )
vmulti.push_back(c);
//转换为string
string ans;
for(int i = vmulti.size() - ; i >= ; i--)
{
ans += (vmulti[i] + '');
}
return ans;
}
大神总是能把多个步骤一次到位:
string multiply(string num1, string num2) {
string sum(num1.size() + num2.size(), '');
for (int i = num1.size() - ; <= i; --i) {
int carry = ;
for (int j = num2.size() - ; <= j; --j) {
int tmp = (sum[i + j + ] - '') + (num1[i] - '') * (num2[j] - '') + carry; //把当前乘出来的数字和之前的数字以及进位相加
sum[i + j + ] = tmp % + '';
carry = tmp / ;
}
sum[i] += carry; //这个是最高位多出来的进位 其他的都是i+j+1 这里没有+1
}
size_t startpos = sum.find_first_not_of("");
if (string::npos != startpos) {
return sum.substr(startpos);
}
return "";
}
【leetcode】Multiply Strings(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- greenDao生成的实体类无法存放JsonArray的解决方法
今天在解析Json数据的时候,发现我们用greenDao生成的实体类只能是基本数据类型,而我请求回来的json数据里面还包含了jsonArray. 下面是json的数据格式 "content ...
- android开发中经常遇到的问题汇总
大家都在为项目开发成功而喜悦,但可不知成功的路上是会经常出错的,下面是我碰到的一些错误集合! [错误信息] [2011-01-19 16:39:10 - ApiDemos] WARNING: Appl ...
- linux 捕获信号处理中遇到的死锁
tag: 信号 signal sigchld 死锁 堆栈 我们的程序需要捕获信号自己处理,所以尝试对1-32的信号处理(后面33-64的信号不处理).但是在调试代码时,发现一个线程死锁的问题.程序 ...
- ubuntu14.04建立交叉编译环境, 注意事项
ubuntu14.04建立交叉编译环境, 注意事项 ~$ arm-linux-gcc/opt/FriendlyARM/toolschain/4.4.3/bin/arm-linux-gcc: 15: e ...
- Quartus13.0破解方法
一定要按照步骤顺序才能破解,这里很关键 1.下载和打开Quartus II破解器,选择“应用”,选择“是”,找到bin(64位系统是bin64)目录下的sys_cpt.dll,“打开” 2.然后将li ...
- 26.68013 烧录方式 及iic生成
硬件程序烧录 1)因为本产品要求将二进制代码和硬件PID/VID烧录在EEPROM,而不是使用CYPRESS推荐的在线下载方式,所以外部采用了8K的EEPROM.上电后68013A会将EEPROM中的 ...
- UIProgressView swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- P1179: [Apio2009]Atm
缩点+spfa最短路,因为最终不可能有环,所以直接spfa. ; type node=record f,t:longint; end; var n,m,s,i,j,ans,cnt,num,u,x,dg ...
- unity工具IGamesTools之批量生成帧动画
unity工具IGamesTools批量生成帧动画,可批量的将指定文件夹下的帧动画图片自动生成对应的资源文件(Animation,AnimationController,Prefabs) unity工 ...
- (转)Unity3D游戏开发 NGUI之渐变加载到100%的Loading场景进度条
NGUI 现有的进度条存在的问题: 进度条跳跃式前进,加载到90%后卡住,突然进入下一个场景.接下来就是解决这个问题. 背景 通常游戏的主场景包含的资源较多,这会导致加载场景的时间较长.为了避免这个问 ...