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.
- Converting the input string to integer is NOT allowed.
- You should NOT use internal library such as BigInteger.
高精度乘法。需要注意的是char表示的数字和int对应的数字之间的转化。
class Solution {
public:
string multiply(string num1, string num2) {
int l1=num1.length(),l2=num2.length();
if(l1==||l2==){
string ans="";
return ans;
}
string ans(l1+l2,'');
for(int i=l1-;i>=;i--){
int add=;
for(int j=l2-;j>=;j--){
int t=(ans[i+j+]-'')+(num1[i]-'')*(num2[j]-'')+add;
ans[i+j+]=t%+'';
add=t/;
}
ans[i]=add+'';
}
size_t s0 = ans.find_first_not_of('');
if(s0!=string::npos){
return ans.substr(s0);
}
return "";
}
};
leetcode 43. Multiply Strings(高精度乘法)的更多相关文章
- [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 ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- 43. Multiply Strings (大数乘法)
DescriptionHintsSubmissionsDiscussSolution Pick One Given two non-negative integers num1 and num2 ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
随机推荐
- [BLE--Link Layer]物理信道
简述 有线通信,是用电缆直接连接.然后分距离的长短.有些会须要载入波,信号也可能会经过不同的调制方式调制. 无线通信也是一样,仅仅是信号的传输是通过射频了,通过在某一频段.对无线信道进行调制,将数据发 ...
- python中@property的使用
在绑定属性时,如果我们将属性直接暴露在外面,就可能导致属性被任意修改,有时候这个是我们不希望看到的如:设置学生的成绩 class Student(object): def __init__(self) ...
- HDU 2255 奔小康赚大钱 KM裸题
#include <stdio.h> #include <string.h> #define M 310 #define inf 0x3f3f3f3f int n,nx,ny; ...
- javascript关闭弹出窗体时刷新父窗体和居中显示弹出窗
居中显示用到了moveTO()方法: 关闭弹出窗时刷新父窗体用到了window.opener方法: 父窗体代码例如以下: <%@ Page Language="C#" Aut ...
- ASP.NET机制详细的管道事件流程(转)
ASP.NET机制详细的管道事件流程 第一:浏览器向服务器发送请求. 1)浏览器向iis服务器发送请求网址的域名,根据http协议封装成请求报文,通过dns解析请求的ip地址,接着通过socket与i ...
- python学习(十一)函数、作用域、参数
定义和调用函数 在这里函数的定义和调用和一般的语句没什么不一样,感觉函数也是对象 #!/usr/bin/python def times(x, y): # 定义函数 ...
- Yii的权限管理rbac
1.首先我们要在配置文件的组件(component)里面配置一下 Rbac 在对应项目下的config/main.php或者config/main-local.php下添加 'authManager' ...
- struts2 eclipse集成jdk与tomcat (2)
Eclipse 中集成jdk与tomcat 1. 首次打开Eclipse为让你选择工作空间,选择合适即可. 添加JDK (1) 在Eclipse的菜单中选择window选项,单击 perference ...
- 解析域名得到IP
本文转载至 http://www.cocoachina.com/bbs/read.php?tid=142713&page=e&#a 分享类型:游戏开发相关 #include &l ...
- php函数: set_error_handler
<?php // $errno, $errstr, $errfile, $errline , 系统自动生成这四个变量的值(如果存在!) function error_catcher($errno ...