题目:

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.

给两个用字符串表示的数字,求它们的乘积。

注意:数字非负,且可以无限大

————————————————————————

之前未考虑数字可以无限大的情况,采用的方法是 将字符串转化为数字,然后相乘,将乘积再次转化为字符串 输出,

最后发现只能通过一部分的测试用例,对于超过long类型表示范围的数字,无法通过。

-----------------------------------------------------

方法思想:根据乘法的演算过程

------------------------------------------

代码:(在牛客网上可以ac,但是leetcode有个用例过不去,超时问题)

public class Solution {
public String multiply(String num1, String num2){
if(num1.equals("0")|| num2.equals("0"))
return "0";
if(num1.charAt(0) == '-' || num2.charAt(0) == '-')
return "false";
int len_num1,len_num2;
len_num1 = num1.length();
len_num2 = num2.length();
String [] sing_mul = new String[len_num2];
int maxLen_sing = 0;
for(int z=0; z<len_num2; z++) //初始化
sing_mul[z] = ""; int times = 0;
//被乘数*乘数 --> 返回是字符串数组sing_mul,每一个元素是某个乘数位*被乘数的结果。
for(int i=len_num2-1; i>=0; i--){
int num2Cur = Integer.parseInt(String.valueOf(num2.charAt(i))); //乘数的某一位
int add = 0;
String temp = "";
//某个乘数位*被乘数 ---> 返回是一个字符串
for(int j=len_num1-1; j>=0; j--){ //遍历被乘数的每一位
int num1Cur = Integer.parseInt(String.valueOf(num1.charAt(j)));
int mulCur = num1Cur * num2Cur + add;
temp += String.valueOf(mulCur%10);
add = mulCur/10;
}
if(add != 0){
temp += String.valueOf(add);
}
StringBuffer sb_temp = new StringBuffer(temp);
StringBuffer temp_rev = sb_temp.reverse();
String str_temp ;
str_temp = temp_rev.toString();
//末尾补充"0";
for (int z=0; z<times; z++)
str_temp += "0";
sing_mul[i] = str_temp;
times ++; //maxLen_sing --> 字符串数组sing_mul中元素的最大长度
if(sing_mul[i].length()>maxLen_sing)
maxLen_sing = sing_mul[i].length();
} //将字符串数组中的所有元素相加。
String result = sing_mul[0];
for(int z=1; z<len_num2; z++){
int len_res = result.length();
//给短的字符串前补充"0".
if(sing_mul[z].length()<len_res){ //字符串前部进行填充
//先反转字符串,补"0",再次反转字符串。
StringBuffer sb_z = new StringBuffer(sing_mul[z]);
String sb_z_str = sb_z.reverse().toString();
for(int w=0; w<(len_res-sing_mul[z].length());w++)
sb_z_str += "0";
StringBuffer sb_temp = new StringBuffer(sb_z_str);
sing_mul[z] = sb_temp.reverse().toString();
}
int add_ = 0;
String temp_str = "";
for(int i=len_res-1; i>=0; i--){
int n1 = Integer.parseInt(String.valueOf(result.charAt(i)));
int n2 = Integer.parseInt(String.valueOf(sing_mul[z].charAt(i)));
int tep = n1+n2+add_;
add_ = tep/10;
temp_str += tep%10;
}
if(add_ != 0){
temp_str += String.valueOf(add_);
}
StringBuffer sb_temp_str = new StringBuffer(temp_str);
result = sb_temp_str.reverse().toString();
}
return result;
}
public static void main(String [] args){
Solution mulStr = new Solution();
String result = mulStr.multiply("9133","0");
System.out.print(result);
}
}

代码解释:

1. num1为被乘数,num2为乘数,用乘数的每一位去乘被乘数,得到一个字符串---》最终会得到字符串数组sig_mul,其中包含乘数位数个字符串

(注意:除了最后一个乘数位,其余的乘数位和被乘数相等得到的字符串应该在末尾补充相应位数的“0”)

2. sig_mul[0]中的字符串是最长的,因此result = sig_mul[0]

3. 每次使用result和sig_mul中的元素相加(1<=i<sig_mul.length)

(注意:每次相加之后的位数都会变化,但是以result的位数为准,因为它始终表示最长的位数)

(注意:每次使用sig_mul中的元素和result相加时,当sig_mul[i]的位数<result的位数时,应该给sig_mul[i]前补充相应位数的“0”,从而使得两个相加的字符串的位数相等,可以进行相加

4. 在这个过程中会频繁的用到字符串reverse。

(因为每次相加或者相乘都是从字符串的末尾开始的,而正确的字符串应该是相乘或者相加结果的反转的字符串)

文中转化的方式是 String  转化 StringBuffer --》利用StringBuffer.reverse() --》StringBuffer 转化String

-----------------------------------------

部分问题未解决,后面更新。

[leetcode]multiply-strings java代码的更多相关文章

  1. LeetCode: Multiply Strings. Java

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

  2. LeetCode: Multiply Strings 解题报告

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

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

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

  4. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  5. 43. Multiply Strings (JAVA)

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

  6. LeetCode:Multiply Strings

    题目链接 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  7. [Leetcode] Multiply strings 字符串对应数字相乘

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

  8. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  9. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  10. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

随机推荐

  1. oracle和mysql对时间与字符串的转换

    1,oracle to_date(#{item.value},'YYYY-MM-DD hh24-mi-ss') to_char(CRERATE_TIME,'YYYY-MM-DD hh24-mi-ss' ...

  2. 【数据预处理】TIMIT语料库WAV文件转换

    1 问题描述 这两天复现代码.先构造数据集,纯净语音.不同噪声.不同SNR的混合语音.其中纯净语音由两部分组成,IEEE corpus和TIMIT. 一开始我用MATLAB中的audioread读取音 ...

  3. P2P通讯原理

    1.简介 当今互联网到处存在着一些中间件(MIddleBoxes),如NAT和防火墙,导致两个(不在同一内网)中的客户端无法直接通信.这些问题即便是到了IPV6时代也会存在,因为即使不需要NAT,但还 ...

  4. (2016.2.2)1001.A+B Format (20)解题思路

    https://github.com/UNWILL2LOSE/object-oriented 解题思路 目标: *首先运算要求实现输入2个数后,输出类似于银行的支票上的带分隔符规则的数字. 代码实现思 ...

  5. Leetcode题库——7.反转整数

    @author: ZZQ @software: PyCharm @file: IntReverse.py @time: 2018/9/16 16:36 要求:整数反转(给定一个 32 位有符号整数,将 ...

  6. HDU 2123 An easy problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2123 Problem Description In this problem you need to make ...

  7. [转帖 cnblog 的news ]技术实力超群的Netflix,为何没有CTO

    技术实力超群的Netflix,为何没有CTO https://news.cnblogs.com/n/581824/ 投递人 itwriter 发布于 2017-11-05 16:12 评论(2) 有1 ...

  8. TCP/IP协议三次握手和四次挥手大白话解说

    前言 昨天晚上被一位师傅问到了TCP/IP的工作机制,心里很清楚三次握手,然而对于四次挥手却忘了,这是大学习里学过的,奋而翻阅书籍和网络对之前所学的做一个温顾,算是夯实自我吧. TCP(Transmi ...

  9. [C/C++] multimap查找一个key对应的多个value

    在multimap中,同一个键关联的元素必然相邻存放.基于这个事实,就可以将某个键对应的值一一输出. 1.使用find和count函数.count函数求出某个键出现的次数,find函数返回一个迭代器, ...

  10. UVA11374_Airport Express

    给一个无向图,有的边是特殊边,最多可以取一条特殊边,求最短路,并且输出路径. 这样考虑,加入所有非特殊边,求出每个点到起点和终点的最短路.然后加入特殊边的时候,如果取当前这条特殊边,那么答案会是两点预 ...