括号嵌套问题(建议收藏)

x33g5p2x  于2022-07-26 转载在 其他  
字(2.9k)|赞(0)|评价(0)|浏览(524)

一.字符串解码

二.压缩算法

三.括号的得分

本篇文章主要给大家带来这个括号嵌套问题的解法,对于这种括号嵌套问题可能很多老铁都是非常的头痛。下面给大家带来这一类题目的通解

下面我们来看看第一题字符串解码:

一.字符串解码

1.对应letecode链接:
394. 字符串解码 - 力扣(LeetCode)

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.对应代码:

  1. class Solution {
  2. public:
  3. struct Info {
  4. Info(string a, int s) {
  5. ans = a;
  6. stop = s;
  7. }
  8. string ans;
  9. int stop;
  10. };
  11. string decodeString(string s) {
  12. return process(s, 0)->ans;
  13. }
  14. Info* process(const string& str, int index) {
  15. string ans;//记录答案
  16. int num = 0; //记录出现的次数
  17. while (str[index] != ']' && index < str.size()) {
  18. if (isalpha(str[index])) {//字母
  19. ans += str[index++];
  20. //字母直接放入答案中即可
  21. } else if (isdigit(str[index])) {
  22. //数字
  23. num = num * 10 + str[index] - '0';
  24. index++;
  25. } else { //遇到了'['
  26. Info* ret = process(str, index + 1);
  27. ans += addString(num, ret->ans);
  28. num = 0;
  29. index = ret->stop + 1;//从递归返回的下一个位置计算
  30. }
  31. }
  32. return new Info(ans, index);
  33. }
  34. string addString(int num, string str) {
  35. string ans;
  36. for (int i = 0; i < num; i++) {
  37. ans += str;
  38. }
  39. return ans;
  40. }
  41. };

下面我们来看一道腾讯的笔试题与上题十分的类似

二.压缩算法

1.对应OJ链接:
压缩算法_腾讯2020校园招聘-后台_牛客网 (nowcoder.com)

2.题目描述:

3.解题思路

本题和上题十分类似具体请看代码:

  • 如果当前字符为数字,即'0' <= s[i] <= '9'将该数字加到子字符串要重复的次数num中去因为数字可能有多位,因此注意原num要先乘以10再加上新加入的数字。
  • 如果当前字符为左括号,即s[i] == '['进行递归,递归的返回值为当前已遍历到的索引i和子字符串ans拿到返回值并将其加入到当前答案中

4.对应代码:

  1. class Solution {
  2. public:
  3. /**
  4. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
  5. *
  6. *
  7. * @param str string字符串
  8. * @return string字符串
  9. */
  10. struct Info
  11. {
  12. Info(const string&str,int end)
  13. {
  14. ans=str;
  15. stop=end;
  16. }
  17. string ans;
  18. int stop;
  19. };
  20. string compress(string str) {
  21. // write code here
  22. return process(str,0)->ans;
  23. }
  24. Info*process(const string&str,int index)
  25. {
  26. string ans;//记录答案
  27. int num=0;
  28. while(index<str.size()&&str[index]!=']')
  29. {
  30. if(isalpha(str[index]))
  31. {
  32. ans+=str[index++];
  33. }
  34. else if(isdigit(str[index]))
  35. {
  36. num=num*10+str[index]-'0';
  37. index++;
  38. }
  39. else if(str[index]=='|')
  40. {
  41. index++;
  42. }
  43. else //遇到左括号
  44. {
  45. Info*res=process(str,index+1);
  46. ans+=res->ans;
  47. index=res->stop+1;
  48. }
  49. }
  50. if(num!=0)
  51. {
  52. ans= addString(ans,num);
  53. }
  54. return new Info(ans,index);
  55. }
  56. string addString(string &str,int num)
  57. {
  58. string ans;
  59. for(int i=0;i<num;i++)
  60. {
  61. ans+=str;
  62. }
  63. return ans;
  64. }
  65. };

三.括号的得分

1.对应letecode链接:
856. 括号的分数 - 力扣(LeetCode)

2.题目描述:

3.解题思路:

  • 遇到左括号直接调用递归函数
  • ans+=max(递归函数返回的结果*2,1)
  • 返回答案和计算到了那个位置

4.对应代码

  1. class Solution {
  2. public:
  3. struct Info {
  4. Info(int _score, int _stop)
  5. : score(_score)
  6. , stop(_stop)
  7. {}
  8. int score;//得分
  9. int stop;//计算到了那个位置
  10. };
  11. int scoreOfParentheses(string s) {
  12. return process(s, 0)->score;
  13. }
  14. Info* process(const string& str, int index) {
  15. if (str[index] == ')') {//右括号直接返回
  16. return new Info(0, index);
  17. }
  18. int ans = 0;
  19. //记录得分
  20. while (index < str.size() && str[index] != ')') {
  21. Info* res = process(str, index + 1);
  22. ans += max(2 * res->score, 1);//进行比较
  23. index = res->stop + 1;//从下一个问题开始计算
  24. delete res;
  25. }
  26. return new Info(ans, index);
  27. }
  28. };

相关文章