【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 ...
随机推荐
- css雪碧技术的用法。
---恢复内容开始--- 在目前前端开发阶段,页面会出现大量的小图片,服务器加载的时候比较吃力,怎么用 一种办法把图片都合并到一张图片上呢?这就用到了css雪碧技术. 雪碧技术是雪碧团队开发,也有人叫 ...
- Android深入理解Context(二)Activity和Service的Context创建过程
前言 上一篇文章我们学习了Context关联类和Application Context的创建过程,这一篇我们接着来学习Activity和Service的Context创建过程.需要注意的是,本篇的知识 ...
- Python 导出导入安装包
python导出安装包 pip freeze > requirements.txt python导入安装包 pip install -r requirements.txt
- git clone过程中发生的错误
错误提示: 问题原因以及解决方式:http://blog.csdn.net/huihut/article/details/79404421
- 7.log4j2的使用
一.简介 log4j2相对于log4j 1.x有了脱胎换骨的变化,其官网宣称的优势有多线程下10几倍于log4j 1.x和logback的高吞吐量.可配置的审计型日志.基于插件架构的各种灵活配置等.如 ...
- selenium&PhantomJS笔记
配置pip文件 Windows下pip 配置文件的位置%HOME%/pip/pip.ini linux下安装pip,以Debian Linux为例su -apt-get install python- ...
- Linux 加载卷组
root 用户下执行: vgchange -ay vgdatamount /u01 vgdisplay 查看卷组
- B-树特征
在m阶B-树的定义中,要求: 1.树中每个节点至多有m棵子树. 2.若根节点不是叶子节点,则至少有两棵子树. 3.除根之外的所有非终端节点至少有棵子树.
- 将mongodb设置为windows服务
[转载] [转载]安装mongodb以及设置为windows服务 详细步骤 将mongodb设置成windows服务,这样就不用使用命令启动了,设置方法如下: 1.在data文件夹下新建一个log文件 ...
- 设置UINavigationController标题的属性
设置UINavigationController标题的属性 self.title = @"产品详情"; [self.navigationController.navigationB ...