43. Multiply Strings (JAVA)
Given two non-negative integers num1
and num2
represented as strings, return the product of num1
and num2
, also represented as a string.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
Note:
- The length of both
num1
andnum2
is < 110. - Both
num1
andnum2
contain only digits0-9
. - Both
num1
andnum2
do not contain any leading zero, except the number 0 itself. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
需要考虑乘积的长度,以及乘积的每一位对应的是乘数中哪两个数字的乘积。
注意字符串首位出现0的情况,通常情况下至多首位为0,除了一个特殊情况乘数中有0,这样会造成String中多位为0,所以在开头要排除这个可能。
注意JAVA比较字符串相等必须用equals而不能用==;字符转换成int,除了-'0',还需要强制转换(char);char转成String,也要显示转换,使用String.valueOf
class Solution {
public String multiply(String num1, String num2) {
if(num1.equals("0")|| num2.equals("0")) return "0"; int[] product = new int[num1.length()+num2.length()]; //array to save the product
int m1;
int m2;
int p; for(int i = product.length-1; i >= 0; i--){ //iterate each bit of the product
for(int j = num2.length()-1; j>=0; j--){ //iterate each bit of multiplier2
if(i-j-1 < 0) continue;
if(i-j-1 >= num1.length()) break;
m2 = num2.charAt(j)-'0';
m1 = num1.charAt(i-j-1)-'0';
p = m1*m2;
product[i] += p;
}
} //calculate carry
for(int i = product.length-1; i > 0; i--){
if(product[i]<10) continue; product[i-1] += (product[i]/10);
product[i] %= 10;
} //transfrom integer to string
String result;
if(product[0] != 0) {
result = String.valueOf((char) (product[0] + '0'));
}
else result = "";
for(int i = 1; i < product.length; i++){
result += String.valueOf((char) (product[i] + '0'));
} return result;
}
}
43. Multiply Strings (JAVA)的更多相关文章
- [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 ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- 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 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 ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode: Multiply Strings. Java
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- D5000系统使用手册--AVC
有关概念: AVC:自动电压控制 PAS:网络分析应用 SCADA:电网稳态监控应用 闭环:AVC由SCADA系统获得电网的实时运行状态,分析计算后发出控制指令,电网运行状态变化后反馈回AVC,形成闭 ...
- [CSP-S模拟测试]:优化(贪心+DP)
题目描述 $visit\text{_}world$发现有下优化问题可以用很平凡的技巧解决,所以他给你分享了这样一道题:现在有长度为$N$的整数序列$\{ a_i\}$,你需要从中选出$K$个不想叫的连 ...
- 配置Nginx和Apache允许指定域名CORS跨域访问
前后端分离开发,导致前端项目需要跨域请求后端接口,解决方法有很多,本文只介绍两个: 1. 修改后端程序代码实现允许跨域请求 2. 修改服务器配置文件实现允许跨域请求 正文: 方法1:修改后端程序代码实 ...
- mysql 数据增删改的总结
一.在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用INSERT实现数据的插入2.UPDATE实现数据的更新3.使用DELETE实现数据的删除4.使用SELEC ...
- 洛谷P1198 [JSOI2008]最大数(单点修改,区间查询)
洛谷P1198 [JSOI2008]最大数 简单的线段树单点问题. 问题:读入A和Q时,按照读入一个字符会MLE,换成读入字符串就可以了. #include<bits/stdc++.h> ...
- npm 错误记录
npm run dev iview-weapp@1.1.0 dev /Users/Jovins/Desktop/小程序/iview-weapp gulp --gulpfile build/build- ...
- nginx location的优先级
原来一直以为location的优先级是先后顺序,结果有次项目中傻眼了,赶紧百度一下,下面的内容参考了这个链接 location表达式类型 ~ 表示执行一个正则匹配,区分大小写~* 表示执行一个正则匹配 ...
- FineReport打印方式(转)
1. 报表打印机制 各种打印的运行机制,都是选择打印时,先根据报表内容,在服务器的内存中将页面中的内容全部生成完毕,即生成对应格式的对象:然后再由serverlet直接推送给客户端,最后根据选择的打印 ...
- tomcat在45秒内没有启动,启动超时
在部署的时候出现Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server ...
- container_of宏解析 && 为什么需要使用中间变量__mptr?
#define container_of(ptr, type, member) ({ \ )->member ) *__mptr = (ptr); \ (type *)( (char *)__m ...