当前位置:网站首页>2022.07.15_每日一题
2022.07.15_每日一题
2022-07-31 06:07:00 【诺.い】
43. 字符串相乘
题目描述
给定两个以字符串形式表示的非负整数 num1
和 num2
,返回 num1
和 num2
的乘积,它们的乘积也表示为字符串形式。
注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。
示例 1:
输入: num1 = “2”, num2 = “3”
输出: "6“
示例 2:
输入: num1 = “123”, num2 = “456”
输出: “56088”
提示:
1 <= num1.length, num2.length <= 200
num1
和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();
// 模拟
// 如果其中一个数是 "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();
}
}
边栏推荐
- 简单谈谈Feign
- 【并发编程】ReentrantLock的lock()方法源码分析
- 芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠
- 【云原生】3.3 Kubernetes 中间件部署实战
- Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
- 数据库原理作业2 — JMU
- Project exercise - memorandum (add, delete, modify, check)
- nohup原理
- MySQL笔记下
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
猜你喜欢
事务的传播机制
【云原生】-Docker容器迁移Oracle到MySQL
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
[PSQL] SQL基础教程读书笔记(Chapter1-4)
In-depth analysis of z-index
关于求反三角函数的三角函数值
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
什么是半波整流器?半波整流器的使用方法
批量翻译软件免费【2022最新版】
03-SDRAM: Write operation (burst)
随机推荐
Kubernetes scheduling
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
批量免费文字翻译
【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
第十六章:构建n(5,7)阶素数幻方
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
拓扑排序的两种方法 --- dfs+Kahn
Leetcode952. 按公因数计算最大组件大小
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
【Go】Go 语言切片(Slice)
【云原生】-Docker容器迁移Oracle到MySQL
Gradle剔除依赖演示
【Star项目】小帽飞机大战(七)
【并发编程】ReentrantLock的lock()方法源码分析
Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
shell之条件语句(test、if、case)
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
数据库原理作业2 — JMU