博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ:Multiply Strings (字符串乘法)
阅读量:7235 次
发布时间:2019-06-29

本文共 2990 字,大约阅读时间需要 9 分钟。

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.

 

给出两个字符串,返回对应数字想乘后的字符串,由于这个字符串可能很大,所以不能采用一般的乘法,这里用的方法是模拟手工的乘法运算,算法

本身很简单,就是当时写的时候有些很小的细节搞错了,找了很久的错。啊啊啊啊啊,要细心啊。代码如下,没什么好说的:

1 class Solution { 2 public: 3     string multiply(string num1, string num2) { 4         if(num1 == "0" || num2 == "0") return "0"; 5         int steps = 0; 6         int pos = 0; 7         int flag = 0; 8         int val = 0; 9         string result = "";10         reverse(num1.begin(), num1.end());11         reverse(num2.begin(), num2.end());12         int len1 = num1.length();13         int len2 = num2.length();14         for(int i = 0; i < len1; ++i){15               pos = steps;16             for(int j = 0; j < len2; ++j){17                 val = (num1[i] - '0')*(num2[j] - '0') + flag;18                 if(result.size() <= pos){19                     result.append(1, val%10 + '0');20                 }else{21                     val += (result[pos] - '0');22                     result[pos] = val%10 + '0';23                 }24                 flag = val/10;25                 pos++;  26             }27             if(flag > 0)28                 result.append(1, flag + '0');29             flag = 0;30             steps++;31         }32         reverse(result.begin(), result.end());33         return result;34     }35 };

 java的api真是恶心,处理字符串处理了半天,最后debug通过,往上一贴竟然是TLE的代码,代码在下面,应该是我对字符串的处理比较耗时间了,其实最好的应该是先讲String转换成List,List里面相对的API的函数多一点,String里面连最普通的reverse函数都没有。先马一下这种做法,有时间了再来写。下面的这个道理上应该没有任何问题,就是字符串处理的太慢了。

1 public class Solution { 2     public String multiply(String num1, String num2) { 3         if(num1.equals("0") || num2.equals("0")) 4             return (new String("0")); 5         int step = 0; 6         int pos = 0; 7         int carry = 0; 8         int val = 0; 9         String result = new String("");10         num1 = ReverseStr(num1);11         num2 = ReverseStr(num2);12         for(int i = 0; i < num1.length(); ++i){ //java的api好乱啊,一会是length一会有是size()13             pos = step;14             for(int j = 0; j < num2.length(); ++j){15                 val = (num1.charAt(i)-'0')*(num2.charAt(j)-'0') + carry;16                 if(pos >= result.length())17                     result += (char)(val%10 + '0');18                 else{19                     val += (result.charAt(pos)-'0');20                     result = (new StringBuffer(result)).replace(pos, pos + 1, "" + (char)(val%10+'0')).toString();21                 }22                 carry = val/10;23                 pos++;24             }25             if(carry != 0)26                 result += (char)(carry +'0');27             carry = 0;28             step++;29         }30         return ReverseStr(result);31         32     }33 34     public String ReverseStr(String str){35         return (new StringBuffer(str)).reverse().toString();36     }37     38 }

 

转载于:https://www.cnblogs.com/-wang-cheng/p/4870089.html

你可能感兴趣的文章
Linux 扩展LVM下的Volume Group、Logical Volume
查看>>
mysql备份和恢复
查看>>
再见,2012
查看>>
c语言:输出乘法口诀表
查看>>
Memcached学习笔记 — 第二部分:Memcached服务器安装
查看>>
我的友情链接
查看>>
b/s和c/s架构的理解和区别
查看>>
rsyncd.conf配置详细讲解
查看>>
List<String> 如何用jstl foreach遍历
查看>>
H2 Web Console to In Memory Database – Spring Boot
查看>>
我的友情链接
查看>>
mysql 运维常用命令收录
查看>>
linux第二周作业
查看>>
SSL及OpenSSL使用
查看>>
mybatis批量插入(Oracle)
查看>>
手工玫瑰花折纸
查看>>
python浓缩(5)数字
查看>>
LAMP源码编译安装
查看>>
Linux(RHEL 5)中Bind服务的安装与配置全过程-续
查看>>
git设置对比工具
查看>>