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 ...
随机推荐
- UVA 11059
Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the ...
- 关于CSS的图像放大问题的解决,需要借助jQuery等直接用css3设置
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- 【git】学习路径失败了
期初规划:搭建git远程服务器 使用gitlab作为管理工具 过程遇到的问题 1.gitlab不能安装到win ,且对centos要求6以上,我只有一台centos5 让运维帮升级 ...等待.. ...
- easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined
easyui使用时出现这个Uncaught TypeError: Cannot read property 'nodeName' of undefined 最后检查发现是必须给select一个id,光 ...
- Xcode和github入门详细教程
Xcode和github详细教程! 主要是参考了现在网上的一些资料给没整过的人一个详细的指南. (1)先在github上注册账号,自行解决! (2)在导航栏右上角new一个repository(仓库) ...
- bnu 4359 无爱编号(规律)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4359 [题意]:输入N,表示几位数,0-这个N位数,有多少个满足条件的号码,不满足的情况为出现4,1 ...
- Automotive Security的一些资料和心得(1):Security Engineering
陆续更新一些最近在Automotive Security方面的资料和心得. 1. Overview 1.1. Software Engineering Process PLC-Phases: Intr ...
- What we learned in Seoul with AlphaGo
What we learned in Seoul with AlphaGo March 16, 2016 Go isn’t just a game—it’s a living, breathing c ...
- FlushMode属性与transaction(spring注入的事务)
一.参见hibernate的api http://tool.oschina.net/apidocs/apidoc?api=hibernate-3.6.10 http://tool.oschina.ne ...
- CSS3新的字体尺寸单位rem
CSS3引入新的字体尺寸单位 rem ,可以简单记忆为root rm. CSS3的出现,他同时引进了一些新的单位,包括我们今天所说的rem.在W3C官网上 是这样描述rem的——“font size ...