Multiply Strings,字符串相乘
问题描述:给定两个字符串,返回他们的乘积。
public class MultiplyStrings
{
public String multiply(String num1, String num2)
{
String n1 = new StringBuilder(num1).reverse().toString();
String n2 = new StringBuilder(num2).reverse().toString(); int[] d = new int[num1.length()+num2.length()]; //multiply each digit and sum at the corresponding positions
for(int i=0; i<n1.length(); i++){
for(int j=0; j<n2.length(); j++){
d[i+j] += (n1.charAt(i)-'0') * (n2.charAt(j)-'0');
}
} StringBuilder sb = new StringBuilder(); //calculate each digit
for(int i=0; i<d.length; i++)
{
int mod = d[i]%10;
int carry = d[i]/10;
if(i+1<d.length)
{
d[i+1] += carry;
}
sb.insert(0, mod);
} //remove front 0's
while(sb.charAt(0) == '0' && sb.length()> 1){
sb.deleteCharAt(0);
} return sb.toString();
}
}
Multiply Strings,字符串相乘的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 43. Multiply Strings 字符串相乘
1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...
- 043 Multiply Strings 字符串相乘
给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意: num1 和 num2 的长度均小于110. num1 和 num2 均只包含数字 0 ...
- Leetcode43. Multiply Strings字符串相乘(大数相乘)
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode每天一题】Multiply Strings(字符串乘法)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
随机推荐
- AEcs6破解版下载
下载地址 http://pan.baidu.com/share/link?shareid=79184520&uk=1795677788 点击下载
- 170208、用Navicat自动备份mysql数据库
数据库备份很重要,很多服务器经常遭到黑客的恶意攻击,造成数据丢失,如果没有及时备份的话,后果不堪设想. 一:备份的目的: 做灾难恢复:对损坏的数据进行恢复和还原 需求改变:因需求改变而需要把数据还原到 ...
- 阿里巴巴Java开发规约IDEA插件安装及使用
技术交流群:233513714 一.通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories.. ...
- English Grammar
What is Grammar?
- canvas基本使用
1.什么是CANVAS canvas是html5新增的画布元素,可以通过javascript来在画布上绘制图形,图标以及任何视觉性的图像. 2.canvas的用途 替代flash,做各种动态效果,做小 ...
- pro_update_role_pwd
DELIMITER | drop procedure if exists pro_update_role_pwd; CREATE PROCEDURE pro_update_role_pwd ( cro ...
- 让Windows Server 2008+IIS 7+ASP.NET支持10万个同时请求
具体设置如下: 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools >Advanced Settin ...
- Mycat教程---数据库的分库分表
mycat介绍 介绍在官方网站上有比较详细的介绍,在这里复制粘贴没什么意思,大家到官网上看 官网链接 前置条件 本教程是在window环境下运行的,实际生产推荐在Linux上运行. 必备条件(自行安装 ...
- rsync+inotify实时同步
!!!在安装前要先确保,rsync daemon服务配置成功,在安装inotify-tools前先确认你的linux内核是否达到了2.6.13,并且在编译时开启CONFIG_INOTIFY选项,也可以 ...
- 向html当中插入数据
].;i<obj.length;i++){ $('#compclass').append("<option>"+obj[i].fields.name+" ...