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 ...
随机推荐
- jQuery Mobile Datepicker 使用
插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...
- Linux3.18.6内核添加系统调用(32位系统)
1.将已经下载好的内核解压,我的目录是 /home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6 2.添加系统调用表: 在/home/ ...
- Java学习随笔2:Java复合赋值表达式的小问题
问题:i += j只是i = i + j的简写么? 答案:非也!看下面的程序: int i = 5; long j = 8; i += j; // 可以通过编译且结果正确 i = i + j; // ...
- Android自动化测试之Monkey Test 安装(二)
因为Monkey Test是在eclipse上执行的,所以玩monkey test的时候要先配置安卓开发环境 一.Android开发环境搭建指南 1.安装JDK JDK下载链接:http://www. ...
- kylin的安装与配置
我的环境: Cloudera Hadoop5.3.6 其中, Hadoop版本2.5.0 Hbase版本0.98.6 Hive版本0.13.1 使用的kylin版本:1.5.2.1 下载地址: htt ...
- strcmp函数的使用
Action() { /********************************* * Author:旺仔 * object:strcmp * date:2015-12-09 ...
- loadrunner怎么将变量保存到参数中
用这个lr_save_string 函数 char *b = "很简单";lr_save_string(b,"b"); lr_output_message(&q ...
- cvKMeans2函数用法概述
一般情况下,我们通过C++/Matlab/Python等语言进行实现K-means算法,结合近期我刚刚学的C++,先从C++实现谈起,C++里面我们一般采用的是OpenCV库中写好的K-means函数 ...
- 使用J2SE API读取Properties文件的六种方法
1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedInputStream(new FileInputStream( ...
- 【JavaScript基础入门】总结目录
一.JavaScript基础 1.1JavaScript概述 1.2如何使用的JavaScript 1.3JavaScript基本语法 1.4JavaScript数据类型 1.5JavaScript运 ...