LeetCode OJ: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.
给出两个字符串,返回对应数字想乘后的字符串,由于这个字符串可能很大,所以不能采用一般的乘法,这里用的方法是模拟手工的乘法运算,算法
本身很简单,就是当时写的时候有些很小的细节搞错了,找了很久的错。啊啊啊啊啊,要细心啊。代码如下,没什么好说的:
class Solution {
public:
string multiply(string num1, string num2) {
if(num1 == "" || num2 == "") return "";
int steps = ;
int pos = ;
int flag = ;
int val = ;
string result = "";
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
int len1 = num1.length();
int len2 = num2.length();
for(int i = ; i < len1; ++i){
pos = steps;
for(int j = ; j < len2; ++j){
val = (num1[i] - '')*(num2[j] - '') + flag;
if(result.size() <= pos){
result.append(, val% + '');
}else{
val += (result[pos] - '');
result[pos] = val% + '';
}
flag = val/;
pos++;
}
if(flag > )
result.append(, flag + '');
flag = ;
steps++;
}
reverse(result.begin(), result.end());
return result;
}
};
java的api真是恶心,处理字符串处理了半天,最后debug通过,往上一贴竟然是TLE的代码,代码在下面,应该是我对字符串的处理比较耗时间了,其实最好的应该是先讲String转换成List,List里面相对的API的函数多一点,String里面连最普通的reverse函数都没有。先马一下这种做法,有时间了再来写。下面的这个道理上应该没有任何问题,就是字符串处理的太慢了。
public class Solution {
public String multiply(String num1, String num2) {
if(num1.equals("0") || num2.equals("0"))
return (new String("0"));
int step = 0;
int pos = 0;
int carry = 0;
int val = 0;
String result = new String("");
num1 = ReverseStr(num1);
num2 = ReverseStr(num2);
for(int i = 0; i < num1.length(); ++i){ //java的api好乱啊,一会是length一会有是size()
pos = step;
for(int j = 0; j < num2.length(); ++j){
val = (num1.charAt(i)-'0')*(num2.charAt(j)-'0') + carry;
if(pos >= result.length())
result += (char)(val%10 + '0');
else{
val += (result.charAt(pos)-'0');
result = (new StringBuffer(result)).replace(pos, pos + 1, "" + (char)(val%10+'0')).toString();
}
carry = val/10;
pos++;
}
if(carry != 0)
result += (char)(carry +'0');
carry = 0;
step++;
}
return ReverseStr(result); } public String ReverseStr(String str){
return (new StringBuffer(str)).reverse().toString();
} }
LeetCode OJ:Multiply Strings (字符串乘法)的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- 【LeetCode每天一题】Multiply Strings(字符串乘法)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- [leetcode]43. Multiply Strings高精度乘法
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- leetcode 43. Multiply Strings(高精度乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Multiply Strings(字符串乘法模拟,包含了加法模拟)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
随机推荐
- Kettle-1-安装配置
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wl101yjx/article/details/32921691 写在前面一: 数据仓库ETL工具有 ...
- Oracle 11g数据库详解(3)
ORA-14025:不能为实体化视图或实体化视图日志指定PARTITION ORA-14026:PARTITION和CLUSTER子句互相排斥 ORA-14027:仅可以指定一个PARTITION子句 ...
- 005-TCP传输控制协议
一.概述 传输控制协议(英语:Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.在简化的 ...
- CoreThink开发(十)把官方首页轮播替换成HTML5-3D轮播
效果: 资源已经上传到我的下载里边. http://download.csdn.net/detail/u012995856/9587206 1.复制资源文件到CoreThink项目中 corethin ...
- Tomcat 安装、配置与部署
Tomcat的官方网站:http://tomcat.apache.org/,目前最新版本为7.0. Tomcat下载地址: 1.32位:http://mirrors.tuna.tsinghua.edu ...
- 吴超老师课程---Hadoop的分布式集群安装
1.hadoop的分布式安装过程 1.1 分布结构 主节点(1个,是hadoop0):NameNode.JobTracker.SecondaryNameNode 从节点(2个,是 ...
- nginx缓存原理
一.HTTP字段理解 1.Expires: 该字段的http1.0时的规范,值为一个绝对时间的GMT格式的时间字符串,代表缓存资源的过期时间,在这个时点之前即命中缓存. 缺点:服务器返回的时间,可能与 ...
- Linux知识点总结
- 『NiFi 学习之路』入门 —— 下载、安装与简单使用
一.概述 "光说不练假把式." 官网上的介绍多少让人迷迷糊糊的,各种高大上的词语仿佛让 NiFi 离我们越来越远. 实践是最好的老师.那就让我们试用一下 NiFi 吧! 二.安装 ...
- POJ 2253 Frogger (Dijkstra变型)
题意:求点1到点2的路径中,权值最大的那条边,其最小值是多少. 分析:最大值最小化.可以将迪杰斯特拉模板中的松弛操作加以修改,在O(n^2)的时间内解决该问题.其中需要注意的是,dist[i]指的是: ...