【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 ...
随机推荐
- javaSE——File文件的操作
File文件操作: 在java中,File是文件和文件夹的总称. 在java中也是一个类,可以创建多个file对象. File类的一些API import java.io.File; public c ...
- 活字格企业 Web 应用生成器新手上路指南
活字格是一款企业 Web 应用生成器,使用了类 Excel 的设计界面,通过简单的拖拽操作,就能快速制作出一个 Web 信息管理系统.在整个使用过程中无需专业软件知识,没有任何技术门槛,能轻松实现各行 ...
- oracle 分组取第一行数据 ,查询sql语句
oracle 分组取第一行数据 SELECT * FROM ( SELECT ROW_NUMBER() OVER(PARTITION BY x ORDER BY y DESC) rn, t.* FR ...
- 润乾V5手机报表说明文档
1.手机报表实例页面简要说明 index.jsp 是报表资源列表页面: mbReport.jsp 是报表展现页面: mbParam.jsp是参数报表展现页面: echarts.jsp是带有echart ...
- 解决升级PHP7.1后,发邮件时提示“fsockopen(): Peer certificate CN=`xxx.xx.com' did not match expected CN=`113.x.xx.98”
把项目环境升级到PHP7.1后,发现在不使用SSL时可以使用IP发邮件,可设置成SSL时就只能使用hostname发送,PHP提示的错误信息大致意思是说,IP与hostname无法通过SSL验证,修改 ...
- Android组件系列----Activity组件详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- Eigen学习之Array类
Eigen 不仅提供了Matrix和Vector结构,还提供了Array结构.区别如下,Matrix和Vector就是线性代数中定义的矩阵和向量,所有的数学运算都和数学上一致.但是存在一个问题是数学上 ...
- Coroutine及其实现
线程是内核对外提供的服务,应用程序可以通过系统调用让内核启动线程,由内核来负责线程调度和切换.线程在等待IO操作时线程变为unrunnable状态会触发上下文切换.现代操作系统一般都采用抢占式调度,上 ...
- python入门の缩进魔术
idx=1 sum=0 while idx<=100: sum=idx+sum idx=idx+1 print('sum 100 =', sum) ======================= ...
- Hybris阶段总结(2)hybris架构
年前总结一下这两个星期在SAP实习学到的一些东西 先上图: 从底往上总结,之后会有个小例子来解释一下 1.Persistence layer 就是作为hybris所连接的数据库这一层,其中hybr ...