当前位置:网站首页>Leetcode 415. string addition and 43. string multiplication
Leetcode 415. string addition and 43. string multiplication
2022-07-28 00:33:00 【Salted fish learning code】
These two topics are about string Class related exercises . meanwhile , This is also a topic about the operation of large numbers , Let's analyze it !
List of articles
String addition
difficulty Simple OJ link

Let's take an example 2 To explain this idea :
First , We use two subscripts to point to the last position of two strings . Then add these two numbers 6+7=13, So here we are going to enter 1.
And then 3 Insert the header into a new string . Then move the subscript forward one bit .
5+7=12,12 Plus the number on the carry 12+1=13,13 Then you have to carry , Carry or 1. We put 3 Insert the header into the new string , Then the subscript moves forward one bit .
here , A string ends . But it's not over yet , We regard the remaining positions as 0. That is to say 4+0+1( carry )=5.5 Less than 10 There is no need to carry , Let's change the carry to 0. And then we put 5 Insert the header into the new string .
At this point, the number of both strings ends , Is the real end .
that , Let's look at the writing of this problem :
First , We need to set the carry to 0, Because there is no carry when two numbers are added at the beginning . here , Why use the trinocular operator ? as a result of : There may be a string that ends first , If it's over , If we still go to get the position there, we will cross the border . So if one ends first , We'll give it directly 0. And here we subtract one character 0, If we don't subtract characters 0, So we take characters and numbers . image ’1’ Of ASCII The value is 49, And the number we need is 1, So subtract one character 0.
If the sum is greater than 9, Then let's set the carry to 1, If not, change to 0.
Then we insert each character header . At this time, we also need to put characters 0 Add it .
But there is still a problem : Is that if ’9’+'1’ This string , According to the above formula 9+1=10, It is greater than 9 Of , Then subtract 10,ret=0,carry=1. Alphabet character 0 Insert into the string , then end1–,end2–, The cycle is over . At this time, there is only one in the string 0, No, 1. So we have to judge again .
But the time complexity of each head insertion is O(N^2), So the time will be very slow . We can write like this :
Let's directly insert , When all the numbers are inserted . Let's turn it all over again . This time complexity is O(N).
String multiplication
difficulty Simple OJ link 
Thinking process :
Let's take the following data as an example :
According to multiplication we learned in primary school .47=28, We are going to put 8 leave , Then enter 2. But here , Let's not carry . But the 28 Keep it . Back 37=21 Don't carry and keep it . All calculations are not carried .
then , We add up all the numbers in each column .
here , Let's go back and forth , Carry one by one .
Let's start step by step :
First step : If one of these two strings is an empty string , An empty string is returned directly .
The second step : Open up an array , Accumulate the data each time into the array .
Here we first calculate the length of two strings m,n. And how big should this array be ? for instance :100100=10000, Multiply two three digits , The smallest is 5 digit .999999=998001, Multiply two three digits , The biggest thing is 6 digit . therefore , We will open up m+n Space is enough to put . Because the smallest is m+n-1, The biggest is m+n, Then we initialize the space inside 0.
The third step : The numbers of two strings are multiplied by each bit from the back to the front , Then add it to the array .
Some students here may not know why i+j+1?
This is the subscript position of the two strings .
This is the display of the subscript position of the data in the array . We see 4 * 7=28,28 In the array, the subscript is 6 The place of , At this time i by 3,j by 2, therefore 2+3+1=6. image 3*7=21, In the array, the subscript is 5 The place of , here i by 2,j by 2, therefore 2+2+1=5. The same is true for others .
Step four : Start each carry of the data in the array .
Step five : Insert the data in the array into the new string from the beginning to the end .
Some students may not know what this means ? This is to judge whether there is data at the beginning of the array . If it starts with 0, Description no carry to the beginning . We just need to subscript from the array 1 Where the tail begins . If not 0, It means that there is carry to the beginning , We need to subscript from the array to 0 Where the tail begins .
Okay , Let's finish these two questions . If you think it helps , You can like it and collect it . Thank you. !!!
边栏推荐
- adb路径不能包含2空格remote couldn‘t create file: Is a directory
- 【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
- 永州二恶英实验室建设细节查看
- Is it amazing to extract text from pictures? Try three steps to realize OCR!
- 相应通道无电压但ADC的值却在大幅变化且不等于0的可能原因
- Precautions for site selection of Yongzhou clean animal laboratory
- MFC提示this application has requested the runtime to terminate it in an unusual way editbox框已经删了还在使用
- Read one line of string input each time (to be supplemented)
- Description and analysis of main parameters of R language r native plot function and lines function (type, PCH, CEX, lty, LWD, col, xlab, ylab)
- [meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development
猜你喜欢

30余年的元宇宙,为我们带来了什么?

【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发

startUMl

The server is poisoned - the dish is the original sin

Promoting cloud network integration and building a digital economy: Intel unveiled the 5th Digital China Construction Summit - cloud ecosystem Conference

冲量在线出席2022数据要素安全流通论坛—政务领域专场,助力行业政务大数据建设创新发展

MATLAB | MATLAB地形生成:矩形迭代法 · 傅里叶逆变换法 · 分形柏林噪声法

Remote monitoring of pump station

HarmonyOS 3纯净模式可限制华为应用市场检出的风险应用获取个人数据

从第二层到第三层
随机推荐
Shell(3)
Basic elementary function
y79.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(十)
Promoting cloud network integration and building a digital economy: Intel unveiled the 5th Digital China Construction Summit - cloud ecosystem Conference
千万播放竟有通用公式?B站被小看的爆款机会!
Redis-事务与乐观锁
Analysis and solution of errors in symbols uploading when baget manages packages
Understand the parental delegation model
Mqtt---mqtt.fx client software
[leetcode] 547. Number of provinces (medium)
In the third week of July, the list of feigua data station B up main ranking list was released!
几行代码轻松实现对于PaddleOCR的实时推理,快来get!
点分治解析
JS event propagation capture stage bubbling stage onclick addeventlistener
The second uncle cured my spiritual internal friction and made me angry out of station B
Applet helps smart home ecological platform
Introduction to thesis writing | how to write an academic research paper
Description and analysis of main parameters of R language r native plot function and lines function (type, PCH, CEX, lty, LWD, col, xlab, ylab)
The latest ijcai2022 tutorial of "figure neural network: foundation, frontier and application"
How to realize fast recognition of oversized images