Java [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.
解题思路:
设置数组记录单个位置相乘的结果,最后负责相加进位。
代码如下:
public class Solution {
public String multiply(String num1, String num2) {
int num1Length = num1.length();
int num2Length = num2.length();
int d1, d2;
int carry = 0;
int temp;
int[] caculate = new int[num1Length + num2Length];
StringBuilder sb = new StringBuilder(); for (int i = num1.length() - 1; i >= 0; i--) {
d1 = num1.charAt(i) - '0';
for (int j = num2.length() - 1; j >= 0; j--) {
d2 = num2.charAt(j) - '0';
caculate[i + j + 1] += d1 * d2;
}
} for (int i = caculate.length - 1; i >= 0; i--) {
temp = (caculate[i] + carry) % 10;
carry = (caculate[i] + carry) / 10;
caculate[i] = temp;
} for (int num : caculate)
sb.append(num); while (sb.length() > 1 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
} return sb.toString(); }
}
Java [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 ...
- 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 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 ...
随机推荐
- CC2640-之功耗
一.测量方式,以DEMO板测量,以消除其它外围不同造成的电流不同. 二.测量结果 以原厂simpleBLEperipheral工程为例 1.如果在低功耗模式下,+5DB发射,最小电流为1.66MA 2 ...
- java开发命名规范总结
一 包名的书写规范 (Package)推荐使用公司或机构的顶级域名为包名的前缀,目的是保证各公司/机构内所使用的包名的唯一性.包名全部为小写字母,且具有实际的区分意义. 1.1 一般要求1.选择有意义 ...
- HTML 菜单 a 标签设置样式
html: "<div style='font-weight:800;color:red'> <a href='javascript:void(0)'style='colo ...
- js 人工获取年月日
var date = new Date(); var months = new Array("01", "02", "03", " ...
- 自己开发开源jquery插件--给jquery.treeview加上checkbox
很多时候需要把树状的数据显示除来,比如分类,中国省份.城市信息,等,因此这方面的javascript插件也有很多.比如性能优异的jquery.treeview和国人开发的功能强大的zTree. 我最近 ...
- nginx+ tomcat集群+动静资源分离
不知道为什么这个随便删不掉,写了也值显示一半一半不显示, 我把重新写了一遍: nginx + tomcat集群和动静资源分离
- 如何使用 XSD
如何使用 XSD 一个简单的 XML 文档: 请看这个名为 "note.xml" 的 XML 文档: <?xml version="1.0"?> & ...
- jquery 数组和字典
1 数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...
- The 11th Zhejiang Provincial Collegiate Programming Contest->Problem G:G - Ternary Calculation
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3782 题意:把输入的三元运算用计算机运算出来. ; ci ...
- 对JAVA动态代理的理解
叫动态代理就代表着有“静态代理”这回事. 而且,通常“动态”至少听着更NB一点. 关键就在于不明白啥叫“动”,这个得跟“静”比较下. 在我的理解,静态代理得自己声明一个类,实现跟被代理对象同样的接口. ...