力扣-----每日两题

(1)第一题:341. 扁平化嵌套列表迭代器

题目:给你一个嵌套的整型列表。请你设计一个迭代器,使其能够遍历这个整型列表中的所有整数。
列表中的每一项或者为一个整数,或者是另一个列表。其中列表的元素也可能是整数或是其他列表。

1.请注意一定要很认真的读题,因为题目是在代码中有一些详细的描述以注释给出,但由于是英文注释,所以理解的不那么的全面,使得最开始很迷惑不知道怎么下手。
2.但是,看清题目,本题是构造一个容器类NestedIterator,在main函数中我们使用它是:
NestedIterator i(nestedList);
初始化的方式while (i.hasNext()) cout << i.next();
3.也就是说,输入的是nestedList数据是vector类型的,在这里用到了组合类+vector(向量中的每一个数据都是NestedInteger这个类类型的),我们需要做的就是在NestedIterator(vector &nestedList)这个初始化函数里将传入的vector &nestedList嵌套列表转化为正常列表,非嵌套形式的即可。
4.注意,vector中自带很多函数可以用,在这里用到了size()可以直接获取当前列表有多少个元素,为dfs准备。
5.题目中嵌套列表类提供了三个函数,一个是bool isInteger() const;判断当前列表中元素是不是一个数,如果是就用 int getInteger() const;获取该值,否则可以用const vector &getList() const;去获取这个嵌套的列表。

class NestedIterator {
    
    
private:
    vector<int> integer;
    int index=0;
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
    
      //在这里初始化
        convert(nestedList);   //进行转化成本类对象,即拆分嵌套
    }
    
    int next() {
    
    
        return integer[index++];
    }
    
    bool hasNext() {
    
    
        return integer.size()>index;
    }

    void convert(vector<NestedInteger> &nestedList)
    {
    
    
        for(int i=0;i<nestedList.size();i++)    //dfs思想
        {
    
    
            if(nestedList[i].isInteger())
            {
    
    
                integer.push_back(nestedList[i].getInteger());
            }
            else
            {
    
    
                convert(nestedList[i].getList());
            }
        }
    }
};
/**
 * // 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;
 * };
 */

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);   初始化的方式
 * while (i.hasNext()) cout << i.next();
 */

(2)第二题:94. 二叉树的中序遍历

题目:给定一个二叉树的根节点 root ,返回它的 中序 遍历。

1.本题所考的就是二叉树的中序遍历,其实不管是先序遍历还是中序遍历还是后序遍历,其实不同的地方就是是先遍历还是先输出,其实就是顺序的问题。这种问题就是用递归去解比较好,也可以用栈,于递归而言的都可以用栈。
2.但是本题稍微出错的地方就是最开始我直接result[index++]=root->val,这种做法是不可以的必须要先给result赋空间才可以,而如果不想赋空间想让它用多少存多少时,可以直接调用vector的push_back函数,将其插入vector最后。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
private:
    vector<int> result;
public:
    void midtraverse(TreeNode* root)
    {
    
    
        if(root!=nullptr)
        {
    
    
            if(root->left!=nullptr)
                midtraverse(root->left);

            result.push_back(root->val);

            if(root->right!=nullptr)
                midtraverse(root->right);
        }
    }
    vector<int> inorderTraversal(TreeNode* root) {
    
    
        midtraverse(root);
        return result;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_43978754/article/details/115111772