描写叙述:

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. 【最大流】hihocoder 1369 : 网络流一·Ford-Fulkerson算法

    http://hihocoder.com/problemset/problem/1369?sid=1328132 参考 https://blog.csdn.net/a1799342217/articl ...

  2. python转exe2

    转载自 xiake200704       最终编辑 xiake200704 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你 ...

  3. 获取所有querystring变量名

    原文发布时间为:2009-12-04 -- 来源于本人的百度文章 [由搬家工具导入] protected void Page_Load(object sender, EventArgs e)    { ...

  4. Ncut matlab 代码bug 修复

    先说平台: win7 x64,matlab 2012a x64,亲测运行. ncut 是个图像切割方法,以后再回顾具体算法吧,网上很多流程的是Jianbo Shi 这位2000年写的,名字应该是:Da ...

  5. Selenium2+python自动化(学习笔记2)

    from selenium import webdriverdriver = webdriver.Ie()driver.get=("http://www.baidu.com")dr ...

  6. python+requests接口自动化测试框架

    1.首先,我们先来理一下思路. 正常的接口测试流程是什么? 脑海里的反应是不是这样的: 确定测试接口的工具 —> 配置需要的接口参数 —> 进行测试 —> 检查测试结果(有的需要数据 ...

  7. Android启动过程深入解析【转】

    转自:http://www.open-open.com/lib/view/open1403250347934.html 当按下Android设备电源键时究竟发生了什么? Android的启动过程是怎么 ...

  8. spingboot 邮件模板发送;

    <!-- 邮件start --><dependency> <groupId>javax.mail</groupId> <artifactId> ...

  9. JavaScript闭包 循环输出i

    html <body> <p>产品一</p> <p>产品二</p> <p>产品三</p> <p>产品四& ...

  10. webapi net 直接更改协议头

    1.直接更改协议头 [HttpGet] public HttpResponseMessage Users() { var sites = new object(); string str = tool ...