描写叙述:

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

Note: The numbers can be arbitrarily large and are non-negative.

思路:

简而言之,要实现的就是BigInteger(a).Multiply(BigInteger(b))的功能,但非常显然,leetcode中不让用BigInteger

代码:

public class Solution {
public String multiply(String num1, String num2)
{
if(num1==null||num2==null)
return new String();
num1=num1.trim();
num2=num2.trim();
if(num1.equals("0")||num2.equals("0"))
return "0";
List<Integer>listNum1=new ArrayList<>();
List<Integer>listNum2=new ArrayList<>();
List<Integer>listResult=new ArrayList<>();
int len1=num1.length();
int len2=num2.length();
int i=0,j=0,lenResult=0,index=0;
int baseNum=0,flowNum=0,tempNum1=0,tempNum2=0;
for( i=len1-1;i>=0;i--)
listNum1.add(num1.charAt(i)-'0');
for( i=len2-1;i>=0;i--)
listNum2.add(num2.charAt(i)-'0');
tempNum2=listNum2.get(0);
for(i=0;i<len1;i++)
{
tempNum1=listNum1.get(i);
tempNum1=tempNum1*tempNum2+flowNum;
baseNum=tempNum1%10;
flowNum=tempNum1/10;
listResult.add(baseNum);
}
if(flowNum!=0)
{
listResult.add(flowNum);
flowNum=0;
}
for(j=1;j<len2;j++)
{
baseNum=0;flowNum=0;
tempNum2=listNum2.get(j);
lenResult=listResult.size();
for(i=0;i<len1;i++)
{
index=i+j;
if(index<lenResult)
{
tempNum1=listNum1.get(i);
tempNum1=tempNum1*tempNum2+flowNum+listResult.get(index);
baseNum=tempNum1%10;
flowNum=tempNum1/10;
listResult.set(index, baseNum);
}
else {
tempNum1=listNum1.get(i);
tempNum1=tempNum1*tempNum2+flowNum;
baseNum=tempNum1%10;
flowNum=tempNum1/10;
listResult.add(baseNum);
}
}
if(flowNum!=0)
{
listResult.add(flowNum);
flowNum=0;
} }
if(flowNum!=0)
listResult.add(flowNum);
StringBuilder sBuilder=new StringBuilder();
for(int num:listResult)
sBuilder.append(num);
sBuilder.reverse();
return sBuilder.toString(); }
}

leetcode_Multiply Strings的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Multiply Strings

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

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

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

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

随机推荐

  1. Gym 100971B 水&愚

    Description standard input/output Announcement   Statements A permutation of n numbers is a sequence ...

  2. make gif pic

    http://www.cockos.com/licecap/ licecap 1.open licecap 2.record ,make a pic filename,like pic1 3.work ...

  3. python爬虫异常处理

    import urllib2 try: response = urllib2.urlopen('http://www.baidu.com') except urllib2.URLError, e: p ...

  4. 【12】vue-router 之路由重定向

    看之前的项目,突然发现一个不算bug的bug,之前也是一直没有想到,现在发现之后越来越觉得有必要改掉, 项目用的是vue做的,自然切换用的就是路由,一级路由包括:首页.记录和个人中心,二级路由是在记录 ...

  5. JavaScript (JS) 函数补充 (含arguments、eval()、四种调用模式)

    1. 程序异常 ① try-catch语法    测试异常 try-catch语法代码如下: try { 异常代码;     try中可以承重异常代码, console.log(“try”)  出现异 ...

  6. Java数据结构-------List

    三种List:ArrayList,Vector,LinkedList 类继承关系图 ArrayList和Vector通过数组实现,几乎使用了相同的算法:区别是ArrayList不是线程安全的,Vect ...

  7. URL重写IIS7(URL Rewrite Module) 比之前的urlrewrite更方便使用

    原文发布时间为:2011-02-24 -- 来源于本人的百度文章 [由搬家工具导入] 微软在IIS7中添加了URL的重写模块,并且免费使用,可以导入.htaccess规则,确实是个不错的选择 URL ...

  8. Heritrix3.0.0启动介绍

    下面开始使用Heritrix3.0.0 进 入CMD(开始->运行),进入Heritrix3.0.0所在目录,我这里是D:/heritrix/heritrix3.0.0/bin,这里 大家截图也 ...

  9. (转)Kettle命令行

    kettle使用命令行来运行ktr和kjb 1:cmd方式运行 1.ktr的运行:运行transformation文件是通过Pan.bat来运行的. 打开cmd命令行窗口,转到Pan.bat所在的目录 ...

  10. Codeforces Round #454 C. Shockers【模拟/hash】

    C. Shockers time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...