题目描述:

解题思路:

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的更多相关文章

  1. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  2. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  3. 【Leetcode】【Medium】Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. 【LeetCode练习题】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  5. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  6. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  7. 【LeetCode415】Add Strings

    题目描述: 解决思路: 此题较简单,和前面[LeetCode67]方法一样. Java代码: public class LeetCode415 { public static void main(St ...

  8. 【hash】Power Strings

    [题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...

  9. 【LeetCode每天一题】Multiply Strings(字符串乘法)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

随机推荐

  1. js与native的交互

    WebView与Javascript交互(Android): WebView与Javascript交互是双向的数据传递,1.H5网页的JS函数调用Native函数 2.Native函数调用JS函数,具 ...

  2. Javaweb查询客户&分页部分代码

    pageBean工具类代码(分页工具) package com.home.domain; import java.util.List; /** * 分页的JavaBean * @author Admi ...

  3. Java基础—注解的使用

    1.注解的概述: 注解是用来替代配置文件的!你回忆一下,我们以前总是要写一些配置文件,例如web.xml你还记得么?里面要写<servlet>和<servlet-mapping> ...

  4. RecyclerView分隔线定制

    分割线我们利用RecyclerView的addItemDecoration(ItemDecoration fromHtml) 新建一个类来看看到底是什么: public class CategoryI ...

  5. java 空语句

    输入的字符不是回车就重新输入: import java.io.IOException; public class HelloWorld { public static void main(String ...

  6. 回归JavaScript基础(二)

    主题:在HTML中使用JavaScript. 要想把JavaScript放到网页中,就必须涉及到Web的核心语言HTML.向HTML页面中插入JavaScript的主要方法,就是使用<scrip ...

  7. Oracle ALL DBA表

    select * from all_tab_comments -- 查询所有用户的表,视图等 select * from user_tab_comments -- 查询本用户的表,视图等 select ...

  8. java虚拟机---内存

    java虚拟机---内存 Java虚拟机,即JVM,负责运行java程序,每个java程序都运行在一个具体jvm实例上.Java虚拟机的体系架构分为:类装载子系统.运行时数据区.执行引擎.类装载子系统 ...

  9. 全局唯一ID生成器

    分布式环境中,如何保证生成的id是唯一不重复的? twitter,开源出了一个snowflake算法,现在很多企业都按照该算法作为参照,实现了自己的一套id生成器. 该算法的主要思路为: 刚好64位的 ...

  10. 关于easyUI

    <input class="easyui-combobox" id="s_dataDicName" data-options="panelHei ...