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 (字符串乘法)的更多相关文章

  1. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  2. 【LeetCode每天一题】Multiply Strings(字符串乘法)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  3. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  4. [leetcode]43. Multiply Strings高精度乘法

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  5. leetcode 43. Multiply Strings(高精度乘法)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  6. Multiply Strings(字符串乘法模拟,包含了加法模拟)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

  8. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  9. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

随机推荐

  1. I2C通信

    项目之前研究了I2C通信协议的实现,完成FPGA对视频解码芯片SAA7111A的初始化配置,设计实现了I2C主机对从机(SAA7111A)32个寄存器的写操作,因此只简单实现了I2C的写时序. 这次重 ...

  2. Mahout介绍-炼数

    Mahout的中文含义:象夫

  3. go——切片

    切片(slice)可以看作一种对数组的包装形式,它包装的数组为该切片的底层数组.反过来讲,切片是针对其底层数组中某个连续片段的描述,下面的代码声明了一个切片类型的变量: var ips = []str ...

  4. Java基础知识陷阱(一)

    本文发表于本人博客. 事隔好多年了,重新拿起来Java这门语言,看似熟悉其实还很陌生,想想应该梳理下顺便提高下自己.这次先来看看Java里面的String这个对象. 经典的先看下面一段代码,请问最终创 ...

  5. poj1696 Space Ant

    地址: 题目: Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4295   Accepted: 2697 ...

  6. Java中的静态方法是什么?

    静态方法是使用公共内存空间的,就是说所有对象都可以直接引用,不需要创建对象再使用该方法. 例如,我创建一个类,里面有一个静态方法: class Test{ public static int z(in ...

  7. 解决Webpack 安装sass时出现的错误

    webpack环境下,加载css需要 css-loader 和 style-loader. css-loader:使用类似@import和url(...)的方法实现 require的功能: style ...

  8. Django学习笔记之Django中间件

    准备 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装饰 ...

  9. ijkplayer实现IMediaDataSource

    由于ijkplayer不能识别android.resource类型的资源在播放raw中的文件的时候用IjkMediaPlayer不能正常播放,实现IMediaDataSource为IjkMediaPl ...

  10. html5 canvas js(时钟)

    <!doctype html> <html> <head> <title>canvas</title> </head> < ...