Java for LeetCode 043 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.

解题思路一:

BigInteger!!! JAVA实现如下:

 static public String multiply(String num1, String num2) {
    	java.math.BigInteger bi1=java.math.BigInteger.valueOf(0);
    	for(int i=0;i<num1.length();i++){
    		bi1=bi1.multiply(java.math.BigInteger.valueOf(10));
    		bi1=bi1.add(java.math.BigInteger.valueOf(num1.charAt(i)-‘0‘));
    	}
    	java.math.BigInteger bi2=java.math.BigInteger.valueOf(0);
    	for(int i=0;i<num2.length();i++){
    		bi2=bi2.multiply(java.math.BigInteger.valueOf(10));
    		bi2=bi2.add(java.math.BigInteger.valueOf(num2.charAt(i)-‘0‘));
    	}
        return bi1.multiply(bi2).toString();
    }

360 ms Accepted,值得注意的是系统不会自动导入math包,需要在声明时添加,不过使用BigInteger总有种作弊的赶脚

时间: 2024-10-10 14:44:18

Java for LeetCode 043 Multiply Strings的相关文章

LeetCode 043 Multiply Strings

题目要求: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. 分析: 参考网址:http://blog.csdn.net/pickless/article/details/9235907 利用竖式的思想,

【LeetCode】Multiply Strings

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. 我借鉴了JackBauer的一些思想,将乘积逆序存放在int数组result中. 记num1当前为第ind1位(个位为0),num2当前为ind2位,则

Java [Leetcode 43]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. 解题思路: 设置数组记录单个位置相乘的结果,最后负责相加进位. 代码如下: public class Solution { public String multiply(St

LeetCode (19) 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. 对两组非负数字进行相乘,使用数组表示数字,且题目中说明数组很大,因此,因此不能直接将数组转换成数字相乘.这道题目是要求自己构造乘法的思路,需要注意的地方主要为进位的处理. 解题

[LeetCode#43]Multiply Strings

Problem: 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. Analysis: The naive solution of this problem is to following the routine of multipli

【leetcode】Multiply Strings(middle)

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. 思路:直观思路,就是模拟乘法过程.注意进位.我写的比较繁琐. string multiply(string num1, string num2) { vector<int> v1, v

leetcode之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. 这道题的意思是字符串表示的数字相乘. 没啥好说的直接付代码. public String multiply(String num1, String num2) { BigInteger

Java for LeetCode 205 Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.

[C++]LeetCode: 69 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. Answer 1: 基础法 大整数乘法 根据小学手算乘法的规则,我们把每一位相乘,得到一个没有进位的临时结果,如图中间的一行红色数字就是临时结果,然后把临时结果从低位起一次进位.