描写叙述:

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. bzoj 1228 [SDOI2009]E&D SG函数打表 找规律

    题目链接 Description 桌子上有2n 堆石子,编号为1..2n.将第2k-1 堆与第2k 堆(1 ≤ k ≤ n)为同一组.第i堆的石子个数用一个正整数Si表示.一次分割操作指的是,从桌子上 ...

  2. Python Challenge 第九关

    第九关只有一幅图,上面有一些黑点.网页名字叫:connect the dots.可能是要把这些点连起来. 查看源代码,果然有两个整数集合 first 和 second.并且有个提示:first+sec ...

  3. AC日记——3的幂的和 51nod 1013

    3的幂的和 思路: 矩阵快速幂:   sn-1      3 1        sn   *          = 1     0 1  1 来,上代码: #include <cstdio> ...

  4. 判断图连通的三种方法——dfs,bfs,并查集

    Description 如果无向图G每对顶点v和w都有从v到w的路径,那么称无向图G是连通的.现在给定一张无向图,判断它是否是连通的. Input 第一行有2个整数n和m(0 < n,m < ...

  5. BZOJ—— 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3402 Description     贝茜在和约翰玩一个“捉迷藏”的游戏.     她正要找出所有适 ...

  6. 洛谷——P1098 字符串的展开

    P1098 字符串的展开 题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输 ...

  7. Network | Cookie and Session

    Cookies are arbitrary pieces of data chosen by the web server and sent to the browser. The browser r ...

  8. Eclipse的调试功能(转)(让Eclipse也能有VS的即时窗口那样的即时代码调试功能)

    前言:可以很明确的说明,eclipse也有像vs那样的即时窗口来运行即时代码的功能. 调试的界面如下: 如果要像vs那样的即时调试功能,需要做一些设置,就是Expressions功能. 开通步骤:Wi ...

  9. 安全 -- mysql参数化查询,防止Mysql注入

    参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数(Parameter) ...

  10. Android Studio +MAT 分析内存泄漏实战

    对于内存泄漏,在Android中如果不注意的话,还是很容易出现的,尤其是在Activity中,比较容易出现,下面我就说下自己是如何查找内存泄露的. 首先什么是内存泄漏? 内存泄漏就是一些已经不使用的对 ...