当前位置:网站首页>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 <= 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();
// 模拟
// 如果其中一个数是 "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();
}
}
边栏推荐
猜你喜欢

零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

高并发与多线程之间的难点对比(容易混淆)

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

【云原生】-Docker容器迁移Oracle到MySQL

双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分

DirectExchange switch simple introduction demo

嵌入式系统驱动初级【2】——内核模块下_参数和依赖

外贸网站优化-外贸网站优化教程-外贸网站优化软件

【C语言项目合集】这十个入门必备练手项目,让C语言对你来说不再难学!

搭建zabbix监控及邮件报警(超详细教学)
随机推荐
Run the NPM will pop up to ask "how are you going to open this file?"
In-depth analysis of z-index
机器学习反向传播的一些推导公式
Titanic 预测问题
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
从 Google 离职,前Go 语言负责人跳槽小公司
Gradle remove dependency demo
【Go语言入门教程】Go语言简介
【科普向】5G核心网架构和关键技术
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
【云原生】3.3 Kubernetes 中间件部署实战
文件 - 02 上传文件:上传临时文件到服务器
Analysis of the principle and implementation of waterfall flow layout
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
Database Principles Homework 2 — JMU
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
高并发与多线程之间的难点对比(容易混淆)
4-1-7 二叉树及其遍历 家谱处理 (30 分)
Moment.js common methods
文件 - 05 下载文件:根据文件Id下载文件