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 ...
随机推荐
- TCP通讯程序设计
TCP通讯程序设计 这里主要包含客户机和服务器的编程. 一.编程模型函数化 使用函数说明:socket的理解 服务器: 创建socket使用函数----->socket 绑定地址使用函数---- ...
- Linux文本比较-diff&awk
最近为了完成工作,需要将两个文件A.old和A进行比较,然后将A中新增加的部分保存到A中,其他部分删除.经过查找相关资料,发现有两种比较好的方法. 1. 使用diff命令 diff old.file ...
- 函数fseek() 用法(转)
在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h fseek 函数名: fseek ...
- hdu 4770(枚举 + dfs爆搜)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 思路:由于最多只有15个".",可以直接枚举放置的位置,然后判断是否能够全部 ...
- java判断字符串是否为数字或中文或字母
个人认为最好的方法 *各种字符的unicode编码的范围: * 汉字:[0x4e00,0x9fa5](或十进制[19968,40869]) * 数字:[0x30,0x39](或十进制 ...
- Android自动化测试之Monkey Test 安装(二)
因为Monkey Test是在eclipse上执行的,所以玩monkey test的时候要先配置安卓开发环境 一.Android开发环境搭建指南 1.安装JDK JDK下载链接:http://www. ...
- 【转】Kylin实践之使用Hive视图
http://blog.csdn.net/yu616568/article/details/50548967 为什么需要使用视图 Kylin在使用的过程中使用hive作为cube的输入,但是有些情况下 ...
- PB之入门-itemchanged(long row,dwobject dwo,string data)
每天的总结都是必须,好记性不如烂笔头,好吧,一星期没做笔记了,最近忙上PB了,哎东学学西学学,最可怕的就是最后都半斤八两,吐槽一下关于PB的资源为何如此之少,今天记录的是关于itemchanged事件 ...
- hdu5119 dp
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5119 题意: 输入T组数据,每组数据包括两个数n和m,接下来n个数,这n个数可以随意取( ...
- PHP 垃圾回收机制
PHP垃圾回收说到底是对变量及其所关联内存对象的操作, 所以在讨论PHP的垃圾回收机制之前,先简要介绍PHP中变量及其内存对象的内部表示(其C源代码中的表示). PHP官方文档中将PHP中的变量划分为 ...