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 ...
随机推荐
- 全球第一张中文网络协议分析图——By 成都科来软件
网上内容比较全面的网络协议图并不是很多,这些网络协议图大多只遵循OSI,对于TCP/IP基本不支持,有些协议图表示也不够准确.另一方面,现在网上能找到的协议图全都是英文版本,使用起来不是很方便.国内的 ...
- 【JUnit】junit4的几个assert方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhtao01/article/details/27858225 在静态类junit.framewor ...
- DateTimeTypeHandler
mysql-timestimp-------model-DateTime(jodatime) public class DataTimeTypeHandler extends BaseTypeHand ...
- 浅谈HTML文档模式
不知道爱多想的你有没有在编写HTML代码时思考过 <!DOCTYPE html> 或是这一长串看都看不懂的 <!DOCTYPE HTML PUBLIC "-//W3C//D ...
- Delphi 正则表达式语法(7): 匹配转义字符
Delphi 正则表达式语法(7): 匹配转义字符 // ? 号的意义是匹配 0-1 次, 如果需要匹配 ? 怎么办 var reg: TPerlRegEx; begin reg := TPe ...
- Codeforces Round #305 (Div. 2)
C. Mike and Frog 题意:有一只青蛙和一朵花,分别高度为h1.h2,每浇一次水,h1=(x1*h1+y1)mod m,h2=(x2*h2+y2)mod m.求最少浇多少次后h1=a1,h ...
- Python框架之Tornado(请求阶段)
上图是tornado程序启动以及接收到客户端请求后的整个过程,对于整个过程可以分为两大部分: 启动程序阶段,又称为待请求阶段(上图1.2所有系列和3.0) 接收并处理客户端请求阶段(上图3系列) 简而 ...
- Python 字符串连接问题归结
一.概述 Python 字符串连接场景较为普遍.由于编者对 Java 等语言较为熟悉,常常将两者语法混淆. 加之,Python 语法较为灵活.例如,单单实现字符串连接,就有数种方法.在此,一并归结! ...
- Django学习笔记之Ajax入门
AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JS ...
- PHP-FPM 设置多pool、配置文件重写
重写配置文件 1.清空php配置文件 命令:> /usr/local/php/etc/php-fpm.conf 2.重新写入php-fpm配置 命令:vim /usr/local/php/etc ...