043 Multiply Strings 字符串相乘
给定两个以字符串表示的非负整数 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 字符串相乘的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 43. Multiply Strings 字符串相乘
1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...
- Leetcode43. Multiply Strings字符串相乘(大数相乘)
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- 1087 All Roads Lead to Rome (30)(30 分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- 【Lintcode】094.Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- 开发工作之外的修炼Live笔记
“开发工作之外的修炼”这期Live分享了下列话题: [1] 如何发现自己的兴趣 [2] 财富.资源与被动收入 [3] 目标管理 [4] 快速做选择 [5] 时间管理 [6] 如何投资自己 >&g ...
- bzoj 1510 [POI2006]Kra-The Disks——思路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1510 #include<iostream> #include<cstdio ...
- poj1734Sightseeing trip——无向图求最小环
题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...
- JAVA基础知识 String s = new String("ABC") VS String s = "abc"
一: String s = new String("ABC") VS String s = "abc" String s = "abc&q ...
- JavaScript高级程序设计学习笔记第九章--客户端检测
1.能力检测:能力检测的目标不是识别特定的浏览器,而是识别浏览器的能力.(我的理解就是识别浏览器能做什么不能做什么) 2.怪癖检测:目标是识别浏览器的特殊行为.但与能力检测确认浏览器支持什么能力不同, ...
- 0001_第一个测试小程序Login
# -*- coding:utf-8 -*- user = raw_input("Username:") password = raw_input("Password:& ...
- 卸载openjdk安装java后,netbeans启动不了
Cannot find java. Please use the --jdkhome switch. 默认的jdkhome不存在了,所以重新指定一下就可以了. 修改 /netbeans-8.0/etc ...
- CodeForces 484B Maximum Value (数学,其实我也不知道咋分类)
B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...