当前位置:网站首页>LeetCode-43-字符串相乘
LeetCode-43-字符串相乘
2022-06-11 21:23:00 【z754916067】
题目

思路
- 1 <= num1.length, num2.length <= 200,就是说可能会有200位数的相乘,即目前没有数据类型能够存储这些数,故不能转换成数字直接进行相乘。
- 如果是相加的话还挺好做的,记录进不进位,进几位就好了,但相乘的话感觉有点麻烦…
- 煞费苦心写完了,觉得我是个弱智,润去题解看看,感觉题解确实代码比我简洁多了,但也是竖式思想做的。
代码
public String multiply(String num1, String num2) {
if(num1.equals("0") || num2.equals("0")) return "0";
LinkedList<String> ll = new LinkedList<>();
//记录每轮相乘的数 把num1放在上面 num2放在下面
for(int i=num2.length()-1;i>=0;i--){
ll.add(mm(num1,num2.charAt(i)));
}
//记录好了之后 开始把各位数相加
String ans = calute(ll);
return ans;
}
//记录一个长字符串 和只有一位的短字符串相乘的结果
public String mm(String num1,Character c){
StringBuilder sb = new StringBuilder();
int left = (int)c-'0';
//进位
int flag=0;
for(int i=num1.length()-1;i>=0;i--){
Character temp = num1.charAt(i);
int right = (int)temp - '0';
//转成数字
int ans = left*right+flag;
//如果此时i为最后一位 则直接颠倒后添加即可
if(i==0){
//如果ans为20 30 则只会剩2 3所以要另外处理
if(ans>=10){
StringBuilder ans_str = new StringBuilder(String.valueOf(ans));
sb.append(ans_str.reverse().toString());
}else sb.append(ans);
}
else {
if(ans>=10){
flag = ans / 10;
ans = ans % 10;
}else {
flag=0;
}
sb.append(ans);
}
}
return sb.reverse().toString();
}
//得到最终的结果
public String calute(LinkedList<String> ll){
int index=0;
//右加几个零
int right = ll.size()-1;
StringBuilder sb = new StringBuilder();
//给将链表中每一位都扩充到当前的长度
//可以边扩充边相加 记录最后的答案
String ans="";
for(int i=ll.size()-1;i>=0;i--){
//拿出字符串
String temp = ll.get(i);
//左加几个零
int left=0;
if(ans.equals("")) left = ll.get(ll.size()-1).length()-temp.length();
else left = ans.length()-temp.length()-right;
for(int j=0;j<left;j++) sb.append("0");
sb.append(temp);
for(int j=0;j<right;j++) sb.append("0");
if(ans=="") ans = sb.toString();
else {
//将ans与sb.toString()相加
ans = ad(ans,sb.toString());
}
// ans_array[index++] = sb.toString();
sb.delete(0, sb.length());
right--;
}
return ans;
}
//计算两个相同位数的String相加的结果
public String ad(String nums1,String nums2){
StringBuilder sb = new StringBuilder();
//进位
int flag=0;
for(int i=nums1.length()-1;i>=0;i--){
int left = (int)nums1.charAt(i)-'0';
int right = (int)nums2.charAt(i)-'0';
int ans = left+right+flag;
if(i==0){
//如果ans为20 30 则只会剩2 3所以要另外处理
if(ans>=10){
StringBuilder ans_str = new StringBuilder(String.valueOf(ans));
sb.append(ans_str.reverse().toString());
}else sb.append(ans);
}
else {
if(ans>=10){
flag = ans / 10;
ans = ans % 10;
}else {
flag=0;
}
sb.append(ans);
}
}
return sb.reverse().toString();
}
边栏推荐
- 使用 SAP UI5 CLI 命令行工具构建和运行 SAP UI5 应用
- Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day
- Serval and Rooted Tree(CF1153D)-DP
- Work assessment of spectral analysis of Jilin University in March of the 22nd spring -00079
- [Part 13] source code analysis and application details of completabilefuture class [key]
- Ubantu1804 two opencv versions coexist
- BZOJ3189 : [Coci2011] Slika
- ASCII code comparison table
- JVM之堆区
- 【博弈论-绪论】
猜你喜欢

Solution to the problem of PHP strtotime obtaining natural monthly error

Flutter implements the JD address selection component

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

数据库每日一题---第9天:销售员

UML系列文章(29)体系结构建模---模式和框架

Explanation of each column output by explain statement

New product release: domestic single port Gigabit network card is officially mass produced!

BCC tool tool usage

【C語言進階】整型在內存中的存儲

The scale of the global machine vision market continues to rise. Poe image acquisition card provides a high-speed transmission channel for industrial cameras
随机推荐
table_ Display status
应用业务层修改
实现 TabLayout 下标与文字等长,选中字体大小改变
Common file functions
俩月没发过博客了,发一篇证明自己的账号还活着
一个Golang的私有库设置问题
Goto statement of go language
Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day
字符串复制函数
[nk] deleted number of 100 C Xiaohong in Niuke practice match
Leetcode 797. All possible paths
Pyqt5 technical part - cause of the problem that setting the top of the window does not take effect. Setwindowflags() does not take effect after setting the parameters. Solution
第二部分 数据链路层
gateway先启动其他微服务,在启动网关,网关启动不了,且无异常日志;先启动网关,所有服务能正常启动
RANSAC extraction plane (matlab built-in function)
LabVIEW controls Arduino to realize ultrasonic ranging (advanced chapter-5)
Brain cell membrane equivalent neural network training code
Software test plan
A collection of commonly used open source data sets for face recognition
The e-sports Internet cafe uses a 2.5G network card to experience the feeling of flying!