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 ...
随机推荐
- OpenMesh 读写网格控制(读取写入纹理坐标,法向等)
OpenMesh读取网格默认是不自动读取obj网格中的法向,纹理坐标等信息的,写入网格同样也是.所以要读取(或写入)这些信息需要修改默认的选项. 先看一下其读写网格的函数 template<cl ...
- 聊聊传统oo和js的某些对比——对象/函数/new关键字等
自己的学习记录,写的短点可以以后短时间内理清一些疑惑,看前要求你至少了解js中关于原型链等基本概念,因为文章直接以总结的形式理出知识点,没有去解释一些基本的概念! 1.1.熟记两句话,预预热 1. 函 ...
- 程序员必懂:javaweb三大框架知识点总结
原文链接:http://www.cnblogs.com/SXTkaifa/p/5968631.html javaweb三大框架知识点总结 一.Struts2的总结 1.Struts 2的工作流程,从请 ...
- Struts2请求参数校验
校验的分类 客户端数据校验 和 服务器端数据校验 客户端数据校验 ,通过JavaScript 完成校验 (改善用户体验,使用户减少出错 ) 服务器数据校验 ,通过Java代码 完成校验 struts2 ...
- BurpSuite抓App数据包的方法
软件准备: 1.猎豹wifi 2.BurpSuite或者fillder都可以 查看电脑IP地址: 网卡ip: 确保无线网卡的IP和手机的代理IP保持一致即可
- PHP 字符检测自定义函数
<?php /** * 转义字符替换 * * @param string $subject * @return string */public static function sReplace( ...
- iOS 'The sandbox is not sync with the Podfile.lock错误
出现以下错误时, diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or dire ...
- iOS 原生网络请求(推荐使用AFNetWorking库)
. 同步GET请求 //第一步,创建URL NSURL *url = [NSURL URLWithString:@"http://api.hudong.com ...
- java-数字类
浏览以下内容前,请点击并阅读 声明 对于6种基本的数字类型,java提供了相对应的类.Number类和六种细分的子类.
- ural 1072. Routing
1072. Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. ...