当前位置:网站首页>括号嵌套问题(建议收藏)
括号嵌套问题(建议收藏)
2022-07-26 03:35:00 【一个山里的少年】
目录
本篇文章主要给大家带来这个括号嵌套问题的解法,对于这种括号嵌套问题可能很多老铁都是非常的头痛。下面给大家带来这一类题目的通解
下面我们来看看第一题字符串解码:
一.字符串解码
1.对应letecode链接:
2.题目描述:
3.解题思路:
遍历字符串:
如果当前字符为数字,即'0' <= s[i] <= '9'将该数字加到子字符串要重复的次数num中去因为数字可能有多位,因此注意原num要先乘以10再加上新加入的数字。
如果当前字符为左括号,即s[i] == '['进行递归,注意每次递归时num都重新赋值过递归的返回值为当前已遍历到的索引i和子字符串ans拿到返回值后将ans乘以num后加到res中,并重置num。
- 如果当前字符为右括号,即
s[i] == ']'返回当前索引i以及在当前递归层的循环过程中生成的子字符串res如果当前遍历到的是字母直接加入到答案中
下面以"3[a]2[bc]"为例:
首先遇到3是一个数字将其放入num当中即可:
下面来到这个左括号进行递归调用从左括号的下一个问题开始进行调用
此时新的递归调用函数来到了a位置 将其加入到ans当中
然后继续往下面进行遍历来到了右括号此时本次递归结束告诉调用它的地方本次递归的结果和遍历到了那个位置
收到了递归完的结果后使用num进行结果的累加并将num置为0从递归返回的下一个位置开始计算。下面的就不再重复其实都是一样的逻辑。
4.对应代码:
class Solution { public: struct Info { Info(string a, int s) { ans = a; stop = s; } string ans; int stop; }; string decodeString(string s) { return process(s, 0)->ans; } Info* process(const string& str, int index) { string ans;//记录答案 int num = 0; //记录出现的次数 while (str[index] != ']' && index < str.size()) { if (isalpha(str[index])) {//字母 ans += str[index++]; //字母直接放入答案中即可 } else if (isdigit(str[index])) { //数字 num = num * 10 + str[index] - '0'; index++; } else { //遇到了'[' Info* ret = process(str, index + 1); ans += addString(num, ret->ans); num = 0; index = ret->stop + 1;//从递归返回的下一个位置计算 } } return new Info(ans, index); } string addString(int num, string str) { string ans; for (int i = 0; i < num; i++) { ans += str; } return ans; } };
下面我们来看一道腾讯的笔试题与上题十分的类似
二.压缩算法
1.对应OJ链接:
2.题目描述:
3.解题思路
本题和上题十分类似具体请看代码:
- 如果当前字符为数字,即'0' <= s[i] <= '9'将该数字加到子字符串要重复的次数num中去因为数字可能有多位,因此注意原num要先乘以10再加上新加入的数字。
- 如果当前字符为左括号,即s[i] == '['进行递归,递归的返回值为当前已遍历到的索引i和子字符串ans拿到返回值并将其加入到当前答案中
4.对应代码:
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @return string字符串 */ struct Info { Info(const string&str,int end) { ans=str; stop=end; } string ans; int stop; }; string compress(string str) { // write code here return process(str,0)->ans; } Info*process(const string&str,int index) { string ans;//记录答案 int num=0; while(index<str.size()&&str[index]!=']') { if(isalpha(str[index])) { ans+=str[index++]; } else if(isdigit(str[index])) { num=num*10+str[index]-'0'; index++; } else if(str[index]=='|') { index++; } else //遇到左括号 { Info*res=process(str,index+1); ans+=res->ans; index=res->stop+1; } } if(num!=0) { ans= addString(ans,num); } return new Info(ans,index); } string addString(string &str,int num) { string ans; for(int i=0;i<num;i++) { ans+=str; } return ans; } };
三.括号的得分
1.对应letecode链接:
2.题目描述:
3.解题思路:
- 遇到左括号直接调用递归函数
- ans+=max(递归函数返回的结果*2,1)
- 返回答案和计算到了那个位置
4.对应代码
class Solution { public: struct Info { Info(int _score, int _stop) : score(_score) , stop(_stop) {} int score;//得分 int stop;//计算到了那个位置 }; int scoreOfParentheses(string s) { return process(s, 0)->score; } Info* process(const string& str, int index) { if (str[index] == ')') {//右括号直接返回 return new Info(0, index); } int ans = 0; //记录得分 while (index < str.size() && str[index] != ')') { Info* res = process(str, index + 1); ans += max(2 * res->score, 1);//进行比较 index = res->stop + 1;//从下一个问题开始计算 delete res; } return new Info(ans, index); } };
边栏推荐
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- 爆肝出了4W字的Redis面试教程
- Offline data warehouse from 0 to 1-stage II software installation
- 赶紧进来!!!用c语言基础知识几十行代码写一个猜数字小游戏
- TCP experimental verification
- [Yuri crack man] brings you easy understanding - deep copy and shallow copy
- Hcip day 8 notes sorting (OSPF routing control, Appendix E, anti ring, shortest path calculation, republication))
- Navicat连接云端服务器上的MySQL数据库
- tf.constant用法
- Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters
猜你喜欢

c语言指针基本知识要点总结(一)

【数学建模-规划模型总结】| MATLAB求解

Where can Lora and nb-iot be used

File upload error: current request is not a multipart request

Use Anaconda to configure the tensorflow of GPU Version (less than 30 series graphics cards)

Dominate the salary list! What industry has a "money" path?

MPLS basic experiment configuration

UDP和TCP可以使用同一个端口吗?

9-20v input peak charging current 3A dual lithium switch type charging chip sc7102

Sersync/lsync real-time synchronization
随机推荐
LoRa无线网关如何快速实现端到云的传输
Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters
[Yuri crack man] brings you easy understanding - deep copy and shallow copy
Hcip day 14
Canvas - ECG design and how to clean the canvas
[MySQL project practical optimization] complex trigger case sharing
What are the methods of array sorting in JS
IDEA2020.3.1不能打开(双击不能打开),但可以通过idea.bat打开。
C language functions (2)
DDD落地的那叫一个高级
How Lora wireless gateway can quickly realize end-to-cloud transmission
Leetcode-462. make the array elements equal with the minimum number of moves
Opencv annotates the image (picture frame + writing)
Tf.constant usage
oracle 11g “密码延迟验证”特性
Derivation of linear regression principle
网络模型及协议
Portable power fast charging scheme 30W automatic pressure rise and fall PD fast charging
Canvas - drawing pictures - dynamic drawing production
MPLS基础实验配置








