【LeetCode43】 Multiply Strings
题目描述:

解题思路:

java代码:
public class LeetCode43 {
public static void main(String[] args) {
String num1="123";
String num2="45";
System.out.println(num1+"和"+num2+"相乘的结果是:"+new Solution().multiply(num1, num2));
}
}
class Solution {
public String multiply(String num1, String num2) {
int len1=num1.length(),len2=num2.length();
int[] result=new int[len1+len2];
for(int i=len1-1;i>=0;i--){
for(int j=len2-1;j>=0;j--){
int mul=(num1.charAt(i)-'0')*(num2.charAt(j)-'0');
int p1=i+j,p2=i+j+1;
int sum=mul+result[p2];
result[p1]+=sum/10;
result[p2]=sum%10;
}
}
StringBuilder sb=new StringBuilder();
for(int e:result){
if(!(sb.length()==0&&e==0))//若最高位是0,去除
sb.append(e);
}
return sb.length()==0?"0":sb.toString();
}
}
测试结果:

【LeetCode43】 Multiply Strings的更多相关文章
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【Leetcode】【Medium】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode练习题】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- 【POJ2406】【KMP】Power Strings
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- 【LeetCode415】Add Strings
题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(St ...
- 【hash】Power Strings
[题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...
- 【LeetCode每天一题】Multiply Strings(字符串乘法)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
随机推荐
- opencv图像处理时使用stringstream批量读取图片,处理后并保存
简介: 同文件输入输出流一样,使用stringstream可以批量读取图片,处理后并进行保存.因为C++中头文件 stringstream既可以从string读数据也可向string写数据,利于其这个 ...
- ios或者cocos2d-x开发在Xcode编译时自适应失效,获取屏幕尺寸不准确
在cocos2d-x的开发中,发现之前很好使的 setDesignResolutionSize(960.0f, 640.0f, kResolutionExactFit)自适应不好用了,后来调试发现不是 ...
- 记一次Android studio升级之后的坑
像往常一样打开Android studio,但这次它提示我升级!说是什么为了更好的体验,在好奇心的驱使下,我毅然地点击了“update”按钮.升级之后,编译项目,报出了N多个error,我的心都慌完! ...
- Android 获取全局Context的技巧
回想这么久以来我们所学的内容,你会发现有很多地方都需要用到Context,弹出Toast的时候需要.启动活动的时候需要.发送广播的时候需要.操作数据库的时候需要.使用通知的时候需要等等等等.或许目前你 ...
- 将远程UI分支克隆到本地UI分支
git checkout -b UI git remote add origin <url> git fetch origin git branch --track UI origin/U ...
- Selenium clear()方法无法清掉数据
问题描述 clear()方法执行过后, 数据还是在. 根本原因 存在镜像节点. 操作clear()清掉数据后, 镜像节点的数据还在, 就会再补充回去. 解决办法 添加下面代码就可以连同镜像的数据一起去 ...
- ASP.NET Web API 跨域访问(CORS)要注意的地方
一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...
- python基础_类型_dict
#dict字典,其实与其叫字典不如叫key-value更合适,不知道这个名是怎么来的,这个可以用来搞用户名和密码,不过搞这个不用数据库是不是显得太low了 #用花括号{}扩起来,逗号分隔每个元素,每个 ...
- EXC_BAD_ACCESS错误
EXC_BAD_ACCESS错误 一直都是使用ARC开发,咋就莫名其妙的出现这种EXC_BAD_ACCESS错误. 一直都是是怀疑在block中有着特殊处理,导致了使用时出现了这种错误,查找了好久.. ...
- CVE-2013-2551漏洞成因与利用分析(ISCC2014 PWN6)
CVE-2013-2551漏洞成因与利用分析 1. 简介 VUPEN在Pwn2Own2013上利用此漏洞攻破了Win8+IE10,5月22日VUPEN在其博客上公布了漏洞的细节.它是一个ORG数组整数 ...