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 ...
随机推荐
- oracle入门(3)——oracle服务解释
[本文介绍] oracle不像mysql,安装后之后一个服务,如果mysql连接不上,打开其服务就行.oracle是有多个服务,哪些服务要开,哪些服务没必要开,对新手来说未必不是一个难点.下面对ora ...
- SAP系统接口方式:
SAP系统接口方式: 1.PI - 信使中间件 (大公司多选择) 数据: SAP- PI- U8 U8- PI- SAPPI 底层用的还是webservice 技术优点:实时性高: 可处理大数据(在调 ...
- 用python简便地抓取刘昊然的写真(17行代码)
17行python代码抓取刘昊然图片之家的写真 用python来爬取网页信息是很简便的.因为它有很多库来帮助我们实现我们想要的功能.本实验用到的库有:requests和bs4中的BeautifulSo ...
- python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】
一.shelve模块 import shelve # 基于pickle模块, d = shelve.open('shelve_test') class Test(object): def __init ...
- Django基础一
Django基础一 Web框架本质 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演 ...
- IO阻塞模型 非阻塞模型
IO阻塞模型(blocking IO) 在linux中,默认情况下所有的socket都是blocking,一个典型的读操作流程大概是这样: 所以,blocking IO的特点就是在IO执行的两 ...
- Java源码解释之Integer.bitCount
Java中的Integer.bitCount(i)的返回值是i的二进制表示中1的个数.源码如下: public static int bitCount(int i) { // HD, Figure 5 ...
- Spring.Net+NHibernate+Castle学习网站
1.刘冬 http://www.cnblogs.com/GoodHelper/archive/2009/10/16/1584243.html 2.学习资料 http://www.cnblogs.co ...
- form:checkboxes radiobutton select用法
<form:checkboxes path="subjects" items="${requestScope.subjects}" element=&qu ...
- cdoj1328卿学姐与诡异村庄
地址:http://acm.uestc.edu.cn/#/problem/show/1328 题目: 卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others) ...