当前位置:网站首页>【LeetCode】43. String multiplication
【LeetCode】43. String multiplication
2022-07-01 15:50:00 【Uaena_ An】
43. String multiplication

🧸 Reading questions
Give two strings , Multiply to get a string , This problem can't be solved by plastic surgery , Because the later test examples will be very large , plastic , Long plastic surgery can't be installed !
🧸 Ideas
To multiply by bits , Save in string , Then add the strings obtained by the product , The final result is stored in the string in turn ,
🧸 Code
class Solution {
public:
string addStrings(string& count, string& sum)
{
//end1,end2 Add from the tail of two numbers ,carry Record carry
int end1 = count.size() - 1, end2 = sum.size() - 1, carry = 0;
string ret;// Record the sum of two numbers added
while (end1 >= 0 || end2 >= 0)
{
// If end1 Greater than 0 shows count There are also a number , be x1 take end1 The value of a , Otherwise take 0
int x1 = 0;
if (end1 >= 0)
{
x1 = count[end1] - '0';
--end1;
}
int x2 = 0;
if (end2 >= 0)
{
x2 = sum[end2] - '0';
--end2;
}
//add Record the sum of two numbers added , Plus carry , The first carry is 0
int add = x1 + x2 + carry;
// If add Greater than 9, It indicates that carry is required , also add To subtract 10
// And enter the position 1 , Otherwise, enter the position 0, Otherwise, the carry will retain the result of the last carry , There will be errors
if (add > 9)
{
carry = 1;
add -= 10;
}
else {
carry = 0;
}
// Insert the remainder of the carry character at the end of ret in , Turn it over later , To improve efficiency
ret += add;
}
// Come here , It means that both numbers have been added , But there is also carry 1 position , Insert the tail
if (carry != 0)
{
ret += carry;
carry = 0;
}
// Flip
reverse(ret.begin(), ret.end());
// Convert to numeric characters
for (auto& e : ret)
{
e += '0';
}
return ret;
}
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0")
{
return "0";
}
string count = "0";// Record the sum
int end1 = num1.size() - 1, end2 = num2.size() - 1;
// Multiply by place ,num2 every * num1 All bits
for (int i = end2; i >= 0; --i)
{
string sum;// Record product
int carry = 0;// Record carry
// When j = i when , explain j Here you are . Is the last multiplication , Unwanted +0
// When j-- after j It's on the tenth , Multiply the number in the tens by other numbers , Directly in the back +1 individual 0, And so on
for (int j = end2; j > i; --j)
{
sum.push_back(0);
}
//i Control is num2 Flashback every bit of traversal
int x2 = num2[i] - '0';
// The following is to use x2 * num1 every
for (int j = end1; j >= 0; --j)
{
int x1 = num1[j] - '0';
int ret = x1 * x2 + carry;// Multiply two numbers + carry
sum += ret % 10;//sum Save a bit , Tail insertion
carry = ret / 10;//carry Save ten
}
// When two numbers are multiplied Rounding and numbers , You need to Carry tail interpolation
while (carry != 0) {
sum += carry % 10;
carry /= 10;
}
// After tail insertion Flip strings
reverse(sum.begin(), sum.end());
// Convert a string into a numeric string
for (auto &e : sum) {
e += '0';
}
count = addStrings(count, sum);
}
return count;
}
};
🧸 Decoding code
The interpretation code has been written in the code comments !
make blind and disorderly conjectures
This problem is done 2 Hours , Read the explanation of the question , Understand the solution step by step , Then I tried to write , Half understood , I will do it again several times in the future
come on. I wish you get what you want offer!
边栏推荐
- 基于PHP的轻量企业销售管理系统
- Advanced cross platform application development (24): uni app realizes file download and saving
- 自动、智能、可视!深信服SSLO方案背后的八大设计
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
- 马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
- 药品溯源夯实安全大堤
- Connect the ABAP on premises system to the central inspection system for custom code migration
- The latest NLP game practice summary!
- Trace the source of drugs and tamp the safety dike
猜你喜欢

STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律

新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果

S32K1xx 微控制器的硬件設計指南

【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》

HR interview: the most common interview questions and technical answers
![Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]](/img/ea/8c9f716717bc08f2e563c577738ec8.png)
Stm32f411 SPI2 output error, pb15 has no pulse debugging record [finally, pb15 and pb14 were found to be short circuited]
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all

Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
![[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb](/img/ec/fa51b21468708609f998de1b2b84fe.jpg)
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb

基于PHP的轻量企业销售管理系统
随机推荐
An intrusion detection model
phpcms后台上传图片按钮无法点击
Pico, can we save consumer VR?
MySQL backup and restore single database and single table
华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
工厂高精度定位管理系统,数字化安全生产管理
Advanced cross platform application development (24): uni app realizes file download and saving
跨平台应用开发进阶(二十四) :uni-app实现文件下载并保存
你TM到底几点下班?!!!
说明 | 华为云云商店「商品推荐榜」
SAP S/4HANA: 一条代码线,许多种选择
Rhcsa fourth day operation
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
[target tracking] |stark
微信小程序03-文字一左一右显示,行内块元素居中
做空蔚来的灰熊,以“碰瓷”中概股为生?
[one day learning awk] conditions and cycles
Deep operator overloading (2)
What is the forkjoin framework in the concurrent programming series?
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation