leetcode试题总结<6>

还是栈与队列的题目

385. Mini Parser

Given a nested list of integers represented as a string, implement a parser to deserialize it.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Note: You may assume that the string is well-formed:

  • String is non-empty.
  • String does not contain white spaces.
  • String contains only digits 0-9[- ,].

Example 1:

Given s = "324",

You should return a NestedInteger object which contains a single integer 324.

Example 2:

Given s = "[123,[456,[789]]]",

Return a NestedInteger object containing a nested list with 2 elements:

1. An integer containing value 123.
2. A nested list containing two elements:
    i.  An integer containing value 456.
    ii. A nested list with one element:
         a. An integer containing value 789.
试题大意:这道题让我们实现一个迷你解析器用来把一个字符串解析成NestInteger类。

<span style="font-size:18px;">/** 
 * // This is the interface that allows for creating nested lists. 
 * // You should not implement it, or speculate about its implementation 
 * class NestedInteger { 
 *   public: 
 *     // Constructor initializes an empty nested list. 
 *     NestedInteger(); 
 * 
 *     // Constructor initializes a single integer. 
 *     NestedInteger(int value); 
 * 
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list. 
 *     bool isInteger() const; 
 * 
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer 
 *     // The result is undefined if this NestedInteger holds a nested list 
 *     int getInteger() const; 
 * 
 *     // Set this NestedInteger to hold a single integer. 
 *     void setInteger(int value); 
 * 
 *     // Set this NestedInteger to hold a nested list and adds a nested integer to it. 
 *     void add(const NestedInteger &ni); 
 * 
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list 
 *     // The result is undefined if this NestedInteger holds a single integer 
 *     const vector<NestedInteger> &getList() const; 
 * }; 
 */  
class Solution {  
public:  
    NestedInteger deserialize(string s) {  
        if(s[0] != '[') return NestedInteger(stoi(s));  
        stack<NestedInteger*> stk;  
        NestedInteger* ret = NULL;  
        int idx = 0;  
          
        for(int i=0; i<s.size(); ++i) {  
            if(s[i] == '[') {  
                stk.push(new NestedInteger());  
                if(!ret) ret = stk.top();  
                idx = i + 1;  
            }  
            else if(s[i] == ']' || s[i] == ',') {  
                if(idx != i)  
                    stk.top()->add(NestedInteger(stoi(s.substr(idx, i-idx))));  
                if(s[i] == ']') {  
                    NestedInteger* cur = stk.top();  
                    stk.pop();  
                    if(!stk.empty()) stk.top()->add(*cur);  
                }  
                idx = i + 1;  
            }  
        }  
          
        return *ret;  
    }  
};</span>
总结:这道题字符串中括号存在层层嵌套的情况,所以需要借助栈来记录遍历路径上的每层NestedInteger。大致思路是:遇到'['时,表明即将进入新一层链表,此时构造新的NestedInteger并进栈;遇到']'时,表明该层链表结束,此时出栈并将该层链表通过add函数加入上层链表中。数字在遍历到']'或','时结束,此时将数字构造的NestedInteger通过add函数加入栈顶链表中,处理过程中需要记录数字的起始位置。另外,栈中最好存储指针,能够降低空间复杂度。


341. Flatten Nested List Iterator

Given a nested list of integers, implement an iterator to flatten it.

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

Example 1:
Given the list [[1,1],2,[1,1]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]

Example 2:
Given the list [1,[4,[6]]],

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]

<span style="font-size:18px;">/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
    
    stack <NestedInteger> s;
public:
    NestedIterator(vector<NestedInteger> &nestedList) 
    {
        for (int i = nestedList.size() - 1; i >= 0; --i) 
        {
            s.push(nestedList[i]);
        }
    }

    int next() {
        NestedInteger t = s.top(); 
        s.pop();
        return t.getInteger();
    }

    bool hasNext() {
        while (!s.empty())
        {
            NestedInteger temp = s.top(); 
            if (temp.isInteger()) 
                return true;
            s.pop();
            for (int i = temp.getList().size() - 1; i >= 0; --i) {
                s.push(temp.getList()[i]);
            }
        }
        return false;
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */</span>
总结:首先搞懂题目的意思很重要,题目要求将一个具有内嵌的整数序列平坦化(即去掉嵌套结构,输出一个个的整数)。这里主要借助next和hasNext两个函数实现遍历。主要思路如下:

1.通过类的构造函数将NestedInteger类型的向量里的元素从后往前保存到类的一个栈结构中去。

        2.hasNext首先判断栈是否为空,为空返回false,若不为空,取出栈顶的NestedInteger元素,并判断是否为整数,若为整数,则表示next函数可直接输出该整数,若不为整数,即为一个嵌套序列,那么先保存栈顶嵌套序列,然后pop出栈顶元素,取出这个嵌套结构里的各个元素,再push到栈中去,注意,每一次push的顺序都是从尾到头的顺序。如此反复,即可解析全部嵌套结构,并由next输出。

331. Verify Preorder Serialization of a Binary Tree

题意:前序遍历中,当遇到非空的结点时,记录下结点的值,当结点为空时,用一个特殊值#标记结点。


例如,上例中,二叉树用中序遍历可序列化为字符串"9,3,4,#,#,1,#,#,2,#,6,#,#"。这里的#即代表空节点。

给一串用逗号分开的字符串值,检查它是否是正确的二叉树前序遍历的序列,字符串中每个逗号分隔的值只能是整数或者#,而且假设输入的字符串是合法的,及不会出现"1,,3"的类似情况。

class Solution {
public:
    bool isValidSerialization(string preorder) {
        if(preorder.empty())
            return false;
        istringstream in(preorder);
        vector<string> v;
        string str;
        int d = 0;
        while(getline(in,str,','))
            v.push_back(str);
            
        for(int i = 0; i < v.size()-1; i++)
        {
            if(v[i] == "#")
            {
                if(d == 0)
                    return false;
                --d;
            }
            else
                ++d;
        }
        
        return d != 0 ? false : v.back() == "#";
    }
};
class Solution {
public:
    bool isValidSerialization(string preorder) {
        
        string token;
        stringstream ss;
        ss<<preorder;
        stack<string> st;
        while(getline(ss,token,','))
        {
            if(token=="#")
            {
                while(!st.empty() && st.top() == "#")
                {
                    st.pop();
                    if(st.empty()) return false;
                    st.pop();
                }
                st.push(token);
            }else{
                st.push(token);
            }
        }
        return st.size()==1 && st.top()=="#";
        
    }
};
总结:判断给出的序列是否是二叉树的前序遍历,有两种算法思路:

方法1:

观察先序遍历的结果,当出现叶节点时,就会出现两个"#",因为叶节点没有子节点。此时可以将该叶节点消除,即用一个"#"代替,一层层向上归并消除直至根节点,最终只剩一个"#"。可以用栈来实现该过程。以样例为例来演示下该"归并"过程:

9 <- (入栈)

9,3 <-

9,3,4 <-

9,3,4,# <-

9,3,4,#   # ->  9,3,#

9,3,#,1 <-

9,3,#,1,# <-

9,3,#,1,#  #  ->  9,3,#,#   -> 9,#

9,#,2 <-

9,#,2,#  <-

9,#,2,#,6  <-

9,#,2,#,6,#  <-

9,#,2,#,6,#  #  ->  9,#,2,#,# ->9,#,# ->#

每个元素入栈一次,出栈一次,故时间复杂度为O(n),空间复杂度为O(n)。

方法2:

观察二叉树的前序遍历可以得出如下两个规律:

1. 数字的个数总是比#号少一个

2. 最后一个一定是#号

那么我们加入先不考虑最后一个#号,那么此时数字和#号的个数应该相同,如果我们初始化一个为0的计数器,遇到数字,计数器加1,遇到#号,计数器减1,那么到最后计数器应该还是0。下面我们再来看两个返回False的例子,"#,7,6,9,#,#,#"和"7,2,#,2,#,#,#,6,#",那么通过这两个反例我们可以看出,如果根节点为空的话,后面不能再有节点,而且不能有三个连续的#号出现。所以我们再加减计数器的时候,如果遇到#号,且此时计数器已经为0了,再减就成负数了,就直接返回False了,因为正确的序列里,任何一个位置i,在[0, i]范围内的#号数都不大于数字的个数的。当循环完成后,我们检测计数器是否为0的同时还要看看最后一个字符是不是#号。


173. Binary Search Tree Iterator

题意:构造一个二叉搜索树的迭代器的类,以二叉搜索树的根节点初始化迭代器。调用迭代器类中next函数将返回二叉搜索树中值最小的结点。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class BSTIterator {
    stack<TreeNode*> stk;
public:
    BSTIterator(TreeNode *root) {
        while(!stk.empty())
            stk.pop();
        while(root)
        {
            stk.push(root);
            root = root->left;
        }
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
        return !stk.empty();
    }

    /** @return the next smallest number */
    int next() {
        TreeNode* temp = stk.top();
        stk.pop();
        int val = temp->val;
        temp = temp->right;
        while(temp)
        {
            stk.push(temp);
            temp = temp->left;
        }
        return val;
    }
};

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = BSTIterator(root);
 * while (i.hasNext()) cout << i.next();
 */
思路:返回树中最小结点的值,可以使用栈来实现,初始化迭代器类时,将包括根节点在内的所有左子树的结点push入栈。这样,栈顶元素为二叉搜索树中最小的元素值,next函数中,弹出栈顶元素值并返回,需要注意的是,需要push栈顶结点的右子树的所有左子树结点入栈。

150. Evaluate Reverse Polish Notation

题意:计算逆波兰表达式(后缀表达式)的结果

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> stk;
        for(auto a : tokens)
        {
            if(a.size() == 1 && !isdigit(a[0])) //该字符为运算符
            {
                int num2 = stk.top();
                stk.pop();
                int num1 = stk.top();
                stk.pop();
                switch(a[0])
                {
                    case '+':
                        stk.push(num1 + num2);
                        break;
                    case '-':
                        stk.push(num1 - num2);
                        break;
                    case '*':
                        stk.push(num1 * num2);
                        break;
                    case '/':
                        stk.push(num1 / num2);
                        break;
                }
            }
            else
                stk.push(atoi(a.c_str()));
        }
        return stk.top();
    }
};
总结:

表达式一般由操作数(Operand)、运算符(Operator)组成,例如算术表达式中,通常把运算符放在两个操作数的中间, 这称为中缀表达式(Infix Expression),如A+B。 波兰数学家Jan Lukasiewicz提出了另一种数学表示法,它有两种表示形式: 把运算符写在操作数之前,称为波兰表达式(Polish Expression)或前缀表达式(Prefix Expression),如+AB; 把运算符写在操作数之后,称为逆波兰表达式(Reverse Polish Expression)或后缀表达式(Suffix Expression),如AB+; 其中,逆波兰表达式在编译技术中有着普遍的应用。

计算逆波兰是栈结构最为基础的应用。具体思路如下:将表达式中的数据从前到后输入到栈结构中去,这里需要对输入的数据进行判断,如果是运算符,则弹出栈顶的两个元素,并将两个元素与运算符的运算结果push进栈,注意运算顺序,栈顶的元素在运算符的后面。如果是数据,则直接push进栈。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode解题总结 1. 数组 1.1 从有序数组中删除重复元素 1.2 在排序数组被旋转后进行查找 1.3 寻找两个排序数组的中位数 1.4 最长连续序列 1.5 累加和 1.6 移除数组中指定值 1.7 下一个排列 1.8 第n个全排列 1.9 验证数独的正确性 1.10 容纳雨水的量 1.11 旋转图像 1.12 数字加1 1.13 爬楼梯 1.14 格雷码 1.15 设置矩阵的行列为0 1.16 加油站问题 1.17 分糖果 1.18 只出现一次的数 2. 单链表 2.1 单链表相加 2.2 指定位置反转单链表 2.3 依据给定值将链表重新排序 2.4 删除链表中重复元素 2.5 指定位置旋转链表 2.6 删除倒数第N个节点 2.7 成对交换链表元素 2.8 复制复杂链表 2.9 链表环相关问题 2.9.1 链表是否有环 2.9.2 链表环的入口 2.10 改变链表中的元素位置2.11 LRU Cache(设计题) 3. 字符串 3.1 判断字符串是否为回文 3.2 实现strStr() 3.3 字符串转为int(atoi) 3.4 二进制树相加 3.5 最长回文字符串 3.6 正则表达式匹配[hard] 3.7 正则匹配 3.8 最长公共前缀 3.9 验证字符串是否为数字 3.10 数字转为罗马数字 3.11 罗马数字到数字 3.12 Count and Say 3.13 变位词 3.14 简化系统路径 3.15 最后一个单词的长度 3.16 反转字符串中的单词 3.16.1 字符串前后和中间可能存在多个空格 3.16.2 不存在前后和中间的多余空格 3.17 一个编辑距离 4. 栈 4.1 验证括号的正确性 4.2 最长的正确括号表达式 4.3 柱状图中的最大矩形面积 4.4 计算逆波兰式的值 5. 树 5.1 二叉树的遍历 5.1.1 二叉树的前、中、后序遍历 5.1.2 二叉树的层序遍历 5.1.3 恢复二叉树[hard] 5.1.4 判断两棵树是否相等 5.1.5 判断二叉树是否为AVL树 5.1.6 将二叉树转为链表 5.1.7 二叉树添加指向右边节点的指针 5.1.8 树中节点的最小公共祖先 5.2 二叉树的构建5.3 二叉查找树 5.3.1 生成不重复的二叉查找树数目 5.3.2 验证是否为二叉查找树 5.3.3 将有序数组转为二叉树 5.3.4 将有序链表转为二叉树 5.4 二叉树的递归 5.4.1 二叉树的最大深度 5.4.2 二叉树的最小深度 5.4.3 路径和 5.4.4 满二叉树添加指向右边节点的指针 5.4.5 根节点到叶结点的所有路径代表的数字之和 6. 排序 6.1 合并两个有序数组到其中一个数组 6.2 合并两个有序链表 6.3 合并K个有序链表 6.4 使用插入排序来排序链表 6.5 归并排序排序链表 6.6 第一个缺少的正数 6.7 排序颜色 7. 查找 7.1 在排序数组中查找数出现的范围 7.2 在排序数组中查找给定值的插入位置 7.3 在二维排序数组中查找给定值 7.4 在旋转有序数组中查找最小值 7.4.1 数组无重复 7.4.2 数组有重复 7.5 在旋转排序数组中查找指定数字 8. 暴力枚举法 8.1 求集合的子集 8.2 集合的全排列 8.3 在指定树中选择进行全排列 8.4 电话上对应数字的字母组成的所有单词 9. 广度优先搜索 9.1 单词变换路径(Word Ladder) 9.1.1 是否存在变换路径 9.1.2 所有最短变换路径9.2 包围区域 10. 深度优先搜索 10.1 N皇后问题 10.2 恢复IP地址 10.3 集合元素之和 10.3.1 元素可以重复 10.3.2 元素不可重复 10.3.3 给定元素数目和元素范围 10.4 正确的括号对 10.5 解数独 10.6 单词搜索 10.7 小结 10.7.1 适用场景 10.7.2 思考步骤 10.7.3 代码模板 10.7.4 深搜与回溯、递归的区别 11. 分治法 11.1 实现pow(x, n) 11.2 Sqrt(x) 12. 贪心算法 12.1 跳台阶游戏 12.2 买卖股票的最佳时机 12.2.1 最多允许交易一次 12.2.2 可以交易任意多次 12.2.3 最多可以交易两次 12.2.4 可以交易任意多次 12.2.5 交易后需要停止一段时间 12.3 最长不含重复元素的子串 12.4 存放的最大水量 13. 动态规划 13.1 三角形从顶到底的最小路径和 13.2 最大连续子数组 13.3 字符串的所有子回文字符串 13.4 最长公共子序列问题 13.5 字符串的编辑距离 13.6 不同路径之和 13.6.1 无障碍13.6.2 有障碍 13.7 最大矩形面积 13.8 字符串交叉组合 13.9 旋转字符串 13.10 最小路径和 13.11 所有的编码方式 13.12 独一无二的子序列数 13.13 拆分单词 13.13.1 单词是否由词典中的单词组成 13.13.2 返回所有可以切分的解 14. 图 14.1 图的克隆 15. 细节实现题 15.1 反转整数 15.2 对称数判断 15.3 区间的相关操作 15.3.1 在区间中插入新的区间 15.3.2 合并区间 15.4 包含子串元素的最小窗口 15.5 大数乘法 15.6 给定串中是否存在包含所有单词的子串 15.7 Pascal 三角形 15.7.1 生成Pascal三角形 15.7.2 Pascal三角形的第N行 15.8 螺旋形矩阵 15.8.1 螺旋打印矩阵 15.8.2 生成螺旋矩阵 15.9 Z字形输出字符串 15.10 不使用乘、除、取模实现两个整数相除 15.11 文本对齐 15.12 共线的最大点数 16 其他问题 16.1 随机数生成器
你可以使用以下方法对二维向量vector<vector<int>>进行排序: 1. 使用STL中的sort函数进行排序。引用中提到了一些排序算法,比如堆排序(heap_sort),希尔排序(shell_sort),选择排序(select_sort)和快速排序(quick_sort_swap)。你可以根据需要选择其中一个算法对vector<vector<int>>进行排序。 2. 如果你想在创建二维向量时就进行排序,你可以使用引用中的方法通过vector创建二维数组,并在创建时对其中的元素进行排序。 3. 如果你已经创建了一个二维向量,你可以使用引用中的方法使用resize函数对二维向量进行重新分配,并在重新分配时对其中的元素进行排序。 无论你选择哪种方法,都可以对二维向量vector<vector<int>>进行排序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vector<string>和vector<int>](https://blog.csdn.net/toCVer/article/details/122484732)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [leetcode2sumc-Code-Storm2019:力密码2019](https://download.csdn.net/download/weixin_38700430/19937142)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值