当前位置:网站首页>Leetcode-43- string multiplication
Leetcode-43- string multiplication
2022-06-11 21:28:00 【z754916067】
subject

Ideas
- 1 <= num1.length, num2.length <= 200, That means there may be 200 Multiplication of digits , That is, there is currently no data type that can store these numbers , Therefore, it cannot be converted into numbers and multiplied directly .
- It's good if you add it up , Record whether to carry , Just a few of you , But the multiplication is a little troublesome …
- Painstakingly finished , Think I'm retarded , Let's go and have a look , I feel that the code of the problem solution is much simpler than mine , But it is also done by vertical thinking .
Code
public String multiply(String num1, String num2) {
if(num1.equals("0") || num2.equals("0")) return "0";
LinkedList<String> ll = new LinkedList<>();
// Record the number multiplied by each round hold num1 Put it on top. num2 Put it underneath.
for(int i=num2.length()-1;i>=0;i--){
ll.add(mm(num1,num2.charAt(i)));
}
// After recording Start adding up the numbers
String ans = calute(ll);
return ans;
}
// Record a long string The result of multiplying a short string with only one bit
public String mm(String num1,Character c){
StringBuilder sb = new StringBuilder();
int left = (int)c-'0';
// carry
int flag=0;
for(int i=num1.length()-1;i>=0;i--){
Character temp = num1.charAt(i);
int right = (int)temp - '0';
// Turn to numbers
int ans = left*right+flag;
// If at this time i For the last one Then you can directly reverse and add
if(i==0){
// If ans by 20 30 Then only 2 3 So we have to deal with it separately
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();
}
// Get the final result
public String calute(LinkedList<String> ll){
int index=0;
// Add a few zeros to the right
int right = ll.size()-1;
StringBuilder sb = new StringBuilder();
// To expand each bit in the linked list to the current length
// You can add while expanding Record the final answer
String ans="";
for(int i=ll.size()-1;i>=0;i--){
// Take out the string
String temp = ll.get(i);
// Add a few zeros to the left
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 {
// take ans And sb.toString() Add up
ans = ad(ans,sb.toString());
}
// ans_array[index++] = sb.toString();
sb.delete(0, sb.length());
right--;
}
return ans;
}
// Calculate two numbers of the same number String Add up
public String ad(String nums1,String nums2){
StringBuilder sb = new StringBuilder();
// carry
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){
// If ans by 20 30 Then only 2 3 So we have to deal with it separately
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();
}
边栏推荐
- LabVIEW控制Arduino实现红外测距(进阶篇—6)
- Codeforces Round #742 (Div. 2) F. One-Four Overload
- Website online customer service system Gofly source code development log - 2 Develop command line applications
- 八、BOM - 章节课后练习题及答案
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
- How to Load Data from CSV (Data Preparation Part)
- One article to show you how to understand the harmonyos application on the shelves
- Using the sap ui5 cli command line tool to build and run SAP ui5 applications
- Goto statement of go language
- Apache local multi port configuration
猜你喜欢
![Analysis on the development history and market development status of China's system integration industry in 2020 [figure]](/img/3c/b53c2a3f59ff6784f128cb98a5a7a2.jpg)
Analysis on the development history and market development status of China's system integration industry in 2020 [figure]

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)

2021 Niuke multi school 5 double strings
![[advanced C language] integer storage in memory](/img/a5/6c7df3b8f427fe724922a6b853516f.png)
[advanced C language] integer storage in memory

Syntax of SQL

Software test plan

Network security Kali penetration learning introduction to web penetration using MSF penetration to attack win7 host and execute commands remotely

Deriving Kalman filter from probability theory

A collection of commonly used open source data sets for face recognition

js对返回的数据的各种数据类型进行非空判断。
随机推荐
12 golden rules of growth
[Part 15] use and basic principle of forkjoinpool [key]
线性表的链式存储结构
BZOJ3189 : [Coci2011] Slika
Notes on the preload method of Gorm
實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
Educational Codeforces Round 114 (Rated for Div. 2) D
Supplementary questions for the training ground on September 11, 2021
Leetcode-76- minimum covering substring
字符串复制函数
LeetCode-76-最小覆盖子串
Tensorflow 2. X Getting Started tutorial
The gateway starts other microservices first. When the gateway is started, the gateway cannot be started and there is no exception log; Start the gateway first, and all services can be started normall
Software test plan
Apache local multi port configuration
[Part 16] copyonwritearraylist source code analysis and application details [key]
[advanced C language] integer storage in memory
Cs144 lab0 lab1 record
Database daily question --- day 9: salesperson
Codeforces Round #742 (Div. 2) F. One-Four Overload