leetcode_Multiply Strings
描写叙述:
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的更多相关文章
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 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 ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
随机推荐
- javaScript 笔记(5) --- jQuery(上)
这节整理整理 iquery.js 相关的内容... 目录 --- jQuery 语法 --- 文档就绪事件 --- jQuery 选择器 --- jQuery 事件 --- jQuery 效果 jQu ...
- SQLServer (2005/2008) 日志清理方法
--数据库日志名称查询 USE DBNAME GO SELECT file_id, name,* FROM sys.database_files; GO------------------------ ...
- utf-8与unicode
举一个例子:It's 知乎日报 你看到的unicode字符集是这样的编码表: I 0049 t 0074 ' 0027 s 0073 0020 知 77e5 乎 4e4e 日 65e5 报 62a5 ...
- COGS 2485. [HZOI 2016]从零开始的序列
2485. [HZOI 2016]从零开始的序列 ★★ 输入文件:sky_seq.in 输出文件:sky_seq.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] ...
- NOI2001食物链
描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人 ...
- jQuery动态创建form表单并提交到后台(携带一定的数据进行页面跳转)
今天遇到这么一个需求,携带一个编号一个名字跳转到另一个JSP页面,直接页面跳转(get携带数据)的话不太安全,于是想到到后台转发一下. 第一种:直接以表单提交方式的进行 JS代码: var form ...
- Scrapy笔记:使用scrapy shell url时出现403错误的解决办法
参考 : http://www.th7.cn/Program/Python/201704/1154208.shtml 原因是网站的防爬虫配置起到了作用 (1):第一种方法是在命令上加上-s USER_ ...
- Web安全-XSS-SQL注入-CSRF
一.XSS 跨站脚本攻击(Cross Site Scripting): 1.指攻击者在网页中嵌入客户端脚本(例如JavaScript), 当用户浏览此网页时,脚本就会在用户的浏览器上执行,从而达到攻击 ...
- cobbler一键部署centos7.4(脚本)
执行脚本之前你需要做四件事 1. 关闭防火墙 2.关闭selinux 3.配置163或者阿里云的 yum源 4.上传centos7.4的镜像如下图 [root@cobbler ~]# cat auto ...
- LeetCode OJ-- Remove Nth Node From End of List
https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ remove倒数第n个节点 一般list remove node的 ...