给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积。
注意:
    num1 和 num2 的长度均小于110。
    num1 和 num2 均只包含数字 0-9。
    num1 和 num2 均不以零开头。
    你不能使用任何内置的大整数库或直接将输入转换为整数。
详见:https://leetcode.com/problems/multiply-strings/description/

Java实现:

class Solution {
public String multiply(String num1, String num2) {
num1 = new StringBuilder(num1).reverse().toString();
num2 = new StringBuilder(num2).reverse().toString();
int[] d = new int[num1.length() + num2.length()];
for (int i = 0; i < num1.length(); i++) {
int a = num1.charAt(i) - '0';
for (int j = 0; j < num2.length(); j++) {
int b = num2.charAt(j) - '0';
d[i + j] += a * b;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < d.length; i++) {
int digit = d[i] % 10;
int carry = d[i] / 10;
sb.insert(0, digit);
if (i < d.length - 1){
d[i + 1] += carry;
}
}
while (sb.length() > 0 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
}
return sb.length() == 0 ? "0" : sb.toString();
}
}

参考:https://www.cnblogs.com/grandyang/p/4395356.html

https://www.cnblogs.com/springfor/p/3889706.html

043 Multiply Strings 字符串相乘的更多相关文章

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

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

  2. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  3. [LeetCode] Multiply Strings 字符串相乘

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

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

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

  5. 43. Multiply Strings 字符串相乘

    1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...

  6. Leetcode43. Multiply Strings字符串相乘(大数相乘)

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  7. 43. Multiply Strings字符串相乘

    网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...

  8. LeetCode 043 Multiply Strings

    题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...

  9. [Leetcode] Multiply strings 字符串对应数字相乘

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

随机推荐

  1. eclipse的maven工程Dynamic Web Module 2.3 修改为3.0 解决办法

    1. 创建Maven Web工程 2. 项目只有src/main/resources >Java Build Path导入Tomcat运行环境 3. 删除以图片红框中的文件 4. Propert ...

  2. 苹果手机app试玩平台汇总--手机链接入口

    注意: 点击下载,根据提示步骤走即可. 下载后绑定手机号和微信后.才能提现 每天3点更新任务,4点最多! | 平台 | 提现额 | 任务量| 推荐强度 | 下载 | 1.小鱼,10元,大量,强推! → ...

  3. Mac替换sublime图标

    下载.icns格式一个图标:http://www.easyicon.net/language.en/iconsearch/sublime/ 终端执行:open /Applications/Sublim ...

  4. zero to one(1)

    How to google 这两天把之前的过期的账号重新弄了一下,在vutrl上的账号,普通的话现在2.5$只提供ipv6地址,如果是想google我觉得这个应该没有什么问题,或者可以买***的账号, ...

  5. hdfs 查看报告--命令(hdfs dfsadmin -report)

    [hadoop@master sbin]$ hdfs dfsadmin -reportConfigured Capacity: 8202977280 (7.64 GB)Present Capacity ...

  6. loj 6089 小 Y 的背包计数问题——分类进行的背包

    题目:https://loj.ac/problem/6089 直接多重背包,加上分剩余类的前缀和还是n^2的. 但可发现当体积>sqrt(n)时,个数的限制形同虚设,且最多有sqrt(n)个物品 ...

  7. Tomcat加载servlet类文件

    问题1:tomcat什么时候加载servlet? 有两种情况 一种是启动时加载 一种是请求时加载 第一种是在web.xml中的<servlet>节点下增加类似:<load-on-st ...

  8. 五 python 发送邮件

    需求: 系统中使用一个邮箱(这里用QQ)给其他邮箱发消息,用python完成,步骤如下: 1: 开启QQ邮箱的SMTP服务.设置-> 账号 2: 开启邮箱服务:这个需要手机验证,最后会给你一个授 ...

  9. (三)整合SSH测试项目

    整合struts 和 spring 预期:如果可以在action中能够正确调用service里面的方法执行并返回到一个页面中:那么我们认定struts和spring的整合是成功的. 编写JUnit测试 ...

  10. 8. php回调后门

    中国菜刀下载,基于原版中国菜刀优化版20160309. 下载地址: http://pan.baidu.com/s/1jHoJxHW China chopper http://pan.baidu.com ...