Given two non-negative integers num1 and num2represented 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:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. 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)的更多相关文章

  1. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  4. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  5. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  6. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  7. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  8. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. LeetCode: Multiply Strings. Java

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

随机推荐

  1. 2-gitblit添加项目,项目成员

    创建代码仓库,如下图所示, 然后填个项目的名称和描述即可点击保存. 用户也是,还是相同的位置,点击用户,而不是用户中心,创建用户也是填写用户名称和简称,最后对在旁边有个用户的权限,选择相应的代码仓库即 ...

  2. ArrayList类源码浅析(一)

    1.首先来看一下ArrayList类中的字段 可以看出,ArrayList维护了一个Object数组,默认容量是10,size记录数组的长度: 2.ArrayList提供了三个构造器:ArrayLis ...

  3. [LeetCode]-DataBase-Department Top Three Salaries

    The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...

  4. 突破css选择器的局限,实现一个css地址选择器?

    首先看一个效果,注意地址栏的变化 然后思考一下,用css如何实现? css选择器的局限 选择器是css中的一大特色,用于选择需要添加样式的元素. 选择器的种类有很多,比如 元素选择器 p {color ...

  5. C# 前台和后台POST提交信息的实现方法

    一.系统A(官网)与系统B(第三方支付平台)数据交换的方式 1.1  页面浏览器方式:系统A以构造Form表单的方式,通过系统A客户的浏览器重定向到系统B(向系统B发送请求),B系统完成交易后,将交易 ...

  6. ES6中对字符串处理的优点

    目录 1.字符的Unicode表示法 2.字符串的遍历接口 3.直接输入 U+2028 和 U+2029(分行符 和 分段符) 4.JSON.stringify() 的改造 1.字符的Unicode表 ...

  7. NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码)

    NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码)       七月,酷暑难耐,认识的几位同学参加知乎看山杯,均取得不错的排名.当时天池AI医疗大赛初赛结束,官方正在为复赛进行平台调 ...

  8. HTTP协议初步认识

    1.基本概念: HTML:HyperText Transfer Protocol,中文名:超文本传输协议,基于请求/响应模式,基于TCP/IP协议,是一种,无连接,无状态协议: 2.HTTP传输过程: ...

  9. vs2019安装

    1.下载vs_enterprise.exe(建议下载到无中文无空格目录) ,这个很小,官网下载企业版即可 2.在当前目录cmd命令执行: vs_enterprise.exe --layout offl ...

  10. C# App.config 自定义 配置节

    1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration>  ...