LeetCode(43. Multiply Strings)
题目:
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.
思路比较简单,不多说啦
几个需要注意的点:
1. 高位多个为0的,需要进行判断
2. 如果乘数为0的话,可以直接跳过,能节省不少时间
3. string 的两个使用
1) 创建有 len 长的 ‘0’串 string str(len, '0')
2) string 的反转 reverse(str.begin(), str.end()) 反转后操作好像更容易理解~
class Solution {
public:
string multiply(string num1, string num2) {
int size1 = num1.size();
int size2 = num2.size();
string result(size1 + size2 + , '');
reverse(num1.begin(),num1.end());
reverse(num2.begin(), num2.end());
for(int j = ; j < size2; ++j){//num2
if(num2[j] == '')
continue;
int k = j;
for(int i = ; i < size1; ++i){//num1
int tmp = (num1[i] - '') * (num2[j] - '') + result[k] - '';
result[k] = tmp % + '';
result[k + ] = result[k + ] + tmp / ;
++k;
}
}
while(!result.empty() && result.back() == '') result.pop_back();
if(result.empty()) return "";
reverse(result.begin(), result.end());
return result;
}
};
LeetCode(43. Multiply Strings)的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- leetcode 43. Multiply Strings(高精度乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
随机推荐
- 直接拿来用!最火的Android开源项目(完结篇)(转)
摘要:截至目前,在GitHub“最受欢迎的开源项目”系列文章中我们已介绍了40个Android开源项目,对于如此众多的项目,你是Mark.和码友分享经验还是慨叹“活到老要学到老”?今天我们将继续介绍另 ...
- Vs2010工具栏显示“开始执行“按钮
转载来源:http://blog.csdn.net/fromhj/article/details/8795047 前言 在用visual studio 2010的时候,要运行程序,可以使用 1.菜单- ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- Java学习笔记(六)——方法
一.方法定义 1.语法: 其中: (1) 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.private 甚至可以省略 ,其中 public 表示该方法可以被其他任何 ...
- sql2014 新建用户并登陆
EXEC master.dbo.sp_addlogin @loginame = N'testuser1', @passwd = '123456', @defdb = N'master', @defla ...
- Loadrunner中参数化实战(1)-Sequential+Each iteration
参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Sequential+Each iteration 设置迭代4次Action 结果如下:
- [转载]C/C++可变参数之va_start和va_end使用详解
本文主要介绍va_start和va_end的使用及原理. 在以前的一篇帖子Format MessageBox 详解中曾使用到va_start和va_end这两个宏,但对它们也只是泛泛的了解. 介绍这两 ...
- Linux IO模型和网络编程模型
术语概念描述: IO有内存IO.网络IO和磁盘IO三种,通常我们说的IO指的是后两者. 阻塞和非阻塞,是函数/方法的实现方式,即在数据就绪之前是立刻返回还是等待. 以文件IO为例,一个IO读过程是文件 ...
- css的重置和原子类的使用
@charset "utf-8";/* Reset Definitions*/body, div, p, a, ul, ol, li, dl, dt, dd, h1, h2, h3 ...
- struts2总结三:struts2配置文件struts.xml的简单总结
一.struts中的常量constant的配置. 在struts2中同一个常量的配置有三种方式,第一种在struts.xml中,第二种在struts.properties中配置,第三种在web.xml ...