当前位置:网站首页>Leetcode punch in
Leetcode punch in
2022-04-22 07:14:00 【Jack_ joker】
leetcode The first day of punch in
A very simple string deletion problem , Hit multiple pits
Give a string of lowercase letters S, Duplicate deletion selects two adjacent and identical letters , And delete them . stay S Repeat the delete operation on , Until you can't delete . Returns the final string... After all the duplicates have been deleted . The answer is guaranteed to be unique .
Example :
Input :abbaca
Output :ca
To understand and run is to first "bb" Delete , Get a string "aaca", Then judge and delete the adjacent "aa", obtain "ca", because "ca' Adjacent characters do not have the same , So that's the final result .
Programming and commissioning problems
(1) The first is the circulation problem , Only one cycle was judged , The strings obtained by deleting the original adjacent characters are not integrated , Then it is determined again whether there are adjacent characters .
(2) After considering the above problem ,"aaaaaa" The last two of the string "aa" But it's ignored , Or the loop setting is wrong .
Generally speaking, facing this deletion problem , The length of the string changes every time , You need to set up two auxiliary spaces , One is defined as the length of the string before it has not been changed , One is set to the length after change , Then set the cycle condition , If the two lengths are not equal, it means that the final result has not been obtained .
class Solution {
public:
string removeDuplicates(string S) {
int now=S.length();
int next=1;
while(now!=next)// The current string length is not equal to the length after the next removal
{
now=S.length();
for(int i=0;i<now;i++){
if(S[i]==S[i+1])S.erase(i,2);
}
next=S.length();
}
return S;
}
};
leetcode The first logic of problem brushing , When the logic is clear, we can solve the problem , Otherwise, there will be a lot of bug.
版权声明
本文为[Jack_ joker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220607497996.html
边栏推荐
- Digital IC design, after learning Verilog syntax, what else do you need to learn?
- 数字ic设计有前途吗?职业发展,薪资待遇如何?
- Nacos持久化切换配置
- Nacos持久化切换配置
- ASP.NET日常开发随手记------发送邮件
- 数字IC设计自学入门难吗?如何快速入门呢?
- Experiment 4 cycle program design
- Can bus record diagnostic assistant
- 芯片设计怎样准备即将面临的秋季补招和来年的春招?
- How to become an IC Verification Engineer?
猜你喜欢

Application of usbcan card in CAN bus in EOL test system of power battery pack

Nacos持久化切换配置

C#日常开发随手记----解决一个集合引用另一个集合导致一起改变

Alibaba cloud's deployment of tiny RSS

【SVN】Subversion安装使用笔记

Nacos持久化切换配置

mysql的zip安装教程

New year's greetings & wishes to all programmers

Is it difficult to get started with self-study of Digital IC design? How to get started quickly?

Nacos集群配置
随机推荐
STM32 learning record 0005 - JLINK download and debugging
Alibaba cloud's deployment of rsshub stepping on the pit notes
Introduction to IC Analog Layout - learning notes on layout Basics (5)
C#日常开发随手记----解决一个集合引用另一个集合导致一起改变
From spec. to chip_ (Digital IC, analog IC, FPGA / CPLD design process and EDA tools)
送给所有程序员的新年祝福&新年愿望
集成电路模拟版图入门-版图基础学习笔记(一)
Is digital IC design promising? How about career development and salary?
阿里云部署Tiny Tiny RSS踩坑笔记
STM32 timer generates event Tim_ GenerateEvent
微信支付 iframe子页面 无响应
实验室安全考试
leetcode1218:最长定差子序列
Introduction to IC Analog Layout - learning notes on layout Basics (3)
ASP.NET日常开发随手记------iis服务器支持下载apk
ASP.NET日常开发随手记------解析和转换xml
Nacos集群架构
leetcode268:丢失的数字
Is it difficult to get started with self-study of Digital IC design? How to get started quickly?
Nacos cluster configuration