给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积。
注意:
    num1 和 num2 的长度均小于110。
    num1 和 num2 均只包含数字 0-9。
    num1 和 num2 均不以零开头。
    你不能使用任何内置的大整数库或直接将输入转换为整数。
详见:https://leetcode.com/problems/multiply-strings/description/

Java实现:

class Solution {
public String multiply(String num1, String num2) {
num1 = new StringBuilder(num1).reverse().toString();
num2 = new StringBuilder(num2).reverse().toString();
int[] d = new int[num1.length() + num2.length()];
for (int i = 0; i < num1.length(); i++) {
int a = num1.charAt(i) - '0';
for (int j = 0; j < num2.length(); j++) {
int b = num2.charAt(j) - '0';
d[i + j] += a * b;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < d.length; i++) {
int digit = d[i] % 10;
int carry = d[i] / 10;
sb.insert(0, digit);
if (i < d.length - 1){
d[i + 1] += carry;
}
}
while (sb.length() > 0 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
}
return sb.length() == 0 ? "0" : sb.toString();
}
}

参考:https://www.cnblogs.com/grandyang/p/4395356.html

https://www.cnblogs.com/springfor/p/3889706.html

043 Multiply Strings 字符串相乘的更多相关文章

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

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

  2. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

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

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

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

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

  5. 43. Multiply Strings 字符串相乘

    1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...

  6. Leetcode43. Multiply Strings字符串相乘(大数相乘)

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  7. 43. Multiply Strings字符串相乘

    网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...

  8. LeetCode 043 Multiply Strings

    题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...

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

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

随机推荐

  1. 集群 openfire

    openfire_3.8.2集群配置 测试机4台1.四台机器都安装openfire,随即一台安装mysql,执行openfire_mysql.sql脚本.2.四台机器都配置到同一个mysql机器上(不 ...

  2. codeforces 706B B. Interesting drink(二分)

    题目链接: B. Interesting drink 题意: 给出第i个商店的价钱为x[i],现在询问mi能在多少个地方买酒; 思路: sort后再二分; AC代码: #include <ios ...

  3. [Shell]Tetris Game

    这篇文章主要介绍Shell脚本编写俄罗斯方块的方法,原文来自脚本之家,http://www.jb51.net/article/48926.htm 效果图: 代码: #!/bin/bash # Tetr ...

  4. 【LeetCode】047. Permutations II

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Lintcode】028.Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  6. mysql数据库---编码格式基本操作

    1.查看数据库编码格式 mysql> show variables like 'character_set_database'; 2.查看数据表的编码格式 mysql> show crea ...

  7. AtCoder Regular Contest 072 E:Alice in linear land

    题目传送门:https://arc072.contest.atcoder.jp/tasks/arc072_c 题目翻译 给你一个数组\(D\),然后给你一个操作序列\(d\),每次操作可以将\(D\) ...

  8. POJ3660(foyld闭包问题)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8794   Accepted: 4948 Descr ...

  9. SQL 排序规则 CodeProject

    http://www.cnblogs.com/ifreesoft/p/4259626.html 开发ERP数据维护工具之一 修改SQL Server数据库排序规则 Change Collation   ...

  10. Http协议-URI和资源

    所有东西都有一个标准化的名字,以帮助人们寻找城市中的各种资源.书籍有ISBN号,公交车有线路号,银行账户有账户编码,人有身份证,街道有街道名称.人们告诉图书馆管理员书籍的ISBN号,他即可找出该书籍的 ...