描写叙述:

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. poj 3053 优先队列处理

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 39029   Accepted: 12681 De ...

  2. Linux下对superblock的理解

    对superblock的理解首先从partition structure的结构开始: 开始的,总的来说,block这个概念好理解..下面就是对super block的理解了Super block即为超 ...

  3. 【08】node 之 fs文件

    var fs = require("fs");//fs 系统文件模块,对文件进行操作.Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如读取文件内容的函数有 ...

  4. 【05】js异步编程理解

    1.概念 同步:一个任务等待前一个任务结束,然后再执行,程序的执行顺序与任务的排列顺序是一致的.同步的.异步:每一个任务有一个或多个回调函数(callback),前一个任务结束后,不是执行后一个任务, ...

  5. Linux下常用的命令记录

    本文章记录我在linux系统下常用或有用的系统级命令,包括软硬件查看.修改命令,有CPU.内存.硬盘.网络.系统管理等命令.但本文不打算介绍生僻命令,也不介绍各个linux发行版下的特有命令,且以后会 ...

  6. 简析JVM GC的根搜索算法

    根搜索算法的基本思路是通过一系列的“GC Roots”的对象作为起始点,从这些节点开始往下搜索,搜索的走过的路径称为引用链,当一个对象到“GC Roots”没有引用链可达时(也就是用图论的话说就是从G ...

  7. 【HDOJ5956】The Elder(树形DP,斜率优化)

    题意:有一棵n个点的有根树,每条边上有一个边权.给定P,从i跳到它的祖先j的费用是距离的平方+P,问所有点中到根节点1的总花费最大值 n<=1e5,p<=1e6,w<=1e2 思路: ...

  8. C#Json转Xml格式数据的方法

    第一种方法 string Xml = "在这里写Json字符串"; XmlDictionaryReader reader = JsonReaderWriterFactory.Cre ...

  9. linux 常用命令: runuser

    rpm: coreutils-8.4-9.el6.x86_64 runuser --help 用法:runuser [选项]... [-] [用户 [参数]... ] Change the effec ...

  10. Java NIO中的FileLock(文件锁)

    FileLock,文件锁. 文件锁在OS中很常见,如果多个程序同时访问.修改同一个文件,很容易因为文件数据不同步而出现问题.给文件加一个锁,同一时间,只能有一个程序修改此文件,或者程序都只能读此文件, ...