题目描述:

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(String num1, String num2) {
int num1Length = num1.length();
int num2Length = num2.length();
int d1, d2;
int carry = 0;
int temp;
int[] caculate = new int[num1Length + num2Length];
StringBuilder sb = new StringBuilder(); for (int i = num1.length() - 1; i >= 0; i--) {
d1 = num1.charAt(i) - '0';
for (int j = num2.length() - 1; j >= 0; j--) {
d2 = num2.charAt(j) - '0';
caculate[i + j + 1] += d1 * d2;
}
} for (int i = caculate.length - 1; i >= 0; i--) {
temp = (caculate[i] + carry) % 10;
carry = (caculate[i] + carry) / 10;
caculate[i] = temp;
} for (int num : caculate)
sb.append(num); while (sb.length() > 1 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
} return sb.toString(); }
}

  

Java [Leetcode 43]Multiply Strings的更多相关文章

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

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

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

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

  3. LeetCode(43. Multiply Strings)

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

  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(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

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

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

  7. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

  8. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

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

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

随机推荐

  1. The content of element type "sqlMapConfig" is incomplete,

    The content of element type "sqlMapConfig" is incomplete, it must match "(properties? ...

  2. 编译andriod源码出错:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported

    问题:java.lang.UnsupportedClassVersionError: com/google/doclava/Doclava : Unsupported update-java-alte ...

  3. sql with递归

       with temp as ( select Id, UserId, OfficeID, RoleId, DeptId, IsDelete, IsEnd, ParentId from [dbo]. ...

  4. hdu 4740 The Donkey of Gui Zhou(暴力搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...

  5. 团体程序设计天梯赛-练习集L1-010. 比较大小

    L1-010. 比较大小 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆(浙江大学城市学院) 本题要求将输入的任意3个整数从小 ...

  6. ural 1200

    推出公式  然后特判两端  代码其实挺烂      但是有人竟然可以直接暴过去的 ...... #include <cstdio> #include <cstring> #in ...

  7. PHP 7 值得期待的新特性(下)

    这是我们期待已久的 PHP 7 系列文章的第二篇.点此阅读 第一篇本文系 OneAPM 工程师编译整理. 也许你已经知道,重头戏 PHP 7 的发布将在今年到来!现在,让我们来了解一下,新版本有哪些新 ...

  8. 定长内存池之BOOST::pool

    内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.      ...

  9. Kafka操作

    http://blog.csdn.net/xiao_jun_0820/article/details/46831203 http://blog.csdn.net/xcockroach/article/ ...

  10. CF 136A Presents

    http://codeforces.com/problemset/problem/136/A 题意 :就是输入很多数字,第 i 个数字 pi 代表着第 i 个人的礼物给了第 pi 个人,而让你输出的数 ...