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

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

 class Solution {
public String multiply(String num1, String num2) {
int m = num1.length(), n = num2.length();
int[] pos = new int[m + n]; for(int i = m - 1; i >= 0; i--) {
for(int j = n - 1; j >= 0; j--) {
int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
int p1 = i + j, p2 = i + j + 1;
int sum = mul + pos[p2]; pos[p1] += sum / 10;
pos[p2] = (sum) % 10;
}
} StringBuilder sb = new StringBuilder();
for(int p : pos)
if(!(sb.length() == 0 && p == 0))
sb.append(p);
return sb.length() == 0 ? "0" : sb.toString(); }
}

43. Multiply Strings (大数乘法)的更多相关文章

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

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

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

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

  3. leetcode 43 Multiply Strings 大数相乘

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

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

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

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

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

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

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

  7. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】43. Multiply Strings

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

  9. 43. Multiply Strings 字符串表示的大数乘法

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

随机推荐

  1. mac mysql提示mysql.sock'

    Warning: World-writable config file '/Applications/XAMPP/xamppfiles/etc/my.cnf' is ignored ERROR 200 ...

  2. [daily][mirror][daemonlogger][tc] 我想把一个网卡(port A)的流量,镜像到虚拟机的一个网卡(port VA)上去

    iptables tee 模块 https://blog.gnuers.org/?p=740 http://blog.csdn.net/wesleyflagon/article/details/385 ...

  3. 关于struts中的表单元素- Form bean not specified on mapping for action: "helloa.do"报错

    今天测试struts时仿照书上写了一个小的表单提交代码 <html:form action="helloa.do" method="post"> & ...

  4. nodejs 学习四 处理回调地狱

    面对下面回调,你面对这样代码,你心里难道不百万只羊驼吗? nodejs 提供了util.promisify方法,来解决这类问题.(类似es6 种是提供了Promise的方法). const fs = ...

  5. 【SQL】SQL Between用法

  6. explain之key_len计算

    通常在优化SQL查询的时候,我们都会使用explain分析SQL执行计划,通常来说当用到组合索引的时候我们如何判断索引完全用上呢?当然高手看看表结构及SQL语句就知道到底用到了几个字段,对于不熟悉的同 ...

  7. 看大师解说Android高速开发框架EasyAndroid

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010966622/article/details/37601789 前几天做了小应用.感觉小有成就 ...

  8. linux命令学习:PATH and LDFLAGS and CFLAGS

    CFLAGS 表示用于 C 编译器的选项, CXXFLAGS 表示用于 C++ 编译器的选项. 这两个变量实际上涵盖了编译和汇编两个步骤.     先来看几个相关的环境变量:PATH.LDFLAGS. ...

  9. OpenFace的一些了解

    1.OpenFace内4个样例代码 配置学习了两个 其一: Ubantu 基本命令 Docker 安装方式.发布网站方式.查看验证安装结果命令 Openface 基本demo 实现方式.和基本原理 其 ...

  10. 【剑指offer】两个链表的第一个公共结点

    一.题目: 输入两个链表,找出它们的第一个公共结点. 二.思路: 思路一:模拟数组,进行两次遍历,时间复杂度O(n2) 思路二:假定 List1长度: a+n  List2 长度:b+n, 且 a&l ...