当前位置:网站首页>2022.07.15_Daily Question
2022.07.15_Daily Question
2022-07-31 07:40:00 【No. い】
43. 字符串相乘
题目描述
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式.
注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数.
示例 1:
输入: num1 = “2”, num2 = “3”
输出: "6“
示例 2:
输入: num1 = “123”, num2 = “456”
输出: “56088”
提示:
1 <= num1.length, num2.length <= 200num1和num2只能由数字组成.num1和num2都不包含任何前导零,除了数字0本身.
- 数学
- 字符串
- 模拟
coding
class Solution {
public String multiply(String num1, String num2) {
// 会超范围
// Integer n1 = Integer.valueOf(num1);
// Integer n2 = Integer.valueOf(num2);
// return n1 * n2 + "";
// BigDecimal yyds!! 哈哈哈
// BigDecimal s1 = new BigDecimal(num1);
// BigDecimal s2 = new BigDecimal(num2);
// return s1.multiply(s2).toString();
// 模拟
// If a number of them "0", 则结果必定为 "0"
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
String res = "";
Stack<String> stack = new Stack();
int temp = 0;
for (int i = num1.length() - 1; i >= 0; i--) {
for (int j = num2.length() - 1; j >= 0; j--) {
stack.add(String.valueOf((Integer.parseInt(num1.substring(i, i + 1)) * Integer.parseInt(num2.substring(j, j + 1)) + temp) % 10));
temp = (Integer.parseInt(num1.substring(i, i + 1)) * Integer.parseInt(num2.substring(j, j + 1)) + temp) / 10;
}
if (temp != 0) {
stack.add(String.valueOf(temp));
temp = 0;
}
StringBuffer str = new StringBuffer(stack.size());
while (!stack.isEmpty()) {
str.append(stack.pop());
}
int zeroCnt = num1.length() - 1 - i;
while (zeroCnt > 0) {
str.append('0');
zeroCnt --;
}
res = addStr(res, str.toString());
}
return res;
}
private String addStr(String str1, String str2) {
StringBuffer sum = new StringBuffer();
int temp = 0;
Stack<String> stack = new Stack();
int index1 = str1.length() - 1;
int index2 = str2.length() - 1;
while (index1 >= 0 || index2 >= 0) {
int num1 = 0;
int num2 = 0;
if (index1 >= 0) {
num1 = Integer.parseInt(str1.substring(index1, index1 + 1));
}
if (index2 >= 0) {
num2 = Integer.parseInt(str2.substring(index2, index2 + 1));
}
stack.add(String.valueOf((num1 + num2 + temp) % 10));
temp = (num1 + num2 + temp) / 10;
index1 --;
index2 --;
}
if (temp != 0) {
stack.add(String.valueOf(temp));
}
while (!stack.isEmpty()) {
sum.append(stack.pop());
}
return sum.toString();
}
}
边栏推荐
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- DAY18:XSS 漏洞
- 03-SDRAM: Write operation (burst)
- 2022.07.12_每日一题
- Database Principles Homework 3 — JMU
- 金融租赁业务
- SQLite数据库连接字符串
- One of the small practical projects - food alliance ordering system
- Some derivation formulas for machine learning backpropagation
- Kubernetes调度
猜你喜欢

Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary

2022.07.18_每日一题

Conditional statements of shell (test, if, case)

2022.07.20_每日一题

PCB抄板

How to use repeating-linear-gradient

服务器和客户端信息的获取

Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习

Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
随机推荐
360 push-360 push tool-360 batch push tool
深度学习通信领域相关经典论文、数据集整理分享
DirectExchange switch simple introduction demo
03-SDRAM:写操作(突发)
gstreamer的caps event和new_segment event
SCI写作指南
DAY18: Xss Range Clearance Manual
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
【微服务】Nacos集群搭建以及加载文件配置
Difficulty comparison between high concurrency and multithreading (easy to confuse)
04-SDRAM: Read Operation (Burst)
我开发了一个利用 Bun 执行 .ts / .js 文件的 VS Code 插件
Third-party library-store
Conditional statements of shell (test, if, case)
bcos简介及自序
One of the small practical projects - food alliance ordering system
【第四章】详解Feign的实现原理
leetcode 406. Queue Reconstruction by Height
剑指offer(一)
Redux state management