From 75a7f51072b741517586ac376052bbe942be1ee5 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Tue, 9 Nov 2021 17:22:44 +0800 Subject: [PATCH] add exercises --- .../config.json" | 12 + .../desc.html" | 44 +++ .../solution.cpp" | 0 .../solution.json" | 6 + .../solution.md" | 353 ++++++++++++++++++ .../config.json" | 12 + .../desc.html" | 46 +++ .../solution.cpp" | 0 .../solution.json" | 6 + .../solution.md" | 236 ++++++++++++ .../solution.md" | 276 ++++++++++++++ .../solution.md" | 167 ++++++++- 12 files changed, 1153 insertions(+), 5 deletions(-) create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/config.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/desc.html" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.cpp" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.json" create mode 100644 "data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/config.json" new file mode 100644 index 000000000..229baf9b7 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "algorithm-7d5924a413584b319285481e9745c0d9", + "keywords": [ + "leetcode", + "二叉树的序列化与反序列化" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "二叉树的序列化与反序列化" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/desc.html" new file mode 100644 index 000000000..aafb656b6 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/desc.html" @@ -0,0 +1,44 @@ +

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

+ +

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

+ +

提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,2,3,null,null,4,5]
+输出:[1,2,3,null,null,4,5]
+
+ +

示例 2:

+ +
+输入:root = []
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [1]
+输出:[1]
+
+ +

示例 4:

+ +
+输入:root = [1,2]
+输出:[1,2]
+
+ +

 

+ +

提示:

+ + diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.cpp" new file mode 100644 index 000000000..e69de29bb diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.json" new file mode 100644 index 000000000..40dd0743b --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "1cdb3535a5ae494eaeca728a1b141ae1" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" new file mode 100644 index 000000000..3567fb831 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/10.297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" @@ -0,0 +1,353 @@ +# 二叉树的序列化与反序列化 +

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

+ +

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

+ +

提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,2,3,null,null,4,5]
+输出:[1,2,3,null,null,4,5]
+
+ +

示例 2:

+ +
+输入:root = []
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [1]
+输出:[1]
+
+ +

示例 4:

+ +
+输入:root = [1,2]
+输出:[1,2]
+
+ +

 

+ +

提示:

+ + + +

以下错误的选项是?

+ +## aop +### before +```cpp +#include +using namespace std; + +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) {} +}; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + if (!root) + return "null "; + return serialize(root->left) + serialize(root->right) + to_string(root->val); + } + + TreeNode *helptrans(istringstream &ss) + { + string s; + TreeNode *root = NULL; + ss >> s; + if (s == "null") + return root; + root = new TreeNode(stoi(s)); + root->right = helptrans(ss); + root->left = helptrans(ss); + return root; + } + + TreeNode *deserialize(string data) + { + stringstream ss; + ss << data; + string s, trans; + while (ss >> s) + trans = s + " " + trans; + istringstream st(trans); + return helptrans(st); + } +}; +``` +## 选项 + +### A +```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + if (!root) + return "#_"; + return to_string(root->val) + "_" + serialize(root->left) + serialize(root->right); + } + + TreeNode *deserialize(string data) + { + cout << data << endl; + queue q; + stringstream ss(data); + string s; + while (getline(ss, s, '_')) + q.push(s); + return help(q); + } + + TreeNode *help(queue &q) + { + auto cur = q.front(); + q.pop(); + if (cur == "#") + return NULL; + auto root = new TreeNode(stoi(cur)); + root->left = help(q); + root->right = help(q); + return root; + } +}; + +``` + +### B +```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + string ans; + if (root == NULL) + return "[]"; + ans = "["; + queue que; + que.push(root); + + char str[100]; + + while (!que.empty()) + { + TreeNode *top = que.front(); + que.pop(); + + if (top != NULL) + { + que.push(top->left); + que.push(top->right); + + sprintf(str, "%d", top->val); + ans += str; + } + else + { + ans += "null"; + } + + ans += ","; + } + + int end = ans.length() - 1; + while (!(ans[end] >= '0' && ans[end] <= '9')) + end--; + + string rs = ans.substr(0, end + 1); + rs += "]"; + + return rs; + } + + TreeNode *deserialize(string data) + { + + int len = data.size(); + + if (len <= 2) + return NULL; + + int numsCount = 0; + vector nums; + + string word = ""; + for (int i = 1; i <= len - 2; i++) + { + if (data[i] == ',') + { + TreeNode *tmp = NULL; + if (word == "null") + { + } + else + { + int num = atoi(word.c_str()); + tmp = new TreeNode(num); + } + nums.push_back(tmp); + word = ""; + } + else + { + + word += data[i]; + } + } + if (word != "" && word != "null") + { + int num = atoi(word.c_str()); + TreeNode *tmp = new TreeNode(num); + nums.push_back(tmp); + } + + int cnt = nums.size(); + + int q = 0; + int p = 1; + while (p < cnt) + { + if (nums[q] == NULL) + { + q++; + } + else + { + if (p < cnt) + nums[q]->left = nums[p]; + if (p + 1 < cnt) + nums[q]->right = nums[p + 1]; + p += 2; + q++; + } + } + + return nums[0]; + } +}; +``` + +### C +```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + string result = "["; + queue myQue; + myQue.push(root); + while (!myQue.empty()) + { + root = myQue.front(); + myQue.pop(); + if (root == NULL) + { + result += "null,"; + continue; + } + else + { + result += to_string(root->val) + ","; + myQue.push(root->left); + myQue.push(root->right); + } + } + + if (result == "[null,") + { + result.resize(result.size() - 1); + } + else + { + int endIndex = result.size() - 1; + + while (result[endIndex] < '0' || result[endIndex] > '9') + { + endIndex -= 1; + } + result.resize(endIndex + 1); + } + result += "]"; + return result; + } + + TreeNode *deserialize(string data) + { + + vector dataVec; + int dataSize = data.size(); + + for (int index = 1; index < dataSize - 1; ++index) + { + string tempData = ""; + + while (index < dataSize - 1 && data[index] != ',') + { + tempData += data[index++]; + } + dataVec.push_back(tempData); + } + + int dataVecSize = dataVec.size(); + queue myQue; + + if (dataVec[0] == "null") + { + return NULL; + } + TreeNode *result = new TreeNode(atoi(dataVec[0].c_str())), *tempPtr; + myQue.push(result); + for (int index = 1; index < dataVecSize; ++index) + { + tempPtr = myQue.front(); + myQue.pop(); + + if (dataVec[index] != "null") + { + tempPtr->left = new TreeNode(atoi(dataVec[index].c_str())); + myQue.push(tempPtr->left); + } + index += 1; + + if (index < dataVecSize && dataVec[index] != "null") + { + tempPtr->right = new TreeNode(atoi(dataVec[index].c_str())); + myQue.push(tempPtr->right); + } + } + return result; + } +}; +``` diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/config.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/config.json" new file mode 100644 index 000000000..a72328279 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/config.json" @@ -0,0 +1,12 @@ +{ + "node_id": "algorithm-b52188ca107f42db935096b8640aa995", + "keywords": [ + "leetcode", + "扁平化嵌套列表迭代器" + ], + "children": [], + "export": [ + "solution.json" + ], + "title": "扁平化嵌套列表迭代器" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/desc.html" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/desc.html" new file mode 100644 index 000000000..c258c91e7 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/desc.html" @@ -0,0 +1,46 @@ +

给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。

+ +

实现扁平迭代器类 NestedIterator

+ +
    +
  • NestedIterator(List<NestedInteger> nestedList) 用嵌套列表 nestedList 初始化迭代器。
  • +
  • int next() 返回嵌套列表的下一个整数。
  • +
  • boolean hasNext() 如果仍然存在待迭代的整数,返回 true ;否则,返回 false
  • +
+ +

你的代码将会用下述伪代码检测:

+ +
+initialize iterator with nestedList
+res = []
+while iterator.hasNext()
+    append iterator.next() to the end of res
+return res
+ +

如果 res 与预期的扁平化列表匹配,那么你的代码将会被判为正确。

+ +

 

+ +

示例 1:

+ +
+输入:nestedList = [[1,1],2,[1,1]]
+输出:[1,1,2,1,1]
+解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]
+ +

示例 2:

+ +
+输入:nestedList = [1,[4,[6]]]
+输出:[1,4,6]
+解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nestedList.length <= 500
  • +
  • 嵌套列表中的整数值在范围 [-106, 106]
  • +
diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.cpp" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.cpp" new file mode 100644 index 000000000..e69de29bb diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.json" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.json" new file mode 100644 index 000000000..b2d53427f --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "CSDN.net", + "source": "solution.md", + "exercise_id": "29acba1430844966b2c998a2e2c82649" +} \ No newline at end of file diff --git "a/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" new file mode 100644 index 000000000..762ed7829 --- /dev/null +++ "b/data/3.\347\256\227\346\263\225\351\253\230\351\230\266/5.leetcode-\350\256\276\350\256\241/9.341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" @@ -0,0 +1,236 @@ +# 扁平化嵌套列表迭代器 +

给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。

+ +

实现扁平迭代器类 NestedIterator

+ +
    +
  • NestedIterator(List<NestedInteger> nestedList) 用嵌套列表 nestedList 初始化迭代器。
  • +
  • int next() 返回嵌套列表的下一个整数。
  • +
  • boolean hasNext() 如果仍然存在待迭代的整数,返回 true ;否则,返回 false
  • +
+ +

你的代码将会用下述伪代码检测:

+ +
+initialize iterator with nestedList
+res = []
+while iterator.hasNext()
+    append iterator.next() to the end of res
+return res
+ +

如果 res 与预期的扁平化列表匹配,那么你的代码将会被判为正确。

+ +

 

+ +

示例 1:

+ +
+输入:nestedList = [[1,1],2,[1,1]]
+输出:[1,1,2,1,1]
+解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]
+ +

示例 2:

+ +
+输入:nestedList = [1,[4,[6]]]
+输出:[1,4,6]
+解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nestedList.length <= 500
  • +
  • 嵌套列表中的整数值在范围 [-106, 106]
  • +
+ +

以下错误的选项是?

+ +## aop +### before +```cpp +#include +using namespace std; + +// 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 &getList() const; +}; +``` +### after +```cpp + +``` + +## 答案 +```cpp +class NestedIterator +{ +public: + vector data; + vector::iterator it; + + NestedIterator(vector &nestedList) + { + parse(nestedList); + it = data.begin(); + } + + void parse(vector &nestedList) + { + for (auto nes : nestedList) + { + + data.push_back(nes.getInteger()); + parse(nes.getList()); + } + } + + int next() + { + return *it++; + } + + bool hasNext() + { + return it != data.end(); + } +}; +``` +## 选项 + +### A +```cpp +class NestedIterator +{ +public: + stack st; + NestedIterator(vector &nestedList) + { + for (int i = nestedList.size() - 1; i >= 0; --i) + st.push(&nestedList[i]); + } + + int next() + { + int res = st.top()->getInteger(); + st.pop(); + return res; + } + + bool hasNext() + { + if (st.empty()) + return false; + NestedInteger *tmp = st.top(); + while (!tmp->isInteger()) + { + st.pop(); + vector &list = tmp->getList(); + for (int i = list.size() - 1; i >= 0; --i) + st.push(&list[i]); + if (st.empty()) + return false; + tmp = st.top(); + } + return true; + } +}; +``` + +### B +```cpp +class NestedIterator +{ +public: + int cnt = 0; + vector ans; + + void dfs(vector &nestedList) + { + for (auto l : nestedList) + { + if (l.isInteger()) + ans.push_back(l.getInteger()); + else + dfs(l.getList()); + } + } + + NestedIterator(vector &nestedList) + { + dfs(nestedList); + } + + int next() + { + return ans[cnt++]; + } + + bool hasNext() + { + return cnt < ans.size(); + } +}; +``` + +### C +```cpp +class NestedIterator +{ + stack::iterator> begins, ends; + +public: + NestedIterator(vector &nestedList) + { + begins.push(nestedList.begin()); + ends.push(nestedList.end()); + } + + int next() + { + hasNext(); + return (begins.top()++)->getInteger(); + } + + bool hasNext() + { + vector::iterator tp; + while (!begins.empty()) + { + if (begins.top() == ends.top()) + { + begins.pop(); + ends.pop(); + } + else + { + tp = begins.top(); + if (tp->isInteger()) + return true; + + begins.top()++; + + begins.push(tp->getList().begin()); + ends.push(tp->getList().end()); + ; + } + } + return false; + } +}; +``` diff --git "a/data_backup/1.leetcode/297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" "b/data_backup/1.leetcode/297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" index ddcd7176a..3567fb831 100644 --- "a/data_backup/1.leetcode/297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" +++ "b/data_backup/1.leetcode/297-\344\272\214\345\217\211\346\240\221\347\232\204\345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226/solution.md" @@ -45,10 +45,22 @@

以下错误的选项是?

+ ## aop ### before ```cpp +#include +using namespace std; +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) {} +}; ``` ### after ```cpp @@ -57,21 +69,285 @@ ## 答案 ```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + if (!root) + return "null "; + return serialize(root->left) + serialize(root->right) + to_string(root->val); + } + + TreeNode *helptrans(istringstream &ss) + { + string s; + TreeNode *root = NULL; + ss >> s; + if (s == "null") + return root; + root = new TreeNode(stoi(s)); + root->right = helptrans(ss); + root->left = helptrans(ss); + return root; + } + TreeNode *deserialize(string data) + { + stringstream ss; + ss << data; + string s, trans; + while (ss >> s) + trans = s + " " + trans; + istringstream st(trans); + return helptrans(st); + } +}; ``` ## 选项 ### A ```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + if (!root) + return "#_"; + return to_string(root->val) + "_" + serialize(root->left) + serialize(root->right); + } + + TreeNode *deserialize(string data) + { + cout << data << endl; + queue q; + stringstream ss(data); + string s; + while (getline(ss, s, '_')) + q.push(s); + return help(q); + } + + TreeNode *help(queue &q) + { + auto cur = q.front(); + q.pop(); + if (cur == "#") + return NULL; + auto root = new TreeNode(stoi(cur)); + root->left = help(q); + root->right = help(q); + return root; + } +}; ``` ### B ```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + string ans; + if (root == NULL) + return "[]"; + ans = "["; + queue que; + que.push(root); + + char str[100]; + + while (!que.empty()) + { + TreeNode *top = que.front(); + que.pop(); + + if (top != NULL) + { + que.push(top->left); + que.push(top->right); + + sprintf(str, "%d", top->val); + ans += str; + } + else + { + ans += "null"; + } + + ans += ","; + } + + int end = ans.length() - 1; + while (!(ans[end] >= '0' && ans[end] <= '9')) + end--; + + string rs = ans.substr(0, end + 1); + rs += "]"; + + return rs; + } + + TreeNode *deserialize(string data) + { + + int len = data.size(); + + if (len <= 2) + return NULL; + int numsCount = 0; + vector nums; + + string word = ""; + for (int i = 1; i <= len - 2; i++) + { + if (data[i] == ',') + { + TreeNode *tmp = NULL; + if (word == "null") + { + } + else + { + int num = atoi(word.c_str()); + tmp = new TreeNode(num); + } + nums.push_back(tmp); + word = ""; + } + else + { + + word += data[i]; + } + } + if (word != "" && word != "null") + { + int num = atoi(word.c_str()); + TreeNode *tmp = new TreeNode(num); + nums.push_back(tmp); + } + + int cnt = nums.size(); + + int q = 0; + int p = 1; + while (p < cnt) + { + if (nums[q] == NULL) + { + q++; + } + else + { + if (p < cnt) + nums[q]->left = nums[p]; + if (p + 1 < cnt) + nums[q]->right = nums[p + 1]; + p += 2; + q++; + } + } + + return nums[0]; + } +}; ``` ### C ```cpp +class Codec +{ +public: + string serialize(TreeNode *root) + { + string result = "["; + queue myQue; + myQue.push(root); + while (!myQue.empty()) + { + root = myQue.front(); + myQue.pop(); + if (root == NULL) + { + result += "null,"; + continue; + } + else + { + result += to_string(root->val) + ","; + myQue.push(root->left); + myQue.push(root->right); + } + } + + if (result == "[null,") + { + result.resize(result.size() - 1); + } + else + { + int endIndex = result.size() - 1; + + while (result[endIndex] < '0' || result[endIndex] > '9') + { + endIndex -= 1; + } + result.resize(endIndex + 1); + } + result += "]"; + return result; + } + + TreeNode *deserialize(string data) + { + + vector dataVec; + int dataSize = data.size(); + + for (int index = 1; index < dataSize - 1; ++index) + { + string tempData = ""; + + while (index < dataSize - 1 && data[index] != ',') + { + tempData += data[index++]; + } + dataVec.push_back(tempData); + } + + int dataVecSize = dataVec.size(); + queue myQue; + + if (dataVec[0] == "null") + { + return NULL; + } + TreeNode *result = new TreeNode(atoi(dataVec[0].c_str())), *tempPtr; + myQue.push(result); + for (int index = 1; index < dataVecSize; ++index) + { + tempPtr = myQue.front(); + myQue.pop(); + + if (dataVec[index] != "null") + { + tempPtr->left = new TreeNode(atoi(dataVec[index].c_str())); + myQue.push(tempPtr->left); + } + index += 1; + if (index < dataVecSize && dataVec[index] != "null") + { + tempPtr->right = new TreeNode(atoi(dataVec[index].c_str())); + myQue.push(tempPtr->right); + } + } + return result; + } +}; ``` diff --git "a/data_backup/1.leetcode/341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" "b/data_backup/1.leetcode/341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" index fe1c4e298..762ed7829 100644 --- "a/data_backup/1.leetcode/341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" +++ "b/data_backup/1.leetcode/341-\346\211\201\345\271\263\345\214\226\345\265\214\345\245\227\345\210\227\350\241\250\350\277\255\344\273\243\345\231\250/solution.md" @@ -47,10 +47,29 @@ return res

以下错误的选项是?

+ ## aop ### before ```cpp - +#include +using namespace std; + +// 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 &getList() const; +}; ``` ### after ```cpp @@ -59,21 +78,159 @@ return res ## 答案 ```cpp - +class NestedIterator +{ +public: + vector data; + vector::iterator it; + + NestedIterator(vector &nestedList) + { + parse(nestedList); + it = data.begin(); + } + + void parse(vector &nestedList) + { + for (auto nes : nestedList) + { + + data.push_back(nes.getInteger()); + parse(nes.getList()); + } + } + + int next() + { + return *it++; + } + + bool hasNext() + { + return it != data.end(); + } +}; ``` ## 选项 ### A ```cpp - +class NestedIterator +{ +public: + stack st; + NestedIterator(vector &nestedList) + { + for (int i = nestedList.size() - 1; i >= 0; --i) + st.push(&nestedList[i]); + } + + int next() + { + int res = st.top()->getInteger(); + st.pop(); + return res; + } + + bool hasNext() + { + if (st.empty()) + return false; + NestedInteger *tmp = st.top(); + while (!tmp->isInteger()) + { + st.pop(); + vector &list = tmp->getList(); + for (int i = list.size() - 1; i >= 0; --i) + st.push(&list[i]); + if (st.empty()) + return false; + tmp = st.top(); + } + return true; + } +}; ``` ### B ```cpp - +class NestedIterator +{ +public: + int cnt = 0; + vector ans; + + void dfs(vector &nestedList) + { + for (auto l : nestedList) + { + if (l.isInteger()) + ans.push_back(l.getInteger()); + else + dfs(l.getList()); + } + } + + NestedIterator(vector &nestedList) + { + dfs(nestedList); + } + + int next() + { + return ans[cnt++]; + } + + bool hasNext() + { + return cnt < ans.size(); + } +}; ``` ### C ```cpp - +class NestedIterator +{ + stack::iterator> begins, ends; + +public: + NestedIterator(vector &nestedList) + { + begins.push(nestedList.begin()); + ends.push(nestedList.end()); + } + + int next() + { + hasNext(); + return (begins.top()++)->getInteger(); + } + + bool hasNext() + { + vector::iterator tp; + while (!begins.empty()) + { + if (begins.top() == ends.top()) + { + begins.pop(); + ends.pop(); + } + else + { + tp = begins.top(); + if (tp->isInteger()) + return true; + + begins.top()++; + + begins.push(tp->getList().begin()); + ends.push(tp->getList().end()); + ; + } + } + return false; + } +}; ``` -- GitLab