diff --git a/.gitignore b/.gitignore index d333e34ee0e5217d1e51f54114ccb7da48f4565a..0e142fc8d738881c2333ddda8069768ba877cebd 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,9 @@ info.plist #patch *.patch -*.dff \ No newline at end of file +*.dff + +#frame +.vscode +.template +node_modules \ No newline at end of file diff --git a/README.md b/README.md index b024d29d6c1e0536d09f542549cb0bb6946d14bc..7187e5f4bec1382d91b9633f4a7232138f538bf3 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,64 @@ # 日拱一卒,功不唐捐 ## 贪心 +[517. 超级洗衣机](https://leetcode-cn.com/problems/super-washing-machines) + +[121. 买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) + +[502. IPO](https://leetcode-cn.com/problems/ipo)「大顶堆」 + +[55. 跳跃游戏](https://leetcode-cn.com/problems/jump-game) + +[45. 跳跃游戏 II](https://leetcode-cn.com/problems/jump-game-ii) + [881. 救生艇](https://leetcode-cn.com/problems/boats-to-save-people) [1833. 雪糕的最大数量](https://leetcode-cn.com/problems/maximum-ice-cream-bars/) [12. 整数转罗马数字](https://leetcode-cn.com/problems/integer-to-roman/) -[300. 最长递增子序列](https://leetcode-cn.com/problems/longest-increasing-subsequence) +[300. 最长递增子序列](https://leetcode-cn.com/problems/longest-increasing-subsequence)「贪心+二分, DP」 [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence) [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging) ## 动态规则 +[322. 零钱兑换](https://leetcode-cn.com/problems/coin-change) + +[312. 戳气球](https://leetcode-cn.com/problems/burst-balloons) + +[309. 最佳买卖股票时机含冷冻期](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown) + +[279. 完全平方数](https://leetcode-cn.com/problems/perfect-squares) + +[221. 最大正方形](https://leetcode-cn.com/problems/maximal-square) + +[198. 打家劫舍](https://leetcode-cn.com/problems/house-robber) + +[91. 解码方法](https://leetcode-cn.com/problems/decode-ways) + +[639. 解码方法 II](https://leetcode-cn.com/problems/decode-ways-ii) + +[139. 单词拆分](https://leetcode-cn.com/problems/word-break) + +[1143. 最长公共子序列](https://leetcode-cn.com/problems/longest-common-subsequence) + +[583. 两个字符串的删除操作](https://leetcode-cn.com/problems/delete-operation-for-two-strings) + +[673. 最长递增子序列的个数](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence) + +[63. 不同路径 II](https://leetcode-cn.com/problems/unique-paths-ii) + +[72. 编辑距离](https://leetcode-cn.com/problems/edit-distance) + +[600. 不含连续1的非负整数](https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones) + [5. 最长回文子串](https://leetcode-cn.com/problems/longest-palindromic-substring/) [10. 正则表达式匹配](https://leetcode-cn.com/problems/regular-expression-matching/) +[44. 通配符匹配](https://leetcode-cn.com/problems/wildcard-matching) + [剑指 Offer 42.连续子数组的最大和](https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/) [32. 最长有效括号](https://leetcode-cn.com/problems/longest-valid-parentheses) @@ -42,19 +84,47 @@ [1928. 规定时间内到达终点的最小花费](https://leetcode-cn.com/problems/minimum-cost-to-reach-destination-in-time) [1981. 最小化目标值与所选元素的差](https://leetcode-cn.com/problems/minimize-the-difference-between-target-and-chosen-elements)「分组背包」 + +[53. 最大子序和](https://leetcode-cn.com/problems/maximum-subarray)「前缀和」 + +[152. 乘积最大子数组](https://leetcode-cn.com/problems/maximum-product-subarray) ## 排序 +[148. 排序链表](https://leetcode-cn.com/problems/sort-list)「归并排序」 + +[5864. 游戏中弱角色的数量](https://leetcode-cn.com/problems/the-number-of-weak-characters-in-the-game) + [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/)「桶排序」 [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) [274. H 指数](https://leetcode-cn.com/problems/h-index/)「计数(桶)排序」 ## 查找 +[352. 将数据流变为多个不相交区间](https://leetcode-cn.com/problems/data-stream-as-disjoint-intervals)「map二分查找」 + [981. 基于时间的键值存储](https://leetcode-cn.com/problems/time-based-key-value-store/)「二分查找」 [34. 在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array) [1337. 矩阵中战斗力最弱的 K 行](https://leetcode-cn.com/problems/the-k-weakest-rows-in-a-matrix) ## 搜索 +[638. 大礼包](https://leetcode-cn.com/problems/shopping-offers) + +[301. 删除无效的括号](https://leetcode-cn.com/problems/remove-invalid-parentheses) + +[240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii) + +[282. 给表达式添加运算符](https://leetcode-cn.com/problems/expression-add-operators)「回溯」 + +[200. 岛屿数量](https://leetcode-cn.com/problems/number-of-islands)「DFS」 + +[102. 二叉树的层序遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal)「BFS」 + +[79. 单词搜索](https://leetcode-cn.com/problems/word-search)「记忆化搜索」 + +[46. 全排列](https://leetcode-cn.com/problems/permutations)「DFS」 + +[47. 全排列 II](https://leetcode-cn.com/problems/permutations-ii)「DFS+剪枝」 + [797. 所有可能的路径](https://leetcode-cn.com/problems/all-paths-from-source-to-target)「DFS」 [17. 电话号码的字母组合](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/)「回溯」 @@ -67,6 +137,10 @@ [37. 解数独](https://leetcode-cn.com/problems/sudoku-solver)「回溯」 +[51. N 皇后](https://leetcode-cn.com/problems/n-queens)「回溯」 + +[52. N皇后 II](https://leetcode-cn.com/problems/n-queens-ii)「回溯」 + [802. 找到最终的安全状态](https://leetcode-cn.com/problems/find-eventual-safe-states)「三色标记」 [39. 组合总和](https://leetcode-cn.com/problems/combination-sum)「回溯」 @@ -77,6 +151,12 @@ [526. 优美的排列](https://leetcode-cn.com/problems/beautiful-arrangement)「回溯, DP」 ## Hash +[869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2) + +[128. 最长连续序列](https://leetcode-cn.com/problems/longest-consecutive-sequence) + +[442. 数组中重复的数据](https://leetcode-cn.com/problems/find-all-duplicates-in-an-array)「原地Hash」 + [1418. 点菜展示表](https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant/) [1711. 大餐计数](https://leetcode-cn.com/problems/count-good-meals/)「互补 Key , 桶」 @@ -85,6 +165,10 @@ [49. 字母异位词分组](https://leetcode-cn.com/problems/group-anagrams/) 同 [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) ## 位运算 +[136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number)「异或 b^a^b=a」 + +[371. 两整数之和](https://leetcode-cn.com/problems/sum-of-two-integers) + [1603. 设计停车系统](https://leetcode-cn.com/problems/design-parking-system) [5851. 找出不同的二进制字符串](https://leetcode-cn.com/problems/find-unique-binary-string) @@ -92,27 +176,49 @@ [6. Z 字形变换](https://leetcode-cn.com/problems/zigzag-conversion/) [8. 字符串转换整数 (atoi)](https://leetcode-cn.com/problems/string-to-integer-atoi/) + +[394. 字符串解码](https://leetcode-cn.com/problems/decode-string) ## 数组 +[238. 除自身以外数组的乘积](https://leetcode-cn.com/problems/product-of-array-except-self)「前缀积、后缀积」 + [169. 多数元素](https://leetcode-cn.com/problems/majority-element/)「摩尔投票」 [面试题 17.10. 主要元素](https://leetcode-cn.com/problems/find-majority-element-lcci/) [1818. 绝对差值和](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/)「二分查找」 ## 堆 +[347. 前 K 个高频元素](https://leetcode-cn.com/problems/top-k-frequent-elements)「小顶堆」 + +[253. 会议室 II](https://leetcode-cn.com/problems/meeting-rooms-ii/)「小顶堆」 + +[面试题 17.14. 最小K个数](https://leetcode-cn.com/problems/smallest-k-lcci)「大顶堆」 + [295. 数据流的中位数](https://leetcode-cn.com/problems/find-median-from-data-stream)「小/大顶堆」 [218. 天际线问题](https://leetcode-cn.com/problems/the-skyline-problem/)「大顶堆」 ## 栈 +[84. 柱状图中最大的矩形](https://leetcode-cn.com/problems/largest-rectangle-in-histogram)「单调栈」[85. 最大矩形](https://leetcode-cn.com/problems/maximal-rectangle) + [42. 接雨水](https://leetcode-cn.com/problems/trapping-rain-water)「单调栈」 [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) [20. 有效的括号](https://leetcode-cn.com/problems/valid-parentheses/) +[678. 有效的括号字符串](https://leetcode-cn.com/problems/valid-parenthesis-string) + [232. 用栈实现队列](https://leetcode-cn.com/problems/implement-queue-using-stacks) ## 队列 [225. 用队列实现栈](https://leetcode-cn.com/problems/implement-stack-using-queues) ## 指针 +[142. 环形链表 II](https://leetcode-cn.com/problems/linked-list-cycle-ii)「快慢指针」 + +[287. 寻找重复数](https://leetcode-cn.com/problems/find-the-duplicate-number) + +[80. 删除有序数组中的重复项 II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii)「快慢指针」 + +[75. 颜色分类](https://leetcode-cn.com/problems/sort-colors)「双指针, 荷兰国旗问题」 + [11. 盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/)「双指针」 [14. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/) @@ -129,12 +235,18 @@ [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression)「双指针」 ## 滑动窗口 +[76. 最小覆盖子串](https://leetcode-cn.com/problems/minimum-window-substring) + [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) [30. 串联所有单词的子串](https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words) [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element) ## 链表 +[234. 回文链表](https://leetcode-cn.com/problems/palindrome-linked-list) + +[725. 分隔链表](https://leetcode-cn.com/problems/split-linked-list-in-parts) + [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache)「双向链表」 [19. 删除链表的倒数第 N 个结点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/)「双指针」 @@ -149,10 +261,27 @@ [25. K 个一组翻转链表](https://leetcode-cn.com/problems/reverse-nodes-in-k-group/) +[61. 旋转链表](https://leetcode-cn.com/problems/rotate-list) + +[160. 相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists)、 [剑指 Offer 52. 两个链表的第一个公共节点](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof) [138. 复制带随机指针的链表](https://leetcode-cn.com/problems/copy-list-with-random-pointer) ## 树 +[337. 打家劫舍 III](https://leetcode-cn.com/problems/house-robber-iii) + +[230. 二叉搜索树中第K小的元素](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst)「中序遍历」 + +[236. 二叉树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree) + +[124. 二叉树中的最大路径和](https://leetcode-cn.com/problems/binary-tree-maximum-path-sum) + +[105. 从前序与中序遍历序列构造二叉树](https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal) + +[98. 验证二叉搜索树](https://leetcode-cn.com/problems/validate-binary-search-tree)「中序遍历」 + +[94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal)「递归, 迭代, 莫里斯遍历」 + [671. 二叉树中第二小的节点](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree) [863. 二叉树中所有距离为 K 的结点](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree) @@ -160,7 +289,19 @@ [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree) [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree) + +[208. 实现 Trie (前缀树)](https://leetcode-cn.com/problems/implement-trie-prefix-tree)「字典树」 + +[212. 单词搜索 II](https://leetcode-cn.com/problems/word-search-ii)「字典树+DFS」 ## 图 +[399. 除法求值](https://leetcode-cn.com/problems/evaluate-division)「并查集」 + +[207. 课程表](https://leetcode-cn.com/problems/course-schedule)「三色标记」 + +[210. 课程表 II](https://leetcode-cn.com/problems/course-schedule-ii)「拓扑排序」 + +[1976. 到达目的地的方案数](https://leetcode-cn.com/problems/number-of-ways-to-arrive-at-destination)「有向图 , 单源最短路径」 + [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs) [743. 网络延迟时间](https://leetcode-cn.com/problems/network-delay-time) @@ -169,7 +310,17 @@ [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop)「快慢指针」 ## 代数 -[528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight)「前缀和」 +[166. 分数到小数](https://leetcode-cn.com/problems/fraction-to-recurring-decimal)「长除法」 + +[96. 不同的二叉搜索树](https://leetcode-cn.com/problems/unique-binary-search-trees)「卡塔兰数」 + +[48. 旋转图像](https://leetcode-cn.com/problems/rotate-image)「矩阵操作」 + +[50. Pow(x, n)](https://leetcode-cn.com/problems/powx-n)「快速幂」 + +[剑指 Offer 10- I. 斐波那契数列](https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof) + +[43. 字符串相乘](https://leetcode-cn.com/problems/multiply-strings) [LCP 29. 乐团站位](https://leetcode-cn.com/problems/SNJvJP) @@ -181,9 +332,7 @@ [31. 下一个排列](https://leetcode-cn.com/problems/next-permutation) -[1893. 检查是否区域内所有整数都被覆盖](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered)「差分数组 , 前缀和」 - -[1109. 航班预订统计](https://leetcode-cn.com/problems/corporate-flight-bookings)「差分数组 , 前缀和」 +[60. 排列序列](https://leetcode-cn.com/problems/permutation-sequence)「第 k 个排列」 [36. 有效的数独](https://leetcode-cn.com/problems/valid-sudoku) @@ -194,6 +343,30 @@ [5835. 最大方阵和](https://leetcode-cn.com/problems/maximum-matrix-sum) [789. 逃脱阻碍者](https://leetcode-cn.com/problems/escape-the-ghosts)「曼哈顿距离」 +### 差分 +[1893. 检查是否区域内所有整数都被覆盖](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered)「差分数组 , 前缀和」 + +[1109. 航班预订统计](https://leetcode-cn.com/problems/corporate-flight-bookings)「差分数组 , 前缀和」 + +### 前缀和 +[437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii) + +[5882. 网格游戏](https://leetcode-cn.com/problems/grid-game) + +[528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight)「前缀和」 + +[1894. 找到需要补充粉笔的学生编号](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk)「前缀和」 +## 几何 +[223. 矩形面积](https://leetcode-cn.com/problems/rectangle-area) +## 模拟 +[54. 螺旋矩阵](https://leetcode-cn.com/problems/spiral-matrix) + +[59. 螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii) + +[57. 插入区间](https://leetcode-cn.com/problems/insert-interval) +## 状态机 +[65. 有效数字](https://leetcode-cn.com/problems/valid-number) +![有效数字状态图](questions/65.有效数字/state_machine.jpg) ## 引用 [Text to ASCII Art Generator](http://patorjk.com/software/taag/#p=display&f=Ogre&t=mud) diff --git a/include/algm/ListNode.h b/include/algm/ListNode.h index 384b628726a748881afc7ad6895b663f19e8f3f1..b3a50e4170b80dbcd56f0739d2678879d03248b4 100755 --- a/include/algm/ListNode.h +++ b/include/algm/ListNode.h @@ -1,12 +1,14 @@ - -//Definition for singly-linked list. -#ifndef _LISTNODE -#define _LISTNODE -struct ListNode { - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} -}; + +//Definition for singly-linked list. +#ifndef _LISTNODE +#define _LISTNODE +#ifndef linkedlist_h +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +#endif #endif \ No newline at end of file diff --git a/include/linkedlist.h b/include/linkedlist.h index 9e3bc60db03f9898244717a0ac1d08ff09e9472e..05fdef7f4fd148d552cd953897ce3cbc941ec4e4 100644 --- a/include/linkedlist.h +++ b/include/linkedlist.h @@ -1,83 +1,81 @@ -#ifndef linkedlist_h -#define linkedlist_h -#include - -/** - * leetcode's Definition for singly-linked list. - */ -struct ListNode { - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} -}; -ostream & operator << (ostream & out, const ListNode & node){ - return out << node.val; -} -ostream & operator << (ostream & out, const ListNode * node){ - out << "["; - while(node){ out - << " 0x" << hex << (int64_t)node - << "=\"" << node->val << "\"" - << " → 0x" << (int64_t)node->next - << "\n"; - node = node->next; - } out << "]"; - return out; -} - -// 链表相关函数命名空间 -namespace LinkedList{ - -ListNode* Link(vector &vals, vector *list){ - list->reserve(vals.size()); - ListNode *p = NULL; - for(auto&v:vals){ - list->push_back(ListNode(v)); - if(p) p->next = &*(list->rbegin()); - p = &*(list->rbegin()); - } - return list->size() ? &((*list)[0]) : NULL; -} - -ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { - ListNode emptyHead, *pre = &emptyHead; - while(l1 && l2){ - auto s = (l1->val <= l2->val ? &l1 : &l2); - pre->next = *s; - pre = *s; - *s = (*s)->next; - } - pre->next = (l1 ? l1 : l2); - return emptyHead.next; -} - -ListNode* reverseList(ListNode* head) { - if(!head || !head->next) return head; - auto r = head->next; - head->next = NULL; - - while(r->next){ - auto N = r->next; r->next = head; - head = r; r = N; - } - - r->next = head; - return r; -} - -std::pair reverseList(ListNode* head, ListNode* tail) { - if(!tail) return {head, NULL}; - auto ok = tail->next, todo = head; - while(todo){ - auto ntd = todo->next; - todo->next = ok; - ok = todo; - todo = ntd; - } - return {tail, head}; -} - -}; // namespace LinkedList -#endif /* linkedlist_h */ +#ifndef linkedlist_h +#define linkedlist_h +#include + +/** + * leetcode's Definition for singly-linked list. + */ +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; +ostream & operator << (ostream & out, const ListNode & node){ + return out << node.val; +} +ostream & operator << (ostream & out, const ListNode * node){ + out << "["; + while(node){ out + << "\n → 0x" << hex << (int64_t)node + << "=\"" << dec << node->val << "\""; + node = node->next; + } out << " ]"; + return out; +} + +// 链表相关函数命名空间 +namespace LinkedList{ + +ListNode* Link(vector &vals, vector *list){ + list->reserve(vals.size()); + ListNode *p = NULL; + for(auto&v:vals){ + list->push_back(ListNode(v)); + if(p) p->next = &*(list->rbegin()); + p = &*(list->rbegin()); + } + return list->size() ? &((*list)[0]) : NULL; +} + +ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode emptyHead, *pre = &emptyHead; + while(l1 && l2){ + auto s = (l1->val <= l2->val ? &l1 : &l2); + pre->next = *s; + pre = *s; + *s = (*s)->next; + } + pre->next = (l1 ? l1 : l2); + return emptyHead.next; +} + +ListNode* reverseList(ListNode* head) { + if(!head || !head->next) return head; + auto r = head->next; + head->next = NULL; + + while(r->next){ + auto N = r->next; r->next = head; + head = r; r = N; + } + + r->next = head; + return r; +} + +std::pair reverseList(ListNode* head, ListNode* tail) { + if(!tail) return {head, NULL}; + auto ok = tail->next, todo = head; + while(todo){ + auto ntd = todo->next; + todo->next = ok; + ok = todo; + todo = ntd; + } + return {tail, head}; +} + +}; // namespace LinkedList +#endif /* linkedlist_h */ diff --git a/include/stl.h b/include/stl.h index a73124f634dbce5d4feebebe87b3b90169a82b52..c4af3ffb04408374429cd6d4c18b9603cfab05f6 100644 --- a/include/stl.h +++ b/include/stl.h @@ -1,5 +1,7 @@ #pragma once +#include #include +#include #include #include #include diff --git "a/questions/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/main.cpp" "b/questions/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7d2920efcbaca581a8262cfc24291c6898181f58 --- /dev/null +++ "b/questions/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[3,9,20,null,null,15,7]\"],[\"[1]\"],[\"[]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + TreeNode * arg0 = parseTreeNode(args[0]); + vector> result=s->levelOrder(arg0); + string resultabc =serializeIntegerArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/solution.h" "b/questions/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..b57d3917b7f7d75b40782b0b6e30d480c2a4542a --- /dev/null +++ "b/questions/102.\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\345\272\217\351\201\215\345\216\206/solution.h" @@ -0,0 +1,42 @@ +// @algorithm @lc id=102 lang=cpp +// @title binary-tree-level-order-traversal + + +// #define print(...) +// @test([3,9,20,null,null,15,7])=[[3],[9,20],[15,7]] +// @test([1])=[[1]] +// @test([])=[] +class Solution { +public: + vector> levelOrder(TreeNode* root) { + vector> res; + function bfs = [&](TreeNode * node){ + if(!node) return; + list q; + q.push_back(node); + while(int n = q.size()){ + res.emplace_back(vector(n, 0)); + auto it = res.back().begin(); + while(n--){ + auto t = q.front(); q.pop_front(); + *it++ = t->val; + if(t->left) q.push_back(t->left); + if(t->right)q.push_back(t->right); + } + } + }; + bfs(root); + return res; + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file diff --git "a/questions/105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/main.cpp" "b/questions/105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c7aca8f61a75ee75830b41d580bde825984d3994 --- /dev/null +++ "b/questions/105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[3,9,20,15,7]\",\"[9,3,15,20,7]\"],[\"[-1]\",\"[-1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector arg1 = parseIntegerArr(args[1]); + TreeNode * result=s->buildTree(arg0,arg1); + string resultabc =serializeTreeNode(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/solution.h" "b/questions/105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..0673c25225ed82c9791f8e23b4855872982144cc --- /dev/null +++ "b/questions/105.\344\273\216\345\211\215\345\272\217\344\270\216\344\270\255\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227\346\236\204\351\200\240\344\272\214\345\217\211\346\240\221/solution.h" @@ -0,0 +1,41 @@ +// @algorithm @lc id=105 lang=cpp +// @title construct-binary-tree-from-preorder-and-inorder-traversal + + +#define print(...) +// @test([3,9,20,15,7],[9,3,15,20,7])=[3,9,20,null,null,15,7] +// @test([-1],[-1])=[-1] +// 前序遍历:[ 根节点, [左子树的前序遍历结果], [右子树的前序遍历结果] ] +// 中序遍历:[ [左子树的中序遍历结果], 根节点, [右子树的中序遍历结果] ] +class Solution { +public: + TreeNode* buildTree(vector& preorder, vector& inorder) { + unordered_map inPos; // val 在 inorder 中的index,方便找root + int n = inorder.size(); + for(int i=0; i build = + [&](int pFront, int pBack, int iFront, int iBack)->TreeNode*{ + if(pFront > pBack || iFront > iBack) return NULL; + auto root = new TreeNode(preorder[pFront]); + int iRoot = inPos[root->val]; + print(iRoot, ": [",pFront,",",pBack,"] [",iFront,",",iBack,"]\n") + int leftSize = iRoot - iFront, rightSize = iBack - iRoot; + root->left = build(pFront+1, pFront+leftSize, iFront, iRoot-1); + root->right= build(pFront+leftSize+1, pBack, iRoot+1, iBack); + return root; + }; + return build(0, n-1, 0, n-1); + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file diff --git "a/questions/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/main.cpp" "b/questions/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e1fbb76d2a19e6cd72968f09f005db57cc7ca568 --- /dev/null +++ "b/questions/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"abcde\\\"\",\"\\\"ace\\\"\"],[\"\\\"abc\\\"\",\"\\\"abc\\\"\"],[\"\\\"abc\\\"\",\"\\\"def\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + string arg1 = parseString(args[1]); + int result=s->longestCommonSubsequence(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/solution.h" "b/questions/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..ca50790b3377c00ed4654e38555249aecd77a9f9 --- /dev/null +++ "b/questions/1143.\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/solution.h" @@ -0,0 +1,20 @@ +// @algorithm @lc id=1250 lang=cpp +// @title longest-common-subsequence + + +// @test("abcde","ace")=3 +// @test("abc","abc")=3 +// @test("abc","def")=0 +class Solution { +public: + int longestCommonSubsequence(string text1, string text2) { + int m = text1.size(), n = text2.size(); + vector> dp(m+1, vector(n+1, 0)); + for(int i=0; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->maxProfit(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/solution.h" "b/questions/121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..3efce24985cc9739ac2f7724598b754ebd425510 --- /dev/null +++ "b/questions/121.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272/solution.h" @@ -0,0 +1,19 @@ +// @algorithm @lc id=121 lang=cpp +// @title best-time-to-buy-and-sell-stock + + +// @test([7,1,5,3,6,4])=5 +// @test([7,6,4,3,1])=0 +class Solution { +public: + int maxProfit(vector& prices) { + auto everLow = INT_MAX, earn = 0; + for(auto & p : prices){ + if(p < everLow) + everLow = p; + else if(earn < p - everLow) + earn = p - everLow; + } + return earn; + } +}; \ No newline at end of file diff --git "a/questions/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214/main.cpp" "b/questions/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..fb2c965d05236f95dcfcbeb1707acb32ac317340 --- /dev/null +++ "b/questions/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,3]\"],[\"[-10,9,20,null,null,15,7]\"],[\"[2,-1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + TreeNode * arg0 = parseTreeNode(args[0]); + int result=s->maxPathSum(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214/solution.h" "b/questions/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..8ee49b09677d8357ecffac81bfb0db45452454e8 --- /dev/null +++ "b/questions/124.\344\272\214\345\217\211\346\240\221\344\270\255\347\232\204\346\234\200\345\244\247\350\267\257\345\276\204\345\222\214/solution.h" @@ -0,0 +1,33 @@ +// @algorithm @lc id=124 lang=cpp +// @title binary-tree-maximum-path-sum + + +// @test([1,2,3])=6 +// @test([-10,9,20,null,null,15,7])=42 +// @test([2,-1])=2 +class Solution { +public: + int maxPathSum(TreeNode* root) { + int ans = INT_MIN; + function traverse = [&](TreeNode* tree)->int{ + if(!tree) return 0; + auto left = max(0, traverse(tree->left)), + right = max(0, traverse(tree->right)); + ans = max(ans, tree->val + left + right); + return tree->val + max(left, right); + }; + traverse(root); + return ans; + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file diff --git "a/questions/128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/main.cpp" "b/questions/128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2bc83f20176e837332938189e0a910d3c4b16ef0 --- /dev/null +++ "b/questions/128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[100,4,200,1,3,2]\"],[\"[0,3,7,2,5,8,4,6,0,1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->longestConsecutive(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/solution.h" "b/questions/128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c490f9a0e4849e3c1ca1a801aa0bb63572000a8e --- /dev/null +++ "b/questions/128.\346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/solution.h" @@ -0,0 +1,23 @@ +// @algorithm @lc id=128 lang=cpp +// @title longest-consecutive-sequence + + +// @test([100,4,200,1,3,2])=4 +// @test([0,3,7,2,5,8,4,6,0,1])=9 +class Solution { +public: + int longestConsecutive(vector& nums) { + if(nums.size() < 2) return nums.size(); + unordered_set exist; + for(auto & t : nums) + exist.insert(t); + int ans = 0; + for(auto t : nums){ + if(exist.count(t-1)) continue; + int s = t; + while(exist.count(++t)); + ans = max(ans, t-s); + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/main.cpp" "b/questions/136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2254843358a2c21cc6c71cf07af1d760ffcb9a8c --- /dev/null +++ "b/questions/136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[2,2,1]\"],[\"[4,1,2,1,2]\"],[\"[1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->singleNumber(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/solution.h" "b/questions/136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..e615cf4405a38ed0fed2adcdb9edda2df8c7b222 --- /dev/null +++ "b/questions/136.\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/solution.h" @@ -0,0 +1,15 @@ +// @algorithm @lc id=136 lang=cpp +// @title single-number + + +// @test([2,2,1])=1 +// @test([4,1,2,1,2])=4 +// @test([1])=1 +class Solution { +public: + int singleNumber(vector& nums) { + int ans = 0; + for(auto & n : nums) ans ^= n; + return ans; + } +}; \ No newline at end of file diff --git "a/questions/139.\345\215\225\350\257\215\346\213\206\345\210\206/main.cpp" "b/questions/139.\345\215\225\350\257\215\346\213\206\345\210\206/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1acf6ceb2066c441a0f5407ae369e66a3599c009 --- /dev/null +++ "b/questions/139.\345\215\225\350\257\215\346\213\206\345\210\206/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"leetcode\\\"\",\"[\\\"leet\\\",\\\"code\\\"]\"],[\"\\\"applepenapple\\\"\",\"[\\\"apple\\\",\\\"pen\\\"]\"],[\"\\\"catsandog\\\"\",\"[\\\"cats\\\",\\\"dog\\\",\\\"sand\\\",\\\"and\\\",\\\"cat\\\"]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + vector arg1 = parseStringArr(args[1]); + bool result=s->wordBreak(arg0,arg1); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/139.\345\215\225\350\257\215\346\213\206\345\210\206/solution.h" "b/questions/139.\345\215\225\350\257\215\346\213\206\345\210\206/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..dab4296cc77ea1ace7bedf05d38430220fc9043f --- /dev/null +++ "b/questions/139.\345\215\225\350\257\215\346\213\206\345\210\206/solution.h" @@ -0,0 +1,31 @@ +// @algorithm @lc id=139 lang=cpp +// @title word-break + + +// @test("leetcode",["leet","code"])=true +// @test("applepenapple",["apple","pen"])=true +// @test("catsandog",["cats","dog","sand","and","cat"])=false +class Solution { +public: + bool wordBreak(string s, vector& wordDict) { + unordered_set dict; + dict.insert(""); + size_t maxlen = 0; // 最大单词长度 + for(auto & word : wordDict){ + dict.insert(word); + maxlen = max(maxlen, word.size()); + } + list dp; // 有效单词右界(end) + dp.push_back(0); // 空串视为有效 + for(int i=0; i<=s.size(); i++){ + for(auto it=dp.rbegin(); it!=dp.rend(); it++){ + if(dict.count(s.substr(*it, i-*it))){ + dp.push_back(i); + break; + } + if(i-*it>=maxlen) break; + } + } + return dp.back() == s.size(); + } +}; \ No newline at end of file diff --git "a/questions/142.\347\216\257\345\275\242\351\223\276\350\241\250 II/solution.h" "b/questions/142.\347\216\257\345\275\242\351\223\276\350\241\250 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..085ecc65a5e4137eae4cc1167b01b6c889a2b462 --- /dev/null +++ "b/questions/142.\347\216\257\345\275\242\351\223\276\350\241\250 II/solution.h" @@ -0,0 +1,36 @@ +// @algorithm @lc id=142 lang=cpp +// @title linked-list-cycle-ii + + +// @test([3,2,0,-4],1)=1 +// @test([1,2],0)=0 +// @test([1],0)=0 +// @test([1],-1)=-1 +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + ListNode *slow = head, *fast = head; + while(fast){ + if(!fast->next) return NULL; + fast = fast->next->next; + slow = slow->next; + if(slow==fast){ + fast = head; + while(fast != slow){ + fast = fast->next; + slow = slow->next; + } + return fast; + } + } + return NULL; + } +}; +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ \ No newline at end of file diff --git "a/questions/148.\346\216\222\345\272\217\351\223\276\350\241\250/main.cpp" "b/questions/148.\346\216\222\345\272\217\351\223\276\350\241\250/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..39d9b46cd465963910a7affa483451fd7b707f5d --- /dev/null +++ "b/questions/148.\346\216\222\345\272\217\351\223\276\350\241\250/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "linkedlist.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[4,2,1,3]\"],[\"[-1,5,3,4,0]\"],[\"[]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + ListNode * arg0 = parseListNode(args[0]); + ListNode * result=s->sortList(arg0); + string resultabc =serializeListNode(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/148.\346\216\222\345\272\217\351\223\276\350\241\250/solution.h" "b/questions/148.\346\216\222\345\272\217\351\223\276\350\241\250/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..e3b693ecac298d4febeefeecbc9365b6c44d30d1 --- /dev/null +++ "b/questions/148.\346\216\222\345\272\217\351\223\276\350\241\250/solution.h" @@ -0,0 +1,61 @@ +// @algorithm @lc id=148 lang=cpp +// @title sort-list + + +#define print(...) +// @test([4,2,1,3])=[1,2,3,4] +// @test([-1,5,3,4,0])=[-1,0,3,4,5] +// @test([])=[] +class Solution { +public: + ListNode* sortList(ListNode* head) { + if(!head) return NULL; + print("\nlist=", head, "\n") + ListNode dummy(0, head), *cur = &dummy; + int n = 0; while((cur = cur->next)) n++; + for(int s=1; snext; + while(cur){ + auto h1 = cur; + for(int i=1; inext; i++) + cur = cur->next; + + auto h2 = cur->next; cur->next = NULL; cur = h2; + for(int i=1; inext; + ListNode* NEXT = NULL; + if(cur){ + NEXT = cur->next; + cur->next = NULL; + } + last->next = mergeTwoLists(h1, h2); + while(last->next) last=last->next; + cur = NEXT; + } + } + print("ordered:", dummy.next, "\n") + return dummy.next; + } + +ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode emptyHead, *pre = &emptyHead; + while(l1 && l2){ + auto s = (l1->val <= l2->val ? &l1 : &l2); + pre->next = *s; + pre = *s; + *s = (*s)->next; + } + pre->next = (l1 ? l1 : l2); + return emptyHead.next; +} +}; +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ \ No newline at end of file diff --git "a/questions/152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204/main.cpp" "b/questions/152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..3287a238996923f3339fa346a98e409f7a22ecf7 --- /dev/null +++ "b/questions/152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[2,3,-2,4]\"],[\"[-2,0,-1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->maxProduct(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204/solution.h" "b/questions/152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..63b06d3309ddcbacfdfbd66f0f60f99df8956514 --- /dev/null +++ "b/questions/152.\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204/solution.h" @@ -0,0 +1,18 @@ +// @algorithm @lc id=152 lang=cpp +// @title maximum-product-subarray + + +// #define print(...) +// @test([2,3,-2,4])=6 +// @test([-2,0,-1])=0 +class Solution { +public: + int maxProduct(vector& nums) { + vector upper(nums), lower(nums); + for(int i=1; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int arg1 = parseInteger(args[1]); + string result=s->fractionToDecimal(arg0,arg1); + string resultabc =serializeString(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/166.\345\210\206\346\225\260\345\210\260\345\260\217\346\225\260/solution.h" "b/questions/166.\345\210\206\346\225\260\345\210\260\345\260\217\346\225\260/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..f1f35b2fe9720c20c03df98dee1357673f823c68 --- /dev/null +++ "b/questions/166.\345\210\206\346\225\260\345\210\260\345\260\217\346\225\260/solution.h" @@ -0,0 +1,38 @@ +// @algorithm @lc id=166 lang=cpp +// @title fraction-to-recurring-decimal + + +// #define print(...) +// @test(1,2)="0.5" +// @test(2,1)="2" +// @test(2,3)="0.(6)" +// @test(4,333)="0.(012)" +// @test(1,5)="0.2" +class Solution { +public: + string fractionToDecimal(int numerator, int denominator) { + string ans; + long n = numerator, d = denominator; + if(n%d == 0) return to_string(n/d); + // 符号 + if(n>0 ^ d>0) ans += '-'; + // 整数部分 + n = abs(n), d = abs(d); + ans += to_string(n/d); + ans += '.'; + // 小数部分 + unordered_map ni; + long r = n%d, i = ans.size(); + while(r && !ni.count(r)){ + ni[r] = i++; + ans.push_back(r*10/d + '0'); + r = r*10%d; + } + // 循环 + if(r){ + ans.insert(ans.begin()+ni[r], '('); + ans.push_back(')'); + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/1894.\346\211\276\345\210\260\351\234\200\350\246\201\350\241\245\345\205\205\347\262\211\347\254\224\347\232\204\345\255\246\347\224\237\347\274\226\345\217\267/main.cpp" "b/questions/1894.\346\211\276\345\210\260\351\234\200\350\246\201\350\241\245\345\205\205\347\262\211\347\254\224\347\232\204\345\255\246\347\224\237\347\274\226\345\217\267/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..482019e149159ce99361f9e313beeec653a97c1d --- /dev/null +++ "b/questions/1894.\346\211\276\345\210\260\351\234\200\350\246\201\350\241\245\345\205\205\347\262\211\347\254\224\347\232\204\345\255\246\347\224\237\347\274\226\345\217\267/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[5,1,5]\",\"22\"],[\"[3,4,1,2]\",\"25\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->chalkReplacer(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/1894.\346\211\276\345\210\260\351\234\200\350\246\201\350\241\245\345\205\205\347\262\211\347\254\224\347\232\204\345\255\246\347\224\237\347\274\226\345\217\267/solution.h" "b/questions/1894.\346\211\276\345\210\260\351\234\200\350\246\201\350\241\245\345\205\205\347\262\211\347\254\224\347\232\204\345\255\246\347\224\237\347\274\226\345\217\267/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..99f9d0d6505f9c7c1d29687ce341d1172671f587 --- /dev/null +++ "b/questions/1894.\346\211\276\345\210\260\351\234\200\350\246\201\350\241\245\345\205\205\347\262\211\347\254\224\347\232\204\345\255\246\347\224\237\347\274\226\345\217\267/solution.h" @@ -0,0 +1,17 @@ +// @algorithm @lc id=2006 lang=cpp +// @title find-the-student-that-will-replace-the-chalk + + +// @test([5,1,5],22)=0 +// @test([3,4,1,2],25)=1 +class Solution { +public: + int chalkReplacer(vector& chalk, int k) { + for(int i=0; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + vector> arg1 = parseIntegerArrArr(args[1]); + int result=s->countPaths(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/1976.\345\210\260\350\276\276\347\233\256\347\232\204\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260/solution.h" "b/questions/1976.\345\210\260\350\276\276\347\233\256\347\232\204\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..da1a4cfcad1c86c1593557c3c9c1b6c58211de77 --- /dev/null +++ "b/questions/1976.\345\210\260\350\276\276\347\233\256\347\232\204\345\234\260\347\232\204\346\226\271\346\241\210\346\225\260/solution.h" @@ -0,0 +1,55 @@ +// @algorithm @lc id=2090 lang=cpp +// @title number-of-ways-to-arrive-at-destination +// 「记忆化搜索」对应着「深度优先搜索」求解拓扑排序 +// 「动态规划」对应着「广度优先搜索」求解拓扑排序 + + +// #define print(...) +// @test(7,[[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]])=4 +// @test(2,[[1,0,10]])=1 +class Solution { +public: + int countPaths(int n, vector>& roads) { + const int mod = 1000'000'000 + 7; + vector> path(n, vector(n, LLONG_MAX/2)); // 路网 + for(auto & way : roads) + path[way[0]][way[1]] = path[way[1]][way[0]] = way[2]; + for(int i=0; i passed(n, false); // Dijkstra算法求(0到各点)单源最短路径 + auto & to = path[0]; // path[0][ ]: 从 Zero 到各点的距离 + int c = n; + while(c--){ + int j = -1; + for(int i=0; i> G(n); // (从0出发)构造有向图 + for(auto & way : roads){ + auto near = way[to[way[0]] < to[way[1]] ? 0 : 1], + far = -near+way[0]+way[1]; + if(to[near] + way[2] == to[far]) // 搜集最短路径上的点 + G[near].push_back(far); + } + + vector from(n, 0); // 记忆化搜索有向图上到达n-1的路径数 + function dfs = [&](int src)->int{ + if(src == n-1) return 1; + if(from[src]) return from[src]; + for(auto far : G[src]){ + from[src] += dfs(far); + if(from[src] >= mod) + from[src] -= mod; + } + return from[src]; + }; + return dfs(0); + } +}; \ No newline at end of file diff --git "a/questions/198.\346\211\223\345\256\266\345\212\253\350\210\215/main.cpp" "b/questions/198.\346\211\223\345\256\266\345\212\253\350\210\215/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..55b5fb5a35bf7ec44d5187f47e603b32a9410969 --- /dev/null +++ "b/questions/198.\346\211\223\345\256\266\345\212\253\350\210\215/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,3,1]\"],[\"[2,7,9,3,1]\"],[\"[1]\"],[\"[2,1,1,2]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->rob(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/198.\346\211\223\345\256\266\345\212\253\350\210\215/solution.h" "b/questions/198.\346\211\223\345\256\266\345\212\253\350\210\215/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..3fe0a31163c7f100e61bfa555094175d63633f14 --- /dev/null +++ "b/questions/198.\346\211\223\345\256\266\345\212\253\350\210\215/solution.h" @@ -0,0 +1,19 @@ +// @algorithm @lc id=198 lang=cpp +// @title house-robber + + +// @test([1,2,3,1])=4 +// @test([2,7,9,3,1])=12 +// @test([1])=1 +// @test([2,1,1,2])=4 +class Solution { +public: + int rob(vector& nums) { + vector b{0, nums[0]}, c{0, nums[0]}; + for(int i=1; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseStringArrArr(args[0]); + vector> ts; + if(arg0.size() > 0) ts = vector>(arg0.size(), vector(arg0.size(), ' ')); + vv_conv(arg0, ts, [](const string&s){return s[0];}); + int result=s->numIslands(ts); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/200.\345\262\233\345\261\277\346\225\260\351\207\217/solution.h" "b/questions/200.\345\262\233\345\261\277\346\225\260\351\207\217/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..a8eeae70b183cc5a1bf38bc8043a2a23ce57b513 --- /dev/null +++ "b/questions/200.\345\262\233\345\261\277\346\225\260\351\207\217/solution.h" @@ -0,0 +1,29 @@ +// @algorithm @lc id=200 lang=cpp +// @title number-of-islands + + +// #define print(...) +// @test([["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]])=1 +// @test([["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]])=3 +class Solution { +public: + int numIslands(vector>& grid) { + print("\ngrid=\n", Mat(grid)) + int m = grid.size(), n = grid[0].size(); + int ans = 0, dir[][2] = {{0,1}, {1,0}, {0, -1}, {-1, 0}}, F='A'; + function dfs = [&](int i, int j){ + grid[i][j] = F; + for(auto &[x, y] : dir) + if(0<=i+x && i+x> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + vector> arg1 = parseIntegerArrArr(args[1]); + bool result=s->canFinish(arg0,arg1); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/207.\350\257\276\347\250\213\350\241\250/solution.h" "b/questions/207.\350\257\276\347\250\213\350\241\250/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..db03167cc380c9e90efa2eae7348265497653165 --- /dev/null +++ "b/questions/207.\350\257\276\347\250\213\350\241\250/solution.h" @@ -0,0 +1,37 @@ +// @algorithm @lc id=207 lang=cpp +// @title course-schedule + + +// @test(2,[[1,0]])=true +// @test(2,[[1,0],[0,1]])=false +enum State { + TO_LEARN = 0, + LEARNING, + FINISHED, +}; +class Solution { +public: + bool canFinish(int numCourses, vector>& prerequisites) { + vector> dG(numCourses); // [course] --> [ next courses ... ] + for(auto & req : prerequisites) + dG[ req[1] ].push_back( req[0] ); + + vector cs(numCourses, TO_LEARN); + function CanLearn = [&](int i){ + if(cs[i] == FINISHED) + return true; + + cs[i] = LEARNING; + for(auto next : dG[i]) + if(cs[next] == LEARNING || !CanLearn(next)) + return false; + + cs[i] = FINISHED; + return true; + }; + + for(int i=0; i dict; +public: + /** Initialize your data structure here. */ + Trie() : end(false), dict(26, NULL) + { + } + + /** Inserts a word into the trie. */ + void insert(string word) { + auto node = this; + for(auto &c : word){ + auto it = node->dict.begin()+c-'a'; + if(!*it)*it = new Trie(); + node = *it; + } + node->end = true; + } + Trie * match(string word) + { + auto node = this; + for(auto &c : word){ + auto it = node->dict.begin()+c-'a'; + if(!*it) return NULL; + node = *it; + } + return node; + } + + /** Returns if the word is in the trie. */ + bool search(string word) { + auto node = match(word); + return node ? node->end : false; + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + bool startsWith(string prefix) { + return match(prefix); + } +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ \ No newline at end of file diff --git "a/questions/210.\350\257\276\347\250\213\350\241\250 II/main.cpp" "b/questions/210.\350\257\276\347\250\213\350\241\250 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c8b2dfe968820c3cfce67eb1a52e1180886d8634 --- /dev/null +++ "b/questions/210.\350\257\276\347\250\213\350\241\250 II/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"2\",\"[[1,0]]\"],[\"4\",\"[[1,0],[2,0],[3,1],[3,2]]\"],[\"1\",\"[]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + vector> arg1 = parseIntegerArrArr(args[1]); + vector result=s->findOrder(arg0,arg1); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/210.\350\257\276\347\250\213\350\241\250 II/solution.h" "b/questions/210.\350\257\276\347\250\213\350\241\250 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..8343975d952719204b220c0f43a64c35235a144d --- /dev/null +++ "b/questions/210.\350\257\276\347\250\213\350\241\250 II/solution.h" @@ -0,0 +1,32 @@ +// @algorithm @lc id=210 lang=cpp +// @title course-schedule-ii + + +// @test(2,[[1,0]])=[0,1] +// @test(4,[[1,0],[2,0],[3,1],[3,2]])=[0,2,1,3] +// @test(1,[])=[0] +class Solution { +public: + vector findOrder(int numCourses, vector>& prerequisites) { + vector> dG(numCourses); // [course] --> [ next courses ... ] + vector indeg(numCourses, 0); + for(auto & req : prerequisites){ + dG[ req[1] ].push_back( req[0] ); // req[1] --> req[0] + indeg[ req[0] ]++; + } + + queue canLearn; + for(int i=0; i ans; + while(!canLearn.empty()){ + int fin = canLearn.front(); canLearn.pop(); + ans.push_back(fin); + for(auto next : dG[fin]) + if(0 == --indeg[next]) + canLearn.push(next); + } + if(ans.size() == numCourses) return ans; + return {}; + } +}; \ No newline at end of file diff --git "a/questions/212.\345\215\225\350\257\215\346\220\234\347\264\242 II/main.cpp" "b/questions/212.\345\215\225\350\257\215\346\220\234\347\264\242 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b8e681aa0c9a3d70bb45ab80ff45a0a0828e7e65 --- /dev/null +++ "b/questions/212.\345\215\225\350\257\215\346\220\234\347\264\242 II/main.cpp" @@ -0,0 +1,21 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[\\\"o\\\",\\\"a\\\",\\\"a\\\",\\\"n\\\"],[\\\"e\\\",\\\"t\\\",\\\"a\\\",\\\"e\\\"],[\\\"i\\\",\\\"h\\\",\\\"k\\\",\\\"r\\\"],[\\\"i\\\",\\\"f\\\",\\\"l\\\",\\\"v\\\"]]\",\"[\\\"oath\\\",\\\"pea\\\",\\\"eat\\\",\\\"rain\\\"]\"],[\"[[\\\"a\\\",\\\"b\\\"],[\\\"c\\\",\\\"d\\\"]]\",\"[\\\"abcb\\\"]\"],[\"[[\\\"o\\\",\\\"a\\\",\\\"b\\\",\\\"n\\\"],[\\\"o\\\",\\\"t\\\",\\\"a\\\",\\\"e\\\"],[\\\"a\\\",\\\"h\\\",\\\"k\\\",\\\"r\\\"],[\\\"a\\\",\\\"f\\\",\\\"l\\\",\\\"v\\\"]]\",\"[\\\"oa\\\",\\\"oaa\\\"]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseStringArrArr(args[0]); + vector> data(arg0.size(), vector(arg0[0].size())); + vv_conv(arg0, data, [](const string&s)->char{return s[0];}); + vector arg1 = parseStringArr(args[1]); + vector result=s->findWords(data,arg1); + string resultabc =serializeStringArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/212.\345\215\225\350\257\215\346\220\234\347\264\242 II/solution.h" "b/questions/212.\345\215\225\350\257\215\346\220\234\347\264\242 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..53292731d9efaebc2bcd35ce6c54abe6a1328ff4 --- /dev/null +++ "b/questions/212.\345\215\225\350\257\215\346\220\234\347\264\242 II/solution.h" @@ -0,0 +1,55 @@ +// @algorithm @lc id=212 lang=cpp +// @title word-search-ii + + +#define print(...) +// @test([["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]],["oath","pea","eat","rain"])=["eat","oath"] +// @test([["a","b"],["c","d"]],["abcb"])=[] +// @test([["o","a","b","n"],["o","t","a","e"],["a","h","k","r"],["a","f","l","v"]], ["oa","oaa"])=["oa","oaa"] +class Solution { + struct Trie{ + bool used; + string word; + vector> children; + Trie(string w=""):used(false),word(w),children(26, nullptr){} + }; + shared_ptr dict = make_shared(); +public: + vector findWords(vector>& board, vector& words) { + print("\n", words, "\n") + print(Mat(board)) + for(auto & w : words){ + auto node = dict; + for(auto &c : w){ + auto it = node->children.begin() + c - 'a'; + if(!*it) *it = make_shared(); + node = *it; + } + node->word = w; + } + + pair dir[] = { {0,1}, {0,-1}, {1,0}, {-1,0} }; + int m = board.size(), n = board[0].size(); + vector ans; + function)> dfs = + [&](int i, int j, shared_ptr node){ + if(!node) return; + if(node->word.size() && !node->used){ + ans.push_back(node->word); + node->used = true; + } + auto ch = board[i][j]; + board[i][j] = '#'; + for(auto &[di, dj] : dir){ + if(i+di<0||m<=i+di || j+dj<0||n<=j+dj) continue; + if('#' == board[i+di][j+dj]) continue; + dfs(i+di, j+dj, node->children[board[i+di][j+dj]-'a']); + } + board[i][j] = ch; + }; + for(int i=0; ichildren[board[i][j]-'a']); + return ans; + } +}; \ No newline at end of file diff --git "a/questions/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/main.cpp" "b/questions/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..233de9d5001b7f4c97b1cab0e927598f0075df8b --- /dev/null +++ "b/questions/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/main.cpp" @@ -0,0 +1,22 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[\\\"1\\\",\\\"0\\\",\\\"1\\\",\\\"0\\\",\\\"0\\\"],[\\\"1\\\",\\\"0\\\",\\\"1\\\",\\\"1\\\",\\\"1\\\"],[\\\"1\\\",\\\"1\\\",\\\"1\\\",\\\"1\\\",\\\"1\\\"],[\\\"1\\\",\\\"0\\\",\\\"0\\\",\\\"1\\\",\\\"0\\\"]]\"],[\"[[\\\"0\\\",\\\"1\\\"],[\\\"1\\\",\\\"0\\\"]]\"],[\"[[\\\"0\\\"]]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseStringArrArr(args[0]); + vector> ts; + if(arg0.size() && arg0[0].size()) + ts = vector>(arg0.size(), vector(arg0[0].size())); + vv_conv(arg0, ts, [](const string&f){return f[0];}); + int result=s->maximalSquare(ts); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/solution.h" "b/questions/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..156430250e953bb40c11760fa0d0e3dabba95620 --- /dev/null +++ "b/questions/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242/solution.h" @@ -0,0 +1,30 @@ +// @algorithm @lc id=221 lang=cpp +// @title maximal-square + + +// @test([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]])=4 +// @test([["0","1"],["1","0"]])=1 +// @test([["0"]])=0 +class Solution { +public: + int maximalSquare(vector>& matrix) { + int m = matrix.size(); + if(m == 0 || matrix[0].size() == 0) return 0; + int n = matrix[0].size(); + vector> dp(m, vector(n, 0)); + int ans = 0; + for(int i=0; i ans) + ans = dp[i][j]; + } + } + return ans*ans; + } +}; \ No newline at end of file diff --git "a/questions/223.\347\237\251\345\275\242\351\235\242\347\247\257/main.cpp" "b/questions/223.\347\237\251\345\275\242\351\235\242\347\247\257/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f67c5527e935661755ccc0c10b20383c2916a945 --- /dev/null +++ "b/questions/223.\347\237\251\345\275\242\351\235\242\347\247\257/main.cpp" @@ -0,0 +1,25 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"-3\",\"0\",\"3\",\"4\",\"0\",\"-1\",\"9\",\"2\"],[\"-2\",\"-2\",\"2\",\"2\",\"-2\",\"-2\",\"2\",\"2\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int arg1 = parseInteger(args[1]); + int arg2 = parseInteger(args[2]); + int arg3 = parseInteger(args[3]); + int arg4 = parseInteger(args[4]); + int arg5 = parseInteger(args[5]); + int arg6 = parseInteger(args[6]); + int arg7 = parseInteger(args[7]); + int result=s->computeArea(arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/223.\347\237\251\345\275\242\351\235\242\347\247\257/solution.h" "b/questions/223.\347\237\251\345\275\242\351\235\242\347\247\257/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c1971df4002d5984ed817251832af04e7e1f5b22 --- /dev/null +++ "b/questions/223.\347\237\251\345\275\242\351\235\242\347\247\257/solution.h" @@ -0,0 +1,17 @@ +// @algorithm @lc id=223 lang=cpp +// @title rectangle-area + + +// @test(-3,0,3,4,0,-1,9,2)=45 +// @test(-2,-2,2,2,-2,-2,2,2)=16 +class Solution { +public: + int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { + int sa = (ax2 -ax1) * (ay2 - ay1); + int sb = (bx2 -bx1) * (by2 - by1); + int dx = min(ax2, bx2) - max(ax1, bx1); + int dy = min(ay2, by2) - max(ay1, by1); + int ds = max(0, dx) * max(0, dy); + return sa + sb - ds; + } +}; \ No newline at end of file diff --git "a/questions/230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/main.cpp" "b/questions/230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..0846b59c39a4d98a1c0120eae6620e2cc40d5d68 --- /dev/null +++ "b/questions/230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/main.cpp" @@ -0,0 +1,20 @@ +#include "stl.h" +#include "parse.h" +#include +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[3,1,4,null,2]\",\"1\"],[\"[5,3,6,2,4,null,null,1]\",\"3\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + TreeNode * arg0 = parseTreeNode(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->kthSmallest(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/solution.h" "b/questions/230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..977322227294e8959c186278ca573cbc1f237172 --- /dev/null +++ "b/questions/230.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240/solution.h" @@ -0,0 +1,34 @@ +// @algorithm @lc id=230 lang=cpp +// @title kth-smallest-element-in-a-bst + + +// #define print(...) +// @test([3,1,4,null,2],1)=1 +// @test([5,3,6,2,4,null,null,1],3)=3 +class Solution { +public: + int kthSmallest(TreeNode* root, int k) { + stack stk; + while(root || stk.size()){ + while(root){ + stk.push(root); + root = root->left; + } + root = stk.top(); stk.pop(); + if(0 == --k) break; + root = root->right; + } + return root->val; + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file diff --git "a/questions/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.cpp" "b/questions/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5aa10a2cb358bc346ee498867f2aa8d9b72c1d1a --- /dev/null +++ "b/questions/234.\345\233\236\346\226\207\351\223\276\350\241\250/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "linkedlist.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,2,1]\"],[\"[1,2]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + ListNode * arg0 = parseListNode(args[0]); + bool result=s->isPalindrome(arg0); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/234.\345\233\236\346\226\207\351\223\276\350\241\250/solution.h" "b/questions/234.\345\233\236\346\226\207\351\223\276\350\241\250/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..34dc063b720bd006681858a1ccb988efccedfa04 --- /dev/null +++ "b/questions/234.\345\233\236\346\226\207\351\223\276\350\241\250/solution.h" @@ -0,0 +1,52 @@ +// @algorithm @lc id=234 lang=cpp +// @title palindrome-linked-list + + +#define print(...) +// @test([1,2,2,1])=true +// @test([1,2])=false +class Solution { +public: + ListNode * Reverse(ListNode * head){ + ListNode *cur = head, *pre = NULL; + while(cur){ + auto next = cur->next; + cur->next = pre; + pre = cur; + cur = next; + } + return pre; + } + ListNode * Half(ListNode * head){ + auto half = head; + while(head->next && head->next->next){ + half = half->next; + head = head->next->next; + } + return half; + } + bool isPalindrome(ListNode* head) { + auto half = Half(head); + auto rev = Reverse(half->next); + print("head=", head, "\n") + print("rev=", rev, "\n") + while(head && rev){ + if(head->val != rev->val) + return false; + head = head->next; + rev = rev->next; + } + return true; + + } +}; +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ \ No newline at end of file diff --git "a/questions/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/solution.h" "b/questions/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..de9dafc452a4dcb3701e69b963cf1e5c273dced4 --- /dev/null +++ "b/questions/236.\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/solution.h" @@ -0,0 +1,20 @@ +// @algorithm @lc id=236 lang=cpp +// @title lowest-common-ancestor-of-a-binary-tree + +class Solution { +public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + TreeNode * ans = NULL; + function dfs = [&](TreeNode * node)->bool{ + if(ans || !node) return false; + bool lc = dfs(node->left), rc = dfs(node->right); + if((lc || rc) && (node==p || node==q) || (lc && rc)){ + ans = node; + return true; + } + return node == q || node == p || lc || rc; + }; + dfs(root); + return ans; + } +}; \ No newline at end of file diff --git "a/questions/238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/main.cpp" "b/questions/238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1e9d5172756fdfb7f68b910e072a647f4ed1a4ac --- /dev/null +++ "b/questions/238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,3,4]\"],[\"[-1,1,0,-3,3]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector result=s->productExceptSelf(arg0); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/solution.h" "b/questions/238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..025e44424327b650ba786085592c7aec5f55e30c --- /dev/null +++ "b/questions/238.\351\231\244\350\207\252\350\272\253\344\273\245\345\244\226\346\225\260\347\273\204\347\232\204\344\271\230\347\247\257/solution.h" @@ -0,0 +1,21 @@ +// @algorithm @lc id=238 lang=cpp +// @title product-of-array-except-self + + +// @test([1,2,3,4])=[24,12,8,6] +// @test([-1,1,0,-3,3])=[0,0,9,0,0] +class Solution { +public: + vector productExceptSelf(vector& nums) { + int n = nums.size(); + vector ans(n, 1); + for(int i=1; i=0; i--){ + ans[i] *= R; + R *= nums[i]; + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/240.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II/main.cpp" "b/questions/240.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e2c6f1edecb81653ceead14dcad090dc1852e18e --- /dev/null +++ "b/questions/240.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\",\"5\"],[\"[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\",\"20\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + int arg1 = parseInteger(args[1]); + bool result=s->searchMatrix(arg0,arg1); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/240.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II/solution.h" "b/questions/240.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..d9202e0623e4d307d3365570996be83b8ce84b16 --- /dev/null +++ "b/questions/240.\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II/solution.h" @@ -0,0 +1,22 @@ +// @algorithm @lc id=240 lang=cpp +// @title search-a-2d-matrix-ii + + +// @test([[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],5)=true +// @test([[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],20)=false +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + int m = matrix.size(), n = matrix[0].size(); + int row = m - 1, col = 0; + while(0 <= row && col < n){ + if(target < matrix[row][col]) + -- row; + else if(matrix[row][col] < target) + ++ col; + else + return true; + } + return false; + } +}; \ No newline at end of file diff --git "a/questions/253.\344\274\232\350\256\256\345\256\244 II/main.cpp" "b/questions/253.\344\274\232\350\256\256\345\256\244 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..552cbc20ec71113b9f31d4cf9cd452bdc6518eb5 --- /dev/null +++ "b/questions/253.\344\274\232\350\256\256\345\256\244 II/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "solution.h" + +int main(int argc, char *argv[]){ + vector< vector > tests[] = { + {{0,30},{5,10},{15,20}}, + {{7,10},{2,4}}, + }; + Solution sln; + for(auto & t : tests){ + print("input=", t, "\n") + int ans = sln.minMeetingRooms(t); + print("ans:", ans, "\n") + } + + print("\n------ main exit ------\n") + return 0; +} diff --git "a/questions/253.\344\274\232\350\256\256\345\256\244 II/solution.h" "b/questions/253.\344\274\232\350\256\256\345\256\244 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..871237bd8ef1958fb2b9d4de4cb372f33d13f2a8 --- /dev/null +++ "b/questions/253.\344\274\232\350\256\256\345\256\244 II/solution.h" @@ -0,0 +1,19 @@ +// @algorithm @lc id=253 lang=cpp +// @title meeting-rooms-ii + +// @test([[0,30],[5,10],[15,20]])=2 +// @test([[7,10],[2,4]])=1 +class Solution { +public: + int minMeetingRooms(vector>& intervals) { + priority_queue, greater> minHeap; + sort(intervals.begin(), intervals.end()); + for(auto & c : intervals){ + auto start = c[0], fin = c[1]; + if(minHeap.size() && minHeap.top()<= start) + minHeap.pop(); + minHeap.push(fin); + } + return minHeap.size(); + } +}; diff --git "a/questions/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/main.cpp" "b/questions/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5f1077dfbf94312790c185aa7ca6b617e1ec8690 --- /dev/null +++ "b/questions/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"12\"],[\"13\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int result=s->numSquares(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/solution.h" "b/questions/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..f9cdcb6454f2e06f6424a66bbd3580b3c5b50470 --- /dev/null +++ "b/questions/279.\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260/solution.h" @@ -0,0 +1,19 @@ +// @algorithm @lc id=279 lang=cpp +// @title perfect-squares + + +// @test(12)=3 +// @test(13)=2 +class Solution { +public: + int numSquares(int n) { + vector dp(n+1, 0); + for(int i=1; i<=n; i++){ + int s = INT_MAX; + for(int j=1; j*j<=i; j++) + s = min(s, dp[i-j*j]); + dp[i] = s + 1; + } + return dp[n]; + } +}; \ No newline at end of file diff --git "a/questions/282.\347\273\231\350\241\250\350\276\276\345\274\217\346\267\273\345\212\240\350\277\220\347\256\227\347\254\246/main.cpp" "b/questions/282.\347\273\231\350\241\250\350\276\276\345\274\217\346\267\273\345\212\240\350\277\220\347\256\227\347\254\246/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..764c09e52eee222ca4ad5d652d32f043187103fe --- /dev/null +++ "b/questions/282.\347\273\231\350\241\250\350\276\276\345\274\217\346\267\273\345\212\240\350\277\220\347\256\227\347\254\246/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"123\\\"\",\"6\"],[\"\\\"232\\\"\",\"8\"],[\"\\\"105\\\"\",\"5\"],[\"\\\"00\\\"\",\"0\"],[\"\\\"3456237490\\\"\",\"9191\"],[\"\\\"123456789\\\"\",\"45\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + int arg1 = parseInteger(args[1]); + vector result=s->addOperators(arg0,arg1); + string resultabc =serializeStringArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/282.\347\273\231\350\241\250\350\276\276\345\274\217\346\267\273\345\212\240\350\277\220\347\256\227\347\254\246/solution.h" "b/questions/282.\347\273\231\350\241\250\350\276\276\345\274\217\346\267\273\345\212\240\350\277\220\347\256\227\347\254\246/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..d8dbb1e815948ba83c0124a08dcd9956fc9ef9b7 --- /dev/null +++ "b/questions/282.\347\273\231\350\241\250\350\276\276\345\274\217\346\267\273\345\212\240\350\277\220\347\256\227\347\254\246/solution.h" @@ -0,0 +1,42 @@ +// @algorithm @lc id=282 lang=cpp +// @title expression-add-operators + + +// @test("123",6)=["1*2*3","1+2+3"] +// @test("232",8)=["2*3+2","2+3*2"] +// @test("105",5)=["1*0+5","10-5"] +// @test("00",0)=["0*0","0+0","0-0"] +// @test("3456237490",9191)=[] +// @test("123456789", 45)=["1*2*3*4*5-6-78+9","1*2*3*4+5+6-7+8+9","1*2*3+4+5+6+7+8+9","1*2*3+4+5-6*7+8*9","1*2*3+4-5*6+7*8+9","1*2*3+4-5*6-7+8*9","1*2*3-4*5+6*7+8+9","1*2*3-4*5-6+7*8+9","1*2*3-4*5-6-7+8*9","1*2*3-45+67+8+9","1*2*34+56-7-8*9","1*2*34-5+6-7-8-9","1*2+3*4-56+78+9","1*2+3+4+5*6+7+8-9","1*2+3+4-5+6*7+8-9","1*2+3+4-5-6+7*8-9","1*2+3+45+67-8*9","1*2+3-45+6+7+8*9","1*2+34+5-6-7+8+9","1*2+34+56-7*8+9","1*2+34-5+6+7-8+9","1*2+34-56+7*8+9","1*2+34-56-7+8*9","1*2-3*4+5+67-8-9","1*2-3+4-5-6*7+89","1*2-3-4*5+67+8-9","1*2-3-4+56-7-8+9","1*2-34+5*6+7*8-9","1*23+4*5-6+7-8+9","1*23-4-56-7+89","1+2*3*4*5+6+7-89","1+2*3*4+5*6+7-8-9","1+2*3*4-5+6*7-8-9","1+2*3+4*5*6+7-89","1+2*3+4*5-6+7+8+9","1+2*3-4-5-6*7+89","1+2*34-5*6+7+8-9","1+2+3*4*5+6-7-8-9","1+2+3*4+5+6*7-8-9","1+2+3*45-6-78-9","1+2+3+4+5+6+7+8+9","1+2+3+4+5-6*7+8*9","1+2+3+4-5*6+7*8+9","1+2+3+4-5*6-7+8*9","1+2+3-4*5+6*7+8+9","1+2+3-4*5-6+7*8+9","1+2+3-4*5-6-7+8*9","1+2+3-45+67+8+9","1+2-3*4*5+6+7+89","1+2-3*4+5*6+7+8+9","1+2-3*4-5+6*7+8+9","1+2-3*4-5-6+7*8+9","1+2-3*4-5-6-7+8*9","1+2-3+4*5+6*7-8-9","1+2-3+45+6-7-8+9","1+2-3+45-6+7+8-9","1+2-3-4-5*6+7+8*9","1+2-3-45-6+7+89","1+2-34+5+6+7*8+9","1+2-34+5+6-7+8*9","1+2-34-5-6+78+9","1+23*4+5-6-7*8+9","1+23*4-5-6*7+8-9","1+23*4-56+7-8+9","1+23+4+5+6+7+8-9","1+23+4-5*6+7*8-9","1+23+4-5-67+89","1+23-4*5+6*7+8-9","1+23-4*5-6+7*8-9","1+23-4-5+6+7+8+9","1+23-4-5-6*7+8*9","1+23-45+67+8-9","1-2*3*4+5-6+78-9","1-2*3*4-5-6+7+8*9","1-2*3+4*5+6+7+8+9","1-2*3+4*5-6*7+8*9","1-2*3+4+5+6*7+8-9","1-2*3+4+5-6+7*8-9","1-2*3+4+56+7-8-9","1-2*3+45-67+8*9","1-2*3-4+5*6+7+8+9","1-2*3-4-5+6*7+8+9","1-2*3-4-5-6+7*8+9","1-2*3-4-5-6-7+8*9","1-2*34+5*6-7+89","1-2+3*4*5-6-7+8-9","1-2+3+4-5*6+78-9","1-2+3+45+6-7+8-9","1-2+3-4*5-6+78-9","1-2+3-45+6-7+89","1-2-3*4+5+6+7*8-9","1-2-3*4-5-6+78-9","1-2-3+4-5+67-8-9","1-2-3+45-6-7+8+9","1-2-34+5+6+78-9","1-2-34+56+7+8+9","1-2-34-5+6+7+8*9","1-23*4+5+6*7+89","1-23+4*5-6*7+89","1-23+4-5+67-8+9","1-23+45-67+89","1-23-4+5+67+8-9","1-23-4-5-6-7+89","12*3*4-5*6-78+9","12*3+4+5+6-7-8+9","12*3+4+5-6+7+8-9","12*3-4-5-6+7+8+9","12*3-4-56+78-9","12+3*4+5+6-7+8+9","12+3*45-6-7-89","12+3+4-56-7+89","12+3-4*5+67-8-9","12+3-45+6+78-9","12+34-5-6-7+8+9","12-3*4*5+6+78+9","12-3*4-5+67-8-9","12-3+4*5+6-7+8+9","12-3+4+56-7-8-9","12-3-4+5*6-7+8+9","12-3-4-56+7+89","12-3-45-6+78+9"] +using it_t = string::iterator; +class Solution { +public: + vector addOperators(string num, int target) { + vector ans; + function BT = + [&](long added, it_t next, string expr, long value){ + if(next == num.end()){ + if(value == target) + ans.push_back(expr); + return; + } + int sign = expr.size(); // 不可用迭代器,因为后续的push_back操作会使迭代器失效 + if(next != num.begin()) expr.push_back(0); + long v = 0; + auto tail = *next == '0' ? next + 1 : num.end(); + for(auto it = next; it != tail; it++){ + v = v*10 + *it-'0'; + expr.push_back(*it); + if(next == num.begin()){ + BT(v, it+1, expr, v); + continue; + } + expr[sign] = '*'; BT(added*v, it+1, expr, value-added+added*v); + expr[sign] = '+'; BT( v, it+1, expr, value+v); + expr[sign] = '-'; BT(-v, it+1, expr, value-v); + } + }; + BT(0, num.begin(), "", 0); + return ans; + } +}; \ No newline at end of file diff --git "a/questions/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/main.cpp" "b/questions/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1202906d6b380f21514eb6ad92df30c991a77336 --- /dev/null +++ "b/questions/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,3,4,2,2]\"],[\"[3,1,3,4,2]\"],[\"[1,1]\"],[\"[1,1,2]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->findDuplicate(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/solution.h" "b/questions/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..9a4abc3e74fef096d27697363f996d66997de201 --- /dev/null +++ "b/questions/287.\345\257\273\346\211\276\351\207\215\345\244\215\346\225\260/solution.h" @@ -0,0 +1,24 @@ +// @algorithm @lc id=287 lang=cpp +// @title find-the-duplicate-number + + +// @test([1,3,4,2,2])=2 +// @test([3,1,3,4,2])=3 +// @test([1,1])=1 +// @test([1,1,2])=1 +class Solution { +public: + int findDuplicate(vector& nums) { + int slow = 0, fast = 0; + do { + slow = nums[slow]; + fast = nums[ nums[fast] ]; + } while(slow != fast); + slow = 0; + while(slow != fast){ + slow = nums[slow]; + fast = nums[fast]; + } + return slow; + } +}; \ No newline at end of file diff --git "a/questions/300.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227/solution.h" "b/questions/300.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227/solution.h" index 0662915825ba48ad16b61062f7f35a7340e23621..3bf119c39e8626356f44d542c765eb45814fc329 100644 --- "a/questions/300.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227/solution.h" +++ "b/questions/300.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227/solution.h" @@ -6,8 +6,22 @@ // @test([10,9,2,5,3,7,101,18])=4 // @test([0,1,0,3,2,3])=4 // @test([7,7,7,7,7,7,7])=1 +#define USE_DP class Solution { public: +#ifdef USE_DP // 动态规则 + int lengthOfLIS(vector& nums) { + if(nums.empty()) return 0; + vector dp(nums.size(), 1); + dp[0] = 1; + for(int i=1; i& nums) { vector asc; for(auto & a : nums){ @@ -19,4 +33,5 @@ public: } return asc.size(); } +#endif }; \ No newline at end of file diff --git "a/questions/301.\345\210\240\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/main.cpp" "b/questions/301.\345\210\240\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e7bbccabb5cc1c3f17f8bca968dd5ae7c55fe805 --- /dev/null +++ "b/questions/301.\345\210\240\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"()())()\\\"\"],[\"\\\"(a)())()\\\"\"],[\"\\\")(\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + vector result=s->removeInvalidParentheses(arg0); + string resultabc =serializeStringArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/301.\345\210\240\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/solution.h" "b/questions/301.\345\210\240\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..077455c48fb1cecbc3832300ac9a980df126e77a --- /dev/null +++ "b/questions/301.\345\210\240\351\231\244\346\227\240\346\225\210\347\232\204\346\213\254\345\217\267/solution.h" @@ -0,0 +1,60 @@ +// @algorithm @lc id=301 lang=cpp +// @title remove-invalid-parentheses + + +#define print(...) +// @test("()())()")=["()()()","(())()"] +// @test("(a)())()")=["(a)()()","(a())()"] +// @test(")(")=[""] +using it_t = string::iterator; +class Solution { +public: + vector removeInvalidParentheses(string s) { + print("\nins=", s, "\n") + int dL = 0, dR = 0; + for(auto & c : s){ + switch(c){ + case '(': + ++ dL; + break; + case ')': + 0 < dL ? dL-- : dR++; + break; + } + } + unordered_set res; + string ans(s.size(), '\0'); + function dfs = + [&](it_t it, int n, int nL, int nR, int dL, int dR){ + if(it == s.end()){ + if(0==dL && 0==dR) + res.insert(ans.substr(0, n)); + print("may:", ans.substr(0, n), "|dL=", dL, ", dR=", dR, "\n") + return; + } + switch(*it){ + case '(': + if(dL>0) + dfs(it+1, n, nL, nR, dL-1, dR); + ans[n] = *it; + dfs(it+1, n+1, nL+1, nR, dL, dR); + break; + case ')': + if(dR>0) + dfs(it+1, n, nL, nR, dL, dR-1); + if(nL > nR){ + ans[n] = *it; + dfs(it+1, n+1, nL, nR+1, dL, dR); + } + break; + default: + ans[n] = *it; + dfs(it+1, n+1, nL, nR, dL, dR); + break; + } + }; + dfs(s.begin(),0, 0, 0, dL, dR); + print("ans:", res, "\n") + return {res.begin(), res.end()}; + } +}; \ No newline at end of file diff --git "a/questions/309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/main.cpp" "b/questions/309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1ad0e6fa82b98e35b0176837d5ed37e9960f1ea3 --- /dev/null +++ "b/questions/309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,3,0,2]\"],[\"[1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->maxProfit(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/solution.h" "b/questions/309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c530d5f57ddd4dfcfacc4f906e961ba0cb6f7015 --- /dev/null +++ "b/questions/309.\346\234\200\344\275\263\344\271\260\345\215\226\350\202\241\347\245\250\346\227\266\346\234\272\345\220\253\345\206\267\345\206\273\346\234\237/solution.h" @@ -0,0 +1,19 @@ +// @algorithm @lc id=309 lang=cpp +// @title best-time-to-buy-and-sell-stock-with-cooldown + + +// @test([1,2,3,0,2])=3 +// @test([1])=0 +class Solution { +public: + int maxProfit(vector& prices) { + vector f = {-prices[0], 0, 0};// 持有,冷冻期,非冷冻期 + for(int i=1; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->maxCoins(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/312.\346\210\263\346\260\224\347\220\203/solution.h" "b/questions/312.\346\210\263\346\260\224\347\220\203/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c343c1aaf28b521bae71a6f05d31e4271867facf --- /dev/null +++ "b/questions/312.\346\210\263\346\260\224\347\220\203/solution.h" @@ -0,0 +1,25 @@ +// @algorithm @lc id=312 lang=cpp +// @title burst-balloons + +#define print(...) +// @test([3,1,5,8])=167 +// @test([1,5])=10 +class Solution { +public: + int maxCoins(vector& nums) { + int n = nums.size(); + vector val{1, 1}; + val.insert(val.begin()+1, nums.begin(), nums.end()); + print("\ndata:", val, "\n") + vector< vector > dp(n+2, vector(n+2, 0)); + for(int L=n-1; L>=0; L--){ + for(int R=L+2; R> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->coinChange(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/322.\351\233\266\351\222\261\345\205\221\346\215\242/solution.h" "b/questions/322.\351\233\266\351\222\261\345\205\221\346\215\242/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..7c47875468a6b027416fc543212ff6ac462a77dc --- /dev/null +++ "b/questions/322.\351\233\266\351\222\261\345\205\221\346\215\242/solution.h" @@ -0,0 +1,22 @@ +// @algorithm @lc id=322 lang=cpp +// @title coin-change + + +// @test([1,2,5],11)=3 +// @test([2],3)=-1 +// @test([1],0)=0 +// @test([1],1)=1 +// @test([1],2)=2 +class Solution { +public: + int coinChange(vector& coins, int amount) { + vector dp(amount+1, INT_MAX/2); + dp[0] = 0; + for(int s=1; s<=amount; s++){ + for(auto & c : coins){ + if(c <= s) dp[s] = min(dp[s], dp[s-c] + 1); + } + } + return dp[amount]==INT_MAX/2 ? -1 : dp[amount]; + } +}; \ No newline at end of file diff --git "a/questions/337.\346\211\223\345\256\266\345\212\253\350\210\215 III/main.cpp" "b/questions/337.\346\211\223\345\256\266\345\212\253\350\210\215 III/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..82f3bf14f1e0678bab386287c3c9018e3a8a1161 --- /dev/null +++ "b/questions/337.\346\211\223\345\256\266\345\212\253\350\210\215 III/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[3,2,3,null,3,null,1]\"],[\"[3,4,5,1,3,null,1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + TreeNode * arg0 = parseTreeNode(args[0]); + int result=s->rob(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/337.\346\211\223\345\256\266\345\212\253\350\210\215 III/solution.h" "b/questions/337.\346\211\223\345\256\266\345\212\253\350\210\215 III/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..9f662d621aaa971077c90af6d39a171767a09edc --- /dev/null +++ "b/questions/337.\346\211\223\345\256\266\345\212\253\350\210\215 III/solution.h" @@ -0,0 +1,32 @@ +// @algorithm @lc id=337 lang=cpp +// @title house-robber-iii + + +// @test([3,2,3,null,3,null,1])=7 +// @test([3,4,5,1,3,null,1])=9 +class Solution { +public: + pair/*sel, nosel*/ LRD(TreeNode * node){ + if(!node) return {0, 0}; + auto L = LRD(node->left); + auto R = LRD(node->right); + auto sel = node->val + L.second + R.second; + auto nosel = max(L.first, L.second) + max(R.first, R.second); + return {sel, nosel}; + } + int rob(TreeNode* root) { + auto r = LRD(root); + return max(r.first, r.second); + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file diff --git "a/questions/347.\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/main.cpp" "b/questions/347.\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..f6bbbc5f61b1fb1275f0f7ad860b089d7f6ae82b --- /dev/null +++ "b/questions/347.\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,1,1,2,2,3]\",\"2\"],[\"[1]\",\"1\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int arg1 = parseInteger(args[1]); + vector result=s->topKFrequent(arg0,arg1); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/347.\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.h" "b/questions/347.\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..304151ad4ceb372daf49eca0a110d76807ba4aff --- /dev/null +++ "b/questions/347.\345\211\215 K \344\270\252\351\253\230\351\242\221\345\205\203\347\264\240/solution.h" @@ -0,0 +1,35 @@ +// @algorithm @lc id=347 lang=cpp +// @title top-k-frequent-elements + + +// @test([1,1,1,2,2,3],2)=[1,2] +// @test([1],1)=[1] +using T = pair; +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + unordered_map freq; + for(auto & n : nums) + freq[n]++; + + auto cmp = [](const T & a, const T & b){ + return a.second > b.second; + }; + priority_queue, decltype(cmp)> heap(cmp); + for(auto & p : freq){ + if(heap.size()==k){ + if(heap.top().second >= p.second) + continue; + heap.pop(); + } + heap.push(p); + } + + vector ans; + while(heap.size()){ + ans.push_back(heap.top().first); + heap.pop(); + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/352.\345\260\206\346\225\260\346\215\256\346\265\201\345\217\230\344\270\272\345\244\232\344\270\252\344\270\215\347\233\270\344\272\244\345\214\272\351\227\264/main.cpp" "b/questions/352.\345\260\206\346\225\260\346\215\256\346\265\201\345\217\230\344\270\272\345\244\232\344\270\252\344\270\215\347\233\270\344\272\244\345\214\272\351\227\264/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..fda19387f876ff5e33a80d62904cb9bdc22930c6 --- /dev/null +++ "b/questions/352.\345\260\206\346\225\260\346\215\256\346\265\201\345\217\230\344\270\272\345\244\232\344\270\252\344\270\215\347\233\270\344\272\244\345\214\272\351\227\264/main.cpp" @@ -0,0 +1,17 @@ +#include "stl.h" +#include "solution.h" + +int main(int argc, char *argv[]){ + vector testcases[] = { + {1, 3, 7, 2, 6}, + }; + for(auto & ts : testcases){ + SummaryRanges obj; + for(auto val : ts){ + obj.addNum(val); + print(val, ":", obj.getIntervals(), "\n") + } + } + print("\n------ main exit ------\n") + return 0; +} diff --git "a/questions/352.\345\260\206\346\225\260\346\215\256\346\265\201\345\217\230\344\270\272\345\244\232\344\270\252\344\270\215\347\233\270\344\272\244\345\214\272\351\227\264/solution.h" "b/questions/352.\345\260\206\346\225\260\346\215\256\346\265\201\345\217\230\344\270\272\345\244\232\344\270\252\344\270\215\347\233\270\344\272\244\345\214\272\351\227\264/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..aa65f9ae9ae31328f917e78d95d17942e0133b52 --- /dev/null +++ "b/questions/352.\345\260\206\346\225\260\346\215\256\346\265\201\345\217\230\344\270\272\345\244\232\344\270\252\344\270\215\347\233\270\344\272\244\345\214\272\351\227\264/solution.h" @@ -0,0 +1,38 @@ +// @algorithm @lc id=352 lang=cpp +// @title data-stream-as-disjoint-intervals +class SummaryRanges { + map _sec; +public: + SummaryRanges() {} + + void addNum(int val) { + auto R = _sec.upper_bound(val); + auto L = _sec.begin() == R ? _sec.end() : prev(R); + if(L!=_sec.end() && L->first <= val && val <= L->second) + return; + bool aL = L!=_sec.end() && val==L->second + 1; + bool bR = R!=_sec.end() && val==R->first - 1; + if(aL && bR){ + L->second = R->second; + _sec.erase(R); + return; + } + if(aL){ + L->second++; + return; + } + if(bR){ + _sec[val] = R->second; + _sec.erase(R); + return; + } + _sec[val] = val; + } + + vector> getIntervals() { + vector> sec; + for(auto & s : _sec) + sec.push_back({s.first, s.second}); + return sec; + } +}; \ No newline at end of file diff --git "a/questions/371.\344\270\244\346\225\264\346\225\260\344\271\213\345\222\214/main.cpp" "b/questions/371.\344\270\244\346\225\264\346\225\260\344\271\213\345\222\214/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d9365b39a7a41a6a05e70b8f48259f38d2185de7 --- /dev/null +++ "b/questions/371.\344\270\244\346\225\264\346\225\260\344\271\213\345\222\214/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"1\",\"2\"],[\"2\",\"3\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->getSum(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/371.\344\270\244\346\225\264\346\225\260\344\271\213\345\222\214/solution.h" "b/questions/371.\344\270\244\346\225\264\346\225\260\344\271\213\345\222\214/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..d19fc3a1b70b362c45b982364c4a924d5af658e9 --- /dev/null +++ "b/questions/371.\344\270\244\346\225\264\346\225\260\344\271\213\345\222\214/solution.h" @@ -0,0 +1,17 @@ +// @algorithm @lc id=371 lang=cpp +// @title sum-of-two-integers + + +// @test(1,2)=3 +// @test(2,3)=5 +class Solution { +public: + int getSum(int a, int b) { + while(b){ + auto c = uint32_t(a&b)<<1; + a ^= b; + b = c; + } + return a; + } +}; \ No newline at end of file diff --git "a/questions/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.cpp" "b/questions/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b52ba528d0ee5287796b71695c621a11e29a09ce --- /dev/null +++ "b/questions/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"3[a]2[bc]\\\"\"],[\"\\\"3[a2[c]]\\\"\"],[\"\\\"2[abc]3[cd]ef\\\"\"],[\"\\\"abc3[cd]xyz\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + string result=s->decodeString(arg0); + string resultabc =serializeString(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/solution.h" "b/questions/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..dea2aa07c106cbd6fffbb9d4483743f9ecc0b560 --- /dev/null +++ "b/questions/394.\345\255\227\347\254\246\344\270\262\350\247\243\347\240\201/solution.h" @@ -0,0 +1,41 @@ +// @algorithm @lc id=394 lang=cpp +// @title decode-string + + +// @test("3[a]2[bc]")="aaabcbc" +// @test("3[a2[c]]")="accaccacc" +// @test("2[abc]3[cd]ef")="abcabccdcdcdef" +// @test("abc3[cd]xyz")="abccdcdcdxyz" +#define IS_DIGIT(ch) ('0'<=(ch) && (ch)<='9') +#define IS_LETTER(ch) ('a'<=(ch) && (ch)<='z') +class Solution { +public: + string decodeString(string s) { + auto pc = s.begin(); + + function parseNum = [&](){ + int n = 0; + while(pc!=s.end() && IS_DIGIT(*pc)){ + n = n*10 + (*pc - '0'); + pc++; + } + return n; + }; + + function decode = [&]()->string{ + if(pc==s.end() || *pc==']') return ""; + string res; + if(IS_DIGIT(*pc)){ + int n = parseNum(); + pc++; + string w = decode(); + pc++; + while(n--) res += w; + } + while(pc!=s.end() && IS_LETTER(*pc)) + res += *pc++; + return res + decode(); + }; + return decode(); + } +}; \ No newline at end of file diff --git "a/questions/399.\351\231\244\346\263\225\346\261\202\345\200\274/main.cpp" "b/questions/399.\351\231\244\346\263\225\346\261\202\345\200\274/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..1a3332ef90f93092fc286be670f3a99a1744febb --- /dev/null +++ "b/questions/399.\351\231\244\346\263\225\346\261\202\345\200\274/main.cpp" @@ -0,0 +1,43 @@ +#include "stl.h" +#include "solution.h" + +int main(int argc, char *argv[]){ + struct { + vector> equations; + vector values; + vector> queries; + } tests[] = { + { + {{"a","b"},{"b","c"}}, + {2.0,3.0}, + {{"a","c"},{"b","a"},{"a","e"},{"a","a"},{"x","x"}}, + }, // [6.00000,0.50000,-1.00000,1.00000,-1.00000] + { + {{"a","b"},{"b","c"},{"bc","cd"}}, + {1.5,2.5,5.0}, + {{"a","c"},{"c","b"},{"bc","cd"},{"cd","bc"}}, + }, // [3.75000,0.40000,5.00000,0.20000] + { + {{"a","b"}}, + {0.5}, + {{"a","b"},{"b","a"},{"a","c"},{"x","y"}}, + }, // [0.50000,2.00000,-1.00000,-1.00000] + { + {{"a","b"},{"e","f"},{"b","e"}}, + {3.4,1.4,2.3}, + {{"b","a"},{"a","f"},{"f","f"},{"e","e"},{"c","c"},{"a","c"},{"f","e"}}, + }, // [0.29412,10.948,1.0,1.0,-1.0,-1.0,0.71429] + }; + int i = 0; + for(auto & ts : tests){ + print("\n[", ++i,"] equations=", ts.equations, "\n") + print("values=", ts.values, "\n") + print("queries=", ts.queries, "\n") + Solution sln; + auto res = sln.calcEquation(ts.equations, ts.values, ts.queries); + print("res=", res, "\n") + } + + print("\n------ main exit ------\n") + return 0; +} diff --git "a/questions/399.\351\231\244\346\263\225\346\261\202\345\200\274/solution.h" "b/questions/399.\351\231\244\346\263\225\346\261\202\345\200\274/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..9b436aa693fc92c0220f1730cb5fd9a196f978c7 --- /dev/null +++ "b/questions/399.\351\231\244\346\263\225\346\261\202\345\200\274/solution.h" @@ -0,0 +1,74 @@ +// @algorithm @lc id=399 lang=cpp +// @title evaluate-division + + +#define print(...) +// @test([["a","b"],["b","c"]],[2.0,3.0],[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]])=[6.00000,0.50000,-1.00000,1.00000,-1.00000] +// @test([["a","b"],["b","c"],["bc","cd"]],[1.5,2.5,5.0],[["a","c"],["c","b"],["bc","cd"],["cd","bc"]])=[3.75000,0.40000,5.00000,0.20000] +// @test([["a","b"]],[0.5],[["a","b"],["b","a"],["a","c"],["x","y"]])=[0.50000,2.00000,-1.00000,-1.00000] +// @test([["a","b"],["e","f"],["b","e"]], [3.4,1.4,2.3], [["b","a"],["a","f"],["f","f"],["e","e"],["c","c"],["a","c"],["f","e"]])=[0.29412,10.948,1.0,1.0,-1.0,-1.0,0.71429] +// 并查集 +class UnionFind{ + vector direct; // ["a" -> "b"] 有向边, direct[ index("a") ] = index("b") + vector weight; // "a"->"b" 有向边权重 = a/b +public: + UnionFind(size_t n) : direct(n,0), weight(n, 1.0){ + for(int i=0; i index; +public: + vector calcEquation(vector>& equations, + vector& values, + vector>& queries) + { + int n = equations.size(); + UnionFind uf(n*2); + for(int i=0,h=0; i ans(m, -1.0); + for(int i=0; isecond, ib->second); + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/43.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/main.cpp" "b/questions/43.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..14e016d54432c4a2e06e42f06ddca86920b902e5 --- /dev/null +++ "b/questions/43.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"2\\\"\",\"\\\"3\\\"\"],[\"\\\"123\\\"\",\"\\\"456\\\"\"],[\"\\\"0\\\"\",\"\\\"0\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + string arg1 = parseString(args[1]); + string result=s->multiply(arg0,arg1); + string resultabc =serializeString(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/43.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.h" "b/questions/43.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..113c91ccc333341943cb01989f7fddb1827ede3f --- /dev/null +++ "b/questions/43.\345\255\227\347\254\246\344\270\262\347\233\270\344\271\230/solution.h" @@ -0,0 +1,34 @@ +// @algorithm @lc id=43 lang=cpp +// @title multiply-strings + + +// #define print(...) +// @test("2","3")="6" +// @test("123","456")="56088" +// @test("0","0")="0" +class Solution { +public: + string multiply(string num1, string num2) { + int m = num1.size(), n = num2.size(); + if(1==m && num1[0]=='0') return "0"; + if(1==n && num2[0]=='0') return "0"; + vector res(m+n, 0); + auto it = res.rbegin(); + for(auto ni=num2.rbegin(); ni!=num2.rend(); ni++,it++){ + auto ri = it; + for(auto mi=num1.rbegin(); mi!=num1.rend(); mi++,ri++){ + int product = (*mi-'0') * (*ni-'0'); + *ri += product; + if(*ri>9){ + *(ri+1) += *ri/10; + *ri = *ri%10; + } + } + } + string ans; + auto p = res.begin(); + while(!*p && p!=res.end()) p++; + while(p!=res.end()) ans.push_back('0'+*p++); + return ans; + } +}; \ No newline at end of file diff --git "a/questions/437.\350\267\257\345\276\204\346\200\273\345\222\214 III/main.cpp" "b/questions/437.\350\267\257\345\276\204\346\200\273\345\222\214 III/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b34af8b469ec74aa192584146bcd46b288b4d99e --- /dev/null +++ "b/questions/437.\350\267\257\345\276\204\346\200\273\345\222\214 III/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[10,5,-3,3,2,null,11,3,-2,null,1]\",\"8\"],[\"[5,4,8,11,null,13,4,7,2,null,null,5,1]\",\"22\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + TreeNode * arg0 = parseTreeNode(args[0]); + int arg1 = parseInteger(args[1]); + int result=s->pathSum(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/437.\350\267\257\345\276\204\346\200\273\345\222\214 III/solution.h" "b/questions/437.\350\267\257\345\276\204\346\200\273\345\222\214 III/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..4cc10bbbeafbf49b4bfd380a8902561d4154a8cd --- /dev/null +++ "b/questions/437.\350\267\257\345\276\204\346\200\273\345\222\214 III/solution.h" @@ -0,0 +1,38 @@ +// @algorithm @lc id=437 lang=cpp +// @title path-sum-iii + + +// @test([10,5,-3,3,2,null,11,3,-2,null,1],8)=3 +// @test([5,4,8,11,null,13,4,7,2,null,null,5,1],22)=3 +class Solution { +public: + int pathSum(TreeNode* root, int targetSum) { + int ans = 0; + unordered_map prePaths; // preSum => pathCount + prePaths[0] = 1; + function dfs = [&](TreeNode *cur, int sum){ + if(!cur) return; + sum += cur->val; + auto it = prePaths.find(sum-targetSum); + if(it != prePaths.end()) + ans += it->second; + ++ prePaths[sum]; + dfs(cur->left, sum); + dfs(cur->right, sum); + -- prePaths[sum]; + }; + dfs(root, 0); + return ans; + } +}; +/** + * 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) {} + * }; + */ \ No newline at end of file diff --git "a/questions/44.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/main.cpp" "b/questions/44.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7dc798756db9c5a8f3794b9cd59f5ad4ab0588ee --- /dev/null +++ "b/questions/44.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"\\\"\",\"\\\"\\\"\"],[\"\\\"\\\"\",\"\\\"*\\\"\"],[\"\\\"\\\"\",\"\\\"*a\\\"\"],[\"\\\"aa\\\"\",\"\\\"a\\\"\"],[\"\\\"aa\\\"\",\"\\\"*\\\"\"],[\"\\\"cb\\\"\",\"\\\"?a\\\"\"],[\"\\\"adceb\\\"\",\"\\\"*a*b\\\"\"],[\"\\\"acdcb\\\"\",\"\\\"a*c?b\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + string arg1 = parseString(args[1]); + bool result=s->isMatch(arg0,arg1); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/44.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.h" "b/questions/44.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..4388f5a543b4fb0eacf6b70c5a8ff2bd94e834bd --- /dev/null +++ "b/questions/44.\351\200\232\351\205\215\347\254\246\345\214\271\351\205\215/solution.h" @@ -0,0 +1,34 @@ +// @algorithm @lc id=44 lang=cpp +// @title wildcard-matching + + +// #define print(...) +// @test("","")=true +// @test("","*")=true +// @test("","*a")=false +// @test("aa","a")=false +// @test("aa","*")=true +// @test("cb","?a")=false +// @test("adceb","*a*b")=true +// @test("acdcb","a*c?b")=false +class Solution { +public: + bool isMatch(string s, string p) { + int sn = s.size(), pn = p.size(); + vector> sp(sn+1, vector(pn+1)); + sp[0][0] = true; + for(int j=0; j> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector result=s->findDuplicates(arg0); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/442.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\346\215\256/solution.h" "b/questions/442.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\346\215\256/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..b98f86cb8d043da3939ce90367c6b0ee01f2b8bb --- /dev/null +++ "b/questions/442.\346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\346\215\256/solution.h" @@ -0,0 +1,21 @@ +// @algorithm @lc id=442 lang=cpp +// @title find-all-duplicates-in-an-array + + +// @test([4,3,2,7,8,2,3,1])=[2,3] +// @test([1,1,2])=[1] +// @test([1])=[] +// @test([5,4,6,7,9,3,10,9,5,6])=[9,5,6] +class Solution { +public: + vector findDuplicates(vector& nums) { + vector ans; + for(auto s : nums){ + if(s<0) s = -s; + if(nums[s-1]<0) + ans.push_back(s); + nums[s-1] = -nums[s-1]; + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/45.\350\267\263\350\267\203\346\270\270\346\210\217 II/main.cpp" "b/questions/45.\350\267\263\350\267\203\346\270\270\346\210\217 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..d79bba05dbab89c171e6a51ed7bb6dc963773150 --- /dev/null +++ "b/questions/45.\350\267\263\350\267\203\346\270\270\346\210\217 II/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[2,3,1,1,4]\"],[\"[2,3,0,1,4]\"],[\"[2,1,1,1,1]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->jump(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/45.\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.h" "b/questions/45.\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..2f74883245852d743e0b0a4e30ba2ae53107358e --- /dev/null +++ "b/questions/45.\350\267\263\350\267\203\346\270\270\346\210\217 II/solution.h" @@ -0,0 +1,22 @@ +// @algorithm @lc id=45 lang=cpp +// @title jump-game-ii + + +// #define print(...) +// @test([2,3,1,1,4])=2 +// @test([2,3,0,1,4])=2 +// @test([2,1,1,1,1])=3 +class Solution { +public: + int jump(vector& nums) { + int jump = 0/*下一跳的索引*/, nextJump = 0, step = 0/*跳跃次数*/; + for(int i=0; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector> result=s->permute(arg0); + string resultabc =serializeIntegerArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/46.\345\205\250\346\216\222\345\210\227/solution.h" "b/questions/46.\345\205\250\346\216\222\345\210\227/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..d35f5c38920762fc0c0abb4238b3160c54d8a006 --- /dev/null +++ "b/questions/46.\345\205\250\346\216\222\345\210\227/solution.h" @@ -0,0 +1,26 @@ +// @algorithm @lc id=46 lang=cpp +// @title permutations + + +// @test([1,2,3])=[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] +// @test([0,1])=[[0,1],[1,0]] +// @test([1])=[[1]] +class Solution { +public: + vector> permute(vector& nums) { + vector> ans; + function dfs = [&](int cur){ + if(cur==nums.size()){ + ans.push_back(nums); + return; + } + for(int i=cur; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector> result=s->permuteUnique(arg0); + string resultabc =serializeIntegerArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/47.\345\205\250\346\216\222\345\210\227 II/solution.h" "b/questions/47.\345\205\250\346\216\222\345\210\227 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..5d14b8a0e22c1324a7a0ac61b56073a81117c3eb --- /dev/null +++ "b/questions/47.\345\205\250\346\216\222\345\210\227 II/solution.h" @@ -0,0 +1,32 @@ +// @algorithm @lc id=47 lang=cpp +// @title permutations-ii + + +// @test([1])=[[1]] +// @test([1,1,2])=[[1,1,2],[1,2,1],[2,1,1]] +// @test([1,2,3])=[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] +// @test([2,2,1,1])=[[1,1,2,2],[1,2,1,2],[1,2,2,1],[2,1,1,2],[2,1,2,1],[2,2,1,1]] +// @test([0,1,0,0,9])=[[0,0,0,1,9],[0,0,0,9,1],[0,0,1,0,9],[0,0,1,9,0],[0,0,9,0,1],[0,0,9,1,0],[0,1,0,0,9],[0,1,0,9,0],[0,1,9,0,0],[0,9,0,0,1],[0,9,0,1,0],[0,9,1,0,0],[1,0,0,0,9],[1,0,0,9,0],[1,0,9,0,0],[1,9,0,0,0],[9,0,0,0,1],[9,0,0,1,0],[9,0,1,0,0],[9,1,0,0,0]] +class Solution { +public: + vector> permuteUnique(vector& nums) { + vector> ans; + function dfs = [&](int cur){ + if(cur==nums.size()){ + ans.push_back(nums); + return; + } + for(int i=cur; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + s->rotate(arg0); + string resultabc =serializeIntegerArrArr(arg0); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/48.\346\227\213\350\275\254\345\233\276\345\203\217/solution.h" "b/questions/48.\346\227\213\350\275\254\345\233\276\345\203\217/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..1a44f7f5f8a768bc216abe2c8a5f1e08f128d42d --- /dev/null +++ "b/questions/48.\346\227\213\350\275\254\345\233\276\345\203\217/solution.h" @@ -0,0 +1,25 @@ +// @algorithm @lc id=48 lang=cpp +// @title rotate-image + + +#define print(...) +// @test([[1,2,3],[4,5,6],[7,8,9]])=[[7,4,1],[8,5,2],[9,6,3]] +// @test([[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]])=[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] +// @test([[1]])=[[1]] +// @test([[1,2],[3,4]])=[[3,1],[4,2]] +class Solution { +public: + void rotate(vector>& matrix) { + print("mat_ori:\n", Mat(matrix)); + int n = matrix.size(); + if(n==1) return; + for(int i=0; iright:\n", Mat(matrix), "\n"); + } +}; \ No newline at end of file diff --git a/questions/50.Pow(x, n)/main.cpp b/questions/50.Pow(x, n)/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0780d44d19309fc5d7a5820e7e53d8e6aef02404 --- /dev/null +++ b/questions/50.Pow(x, n)/main.cpp @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"2.00000\",\"10\"],[\"2.10000\",\"3\"],[\"2.00000\",\"-2\"],[\"34.00515\",\"-3\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + double arg0 = parseFloat(args[0]); + int arg1 = parseInteger(args[1]); + double result=s->myPow(arg0,arg1); + string resultabc =serializeFloat(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git a/questions/50.Pow(x, n)/solution.h b/questions/50.Pow(x, n)/solution.h new file mode 100644 index 0000000000000000000000000000000000000000..3466d723a4d7f97ffc7884316bb76cc44c00dd39 --- /dev/null +++ b/questions/50.Pow(x, n)/solution.h @@ -0,0 +1,24 @@ +// @algorithm @lc id=50 lang=cpp +// @title powx-n + + +// @test(2.00000,10)=1024.00000 +// @test(2.10000,3)=9.26100 +// @test(2.00000,-2)=0.25000 +// @test(34.00515,-3)=0.00003 +class Solution { +public: + double myPow(double x, int n) { + return n>=0 ? quickPower(x, n) : 1.0/quickPower(x, -(n+1))/x; + } + double quickPower(double x, int n){ + double res = 1.0; + while(n){ + if(n&1) + res *= x; + x *= x; + n >>= 1; + } + return res; + } +}; \ No newline at end of file diff --git a/questions/502.IPO/main.cpp b/questions/502.IPO/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ea9276c4bafd971040bd0d413dd7d35c5c6aaa0 --- /dev/null +++ b/questions/502.IPO/main.cpp @@ -0,0 +1,21 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"2\",\"0\",\"[1,2,3]\",\"[0,1,1]\"],[\"3\",\"0\",\"[1,2,3]\",\"[0,1,2]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int arg1 = parseInteger(args[1]); + vector arg2 = parseIntegerArr(args[2]); + vector arg3 = parseIntegerArr(args[3]); + int result=s->findMaximizedCapital(arg0,arg1,arg2,arg3); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git a/questions/502.IPO/solution.h b/questions/502.IPO/solution.h new file mode 100644 index 0000000000000000000000000000000000000000..96de75a1bf6d6f9ff5490f42113efa5453660a3d --- /dev/null +++ b/questions/502.IPO/solution.h @@ -0,0 +1,29 @@ +// @algorithm @lc id=502 lang=cpp +// @title ipo + + +// @test(2,0,[1,2,3],[0,1,1])=4 +// @test(3,0,[1,2,3],[0,1,2])=6 +class Solution { +public: + int findMaximizedCapital(int k, int w, vector& profits, vector& capital) { + int n = profits.size(); + vector> prj; + for(int i=0; i, less> canDo; // 大顶堆:可做项目的收益 + for(int i=0; ifirst<=w){ // 找出可做项目 + canDo.push(p->second); + p++; + } + if(canDo.empty()) break; + w += canDo.top(); + canDo.pop(); + } + return w; + } +}; \ No newline at end of file diff --git "a/questions/51.N \347\232\207\345\220\216/main.cpp" "b/questions/51.N \347\232\207\345\220\216/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..bc8a909b458c073620f3a059205b5593efbd7b7b --- /dev/null +++ "b/questions/51.N \347\232\207\345\220\216/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"4\"],[\"1\"],[\"5\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + vector> result=s->solveNQueens(arg0); + string resultabc =serializeStringArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/51.N \347\232\207\345\220\216/solution.h" "b/questions/51.N \347\232\207\345\220\216/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..a42524ac0f9e63bcfeb312272bb5d18f31e62be3 --- /dev/null +++ "b/questions/51.N \347\232\207\345\220\216/solution.h" @@ -0,0 +1,37 @@ +// @algorithm @lc id=51 lang=cpp +// @title n-queens + + +#define print(...) +// @test(4)=[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] +// @test(1)=[["Q"]] +// @test(5)=[["Q....","..Q..","....Q",".Q...","...Q."],["Q....","...Q.",".Q...","....Q","..Q.."],[".Q...","...Q.","Q....","..Q..","....Q"],[".Q...","....Q","..Q..","Q....","...Q."],["..Q..","Q....","...Q.",".Q...","....Q"],["..Q..","....Q",".Q...","...Q.","Q...."],["...Q.","Q....","..Q..","....Q",".Q..."],["...Q.",".Q...","....Q","..Q..","Q...."],["....Q",".Q...","...Q.","Q....","..Q.."],["....Q","..Q..","Q....","...Q.",".Q..."]] +class Solution { +public: + vector> solveNQueens(int n) { + vector> res; + vector ans; + string place(n, '.'); + function bt = // mCol,mL,mR 分别代表向下、左下、右下已占bit + [&](int row, int mCol, int mL, int mR){ + if(row == n){ + print(Mat(ans), "\n") + res.push_back(ans); + return; + } + for( int mask = ((1<>1); + ans.pop_back(); + } + }; + print("\nn=", n, "\n") + bt(0, 0, 0, 0); + return res; + } +}; \ No newline at end of file diff --git "a/questions/517.\350\266\205\347\272\247\346\264\227\350\241\243\346\234\272/main.cpp" "b/questions/517.\350\266\205\347\272\247\346\264\227\350\241\243\346\234\272/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8d995f663cbde5bc464a26883e660124fbfb9766 --- /dev/null +++ "b/questions/517.\350\266\205\347\272\247\346\264\227\350\241\243\346\234\272/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,0,5]\"],[\"[0,3,0]\"],[\"[0,2,0]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->findMinMoves(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/517.\350\266\205\347\272\247\346\264\227\350\241\243\346\234\272/solution.h" "b/questions/517.\350\266\205\347\272\247\346\264\227\350\241\243\346\234\272/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..90ea4e5abeaace3cd5d06c4e64990483d9a3122b --- /dev/null +++ "b/questions/517.\350\266\205\347\272\247\346\264\227\350\241\243\346\234\272/solution.h" @@ -0,0 +1,23 @@ +// @algorithm @lc id=517 lang=cpp +// @title super-washing-machines + + +// @test([1,0,5])=3 +// @test([0,3,0])=2 +// @test([0,2,0])=-1 +class Solution { +public: + int findMinMoves(vector& machines) { + int totalCloth = accumulate(machines.begin(), machines.end(), 0); + if(totalCloth % machines.size()) return -1; + int avgCloth = totalCloth / machines.size(); + int sumDiff = 0, ans = 0; + for(auto cloth : machines){ + cloth -= avgCloth; + sumDiff += cloth; + int step = max(abs(sumDiff), cloth); + if(step > ans) ans = step; + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/52.N\347\232\207\345\220\216 II/main.cpp" "b/questions/52.N\347\232\207\345\220\216 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..b43c306d4bf32e1e950c9742b1102defb88d9f40 --- /dev/null +++ "b/questions/52.N\347\232\207\345\220\216 II/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"4\"],[\"1\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int result=s->totalNQueens(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/52.N\347\232\207\345\220\216 II/solution.h" "b/questions/52.N\347\232\207\345\220\216 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..19492ddb3e8da15df45190bf97f4c7407846e247 --- /dev/null +++ "b/questions/52.N\347\232\207\345\220\216 II/solution.h" @@ -0,0 +1,26 @@ +// @algorithm @lc id=52 lang=cpp +// @title n-queens-ii + + +// @test(4)=2 +// @test(1)=1 +class Solution { +public: + int totalNQueens(int n) { + int ans = 0; + function dfs = + [&](int row, int col, int left, int right){ + if(row == n){ + ans ++; + return; + } + for(int mask = ((1<>1); + } + }; + dfs(0, 0, 0, 0); + return ans; + } +}; \ No newline at end of file diff --git "a/questions/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/main.cpp" "b/questions/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..23deea5d6f2012da43e660635feb8dbd72d7ca49 --- /dev/null +++ "b/questions/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[-2,1,-3,4,-1,2,1,-5,4]\"],[\"[1]\"],[\"[5,4,-1,7,8]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + int result=s->maxSubArray(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/solution.h" "b/questions/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..289dfefe20f81745c3f7261ca295798607974abd --- /dev/null +++ "b/questions/53.\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214/solution.h" @@ -0,0 +1,28 @@ +// @algorithm @lc id=53 lang=cpp +// @title maximum-subarray + + +// @test([-2,1,-3,4,-1,2,1,-5,4])=6 +// @test([1])=1 +// @test([5,4,-1,7,8])=23 +class Solution { +public: + int maxSubArray(vector& nums) { + #ifdef USE_PREFIX_SUM + for(int i=1; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + vector result=s->spiralOrder(arg0); + string resultabc =serializeIntegerArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/54.\350\236\272\346\227\213\347\237\251\351\230\265/solution.h" "b/questions/54.\350\236\272\346\227\213\347\237\251\351\230\265/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..bc9dd26dd8668151cefba5536db09a91c3dcfb2f --- /dev/null +++ "b/questions/54.\350\236\272\346\227\213\347\237\251\351\230\265/solution.h" @@ -0,0 +1,33 @@ +// @algorithm @lc id=54 lang=cpp +// @title spiral-matrix + + +// @test([[1,2,3],[4,5,6],[7,8,9]])=[1,2,3,6,9,8,7,4,5] +// @test([[1,2,3,4],[5,6,7,8],[9,10,11,12]])=[1,2,3,4,8,12,11,10,9,5,6,7] +class Solution { +public: + vector spiralOrder(vector>& matrix) { + vector ans; + int left = 0, bottom = matrix.size(), // rows + top = 0, right = matrix[0].size(); // cols + while(true){ + // leftTop -> rightTop + for(int i=left; i= bottom) break; + // rightTop -> rightBottom + for(int i=top; i leftBottom + for(int i=right-1; i>=left; i--) + ans.push_back(matrix[bottom-1][i]); + if(--bottom <= top) break; + // leftBottom -> leftTop + for(int i=bottom-1; i>=top; i--) + ans.push_back(matrix[i][left]); + if(++left >= right) break; + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/55.\350\267\263\350\267\203\346\270\270\346\210\217/main.cpp" "b/questions/55.\350\267\263\350\267\203\346\270\270\346\210\217/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..3f210b1c0fd867255e90a0221ba5acb4d6a871b9 --- /dev/null +++ "b/questions/55.\350\267\263\350\267\203\346\270\270\346\210\217/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[2,3,1,1,4]\"],[\"[3,2,1,0,4]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + bool result=s->canJump(arg0); + string resultabc =serializeBool(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/55.\350\267\263\350\267\203\346\270\270\346\210\217/solution.h" "b/questions/55.\350\267\263\350\267\203\346\270\270\346\210\217/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c0fd115b584796858264e160aaf58728e871e6a2 --- /dev/null +++ "b/questions/55.\350\267\263\350\267\203\346\270\270\346\210\217/solution.h" @@ -0,0 +1,18 @@ +// @algorithm @lc id=55 lang=cpp +// @title jump-game + + +// @test([2,3,1,1,4])=true +// @test([3,2,1,0,4])=false +class Solution { +public: + bool canJump(vector& nums) { + int jmp = nums[0]; + for(int i=0; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + vector arg1 = parseIntegerArr(args[1]); + vector> result=s->insert(arg0,arg1); + string resultabc =serializeIntegerArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/57.\346\217\222\345\205\245\345\214\272\351\227\264/solution.h" "b/questions/57.\346\217\222\345\205\245\345\214\272\351\227\264/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..e6378aef9707af63180edcc655d7ffdf58fbfc8b --- /dev/null +++ "b/questions/57.\346\217\222\345\205\245\345\214\272\351\227\264/solution.h" @@ -0,0 +1,35 @@ +// @algorithm @lc id=57 lang=cpp +// @title insert-interval + + +// #define print(...) +// @test([[1,3],[6,9]],[2,5])=[[1,5],[6,9]] +// @test([[1,2],[3,5],[6,7],[8,10],[12,16]],[4,8])=[[1,2],[3,10],[12,16]] +// @test([],[5,7])=[[5,7]] +// @test([[1,5]],[2,3])=[[1,5]] +// @test([[1,5]],[2,7])=[[1,7]] +// @test([[1,5]], [0,0])=[[0,0],[1,5]] +class Solution { +public: + vector> insert(vector>& intervals, vector& newInterval) { + vector> res; + auto it = intervals.begin(); + for(; it != intervals.end(); it++){ + if(newInterval[1] < (*it)[0]){ + res.push_back(newInterval); + res.insert(res.end(), it, intervals.end()); + return res; + }else if((*it)[1] < newInterval[0]) + res.push_back(*it); + else{ + if((*it)[0] < newInterval[0]) + newInterval[0] = (*it)[0]; + if(newInterval[1] < (*it)[1]) + newInterval[1] = (*it)[1]; + } + } + if(res.empty() || res.back()[1] < newInterval[0]) + res.push_back(newInterval); + return res; + } +}; \ No newline at end of file diff --git "a/questions/583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234/main.cpp" "b/questions/583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c657ce193ea11500bfc0c7eaf72df817bccfea9c --- /dev/null +++ "b/questions/583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"sea\\\"\",\"\\\"eat\\\"\"],[\"\\\"leetcode\\\"\",\"\\\"etco\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + string arg1 = parseString(args[1]); + int result=s->minDistance(arg0,arg1); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234/solution.h" "b/questions/583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..208e9387d13626929f9ead93f4bb1b2ce124c9e3 --- /dev/null +++ "b/questions/583.\344\270\244\344\270\252\345\255\227\347\254\246\344\270\262\347\232\204\345\210\240\351\231\244\346\223\215\344\275\234/solution.h" @@ -0,0 +1,22 @@ +// @algorithm @lc id=583 lang=cpp +// @title delete-operation-for-two-strings + + +// @test("sea","eat")=2 +// @test("leetcode","etco")=4 +class Solution { +public: + int minDistance(string word1, string word2) { + int m = word1.size(), n = word2.size(); + if(m==0 || n==0) return abs(m-n); + vector> dp(m+1, vector(n+1, 0)); + for(int i=1; i<=m; i++) dp[i][0] = i; + for(int j=1; j<=n; j++) dp[0][j] = j; + for(int i=0; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + int result=s->numberOfWeakCharacters(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/5864.\346\270\270\346\210\217\344\270\255\345\274\261\350\247\222\350\211\262\347\232\204\346\225\260\351\207\217/solution.h" "b/questions/5864.\346\270\270\346\210\217\344\270\255\345\274\261\350\247\222\350\211\262\347\232\204\346\225\260\351\207\217/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..6060db6487aecf6576aa6771a58196bb62dd1a49 --- /dev/null +++ "b/questions/5864.\346\270\270\346\210\217\344\270\255\345\274\261\350\247\222\350\211\262\347\232\204\346\225\260\351\207\217/solution.h" @@ -0,0 +1,31 @@ +// @algorithm @lc id=2123 lang=cpp +// @title the-number-of-weak-characters-in-the-game + + +// #define print(...) +// @test([[5,5],[6,3],[3,6]])=0 +// @test([[2,2],[3,3]])=1 +// @test([[1,5],[10,4],[4,3]])=1 +// @test([[1,1],[2,1],[2,2],[1,2]])=1 +class Solution { +public: + int numberOfWeakCharacters(vector>& properties) { + sort(properties.begin(), properties.end(), + [](const vector&a, const vector &b)->bool{ + if(a[0] > b[0]) return true;// [0]降序 + if(a[0] == b[0] && a[1] < b[1]) return true; //[0]相同时[1]升序 + return false; + }); + int pre1 = 0; + int ans = 0; + for(auto & p : properties){ // anyPre[0] >= p[0] + if(pre1 <= p[1]){ + pre1 = p[1]; + }else{ // pre[1] > p[1] + ans++; // 当 pre[0]==p[0] 时 pre[1]<=p[1], 故 pre[0]>p[0] + } + + } + return ans; + } +}; \ No newline at end of file diff --git "a/questions/5882.\347\275\221\346\240\274\346\270\270\346\210\217/main.cpp" "b/questions/5882.\347\275\221\346\240\274\346\270\270\346\210\217/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..336e931b539fe9c063b37ef917f150ee50f8b44b --- /dev/null +++ "b/questions/5882.\347\275\221\346\240\274\346\270\270\346\210\217/main.cpp" @@ -0,0 +1,20 @@ +#include "stl.h" +#include "solution.h" + +int main(int argc, char *argv[]){ + vector> testcases[] = { + {{2,5,4},{1,5,1}}, // 4 + {{3,3,1},{8,5,2}}, // 4 + {{1,3,1,15},{1,3,3,1}}, // 7 + {{20,3,20,17,2,12,15,17,4,15},{20,10,13,14,15,5,2,3,14,3}}, // 63 + }; + Solution sln; + for(auto & ts : testcases){ + print("\ncase:\n", Mat(ts)) + int ans = sln.gridGame(ts); + std::cout<< "ans:" << ans << std::endl; + } + + print("\n------ main exit ------\n") + return 0; +} diff --git "a/questions/5882.\347\275\221\346\240\274\346\270\270\346\210\217/solution.h" "b/questions/5882.\347\275\221\346\240\274\346\270\270\346\210\217/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..4e0a6455e1bfbf59073f621371a79e6c92a3d186 --- /dev/null +++ "b/questions/5882.\347\275\221\346\240\274\346\270\270\346\210\217/solution.h" @@ -0,0 +1,22 @@ +// @algorithm @lc id=2145 lang=cpp +// @title grid-game + + +#define print(...) +class Solution { +public: + long long gridGame(vector>& grid) { + int m = 2, n = grid[0].size(); + vector post(n, 0), pre(n, 0); + for(int j=n-1; j>0; j--) //line1: sum right to left + post[j-1] = post[j] + grid[0][j]; // [0, n-2] + for(int j=0; j> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + vector> result=s->generateMatrix(arg0); + string resultabc =serializeIntegerArrArr(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/59.\350\236\272\346\227\213\347\237\251\351\230\265 II/solution.h" "b/questions/59.\350\236\272\346\227\213\347\237\251\351\230\265 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..63cc04ede5874ba1af2953bc54d2d108675f0908 --- /dev/null +++ "b/questions/59.\350\236\272\346\227\213\347\237\251\351\230\265 II/solution.h" @@ -0,0 +1,38 @@ +// @algorithm @lc id=59 lang=cpp +// @title spiral-matrix-ii + + +#define print(...) +// @test(3)=[[1,2,3],[8,9,4],[7,6,5]] +// @test(1)=[[1]] +class Solution { +public: + vector> generateMatrix(int n) { + vector> mat(n, vector(n)); + int left = 0, right = n, + up = 0, dn = n; + int v = 0; + while(true){ + for(int i=left; i=dn) break; + + for(int i=up; i=left;i--) + mat[dn-1][i] = ++v; + print("dn=", dn, "\n", Mat(mat)) + if(--dn<=up) break; + + for(int i=dn-1; i>=up; i--) + mat[i][left] = ++v; + print("left=", left, "\n", Mat(mat)) + if(++left>=right) break; + } + return mat; + } +}; \ No newline at end of file diff --git "a/questions/60.\346\216\222\345\210\227\345\272\217\345\210\227/main.cpp" "b/questions/60.\346\216\222\345\210\227\345\272\217\345\210\227/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5cc2b5bbf09804b7211d4728735ae6b0fd5ede09 --- /dev/null +++ "b/questions/60.\346\216\222\345\210\227\345\272\217\345\210\227/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"3\",\"3\"],[\"4\",\"9\"],[\"3\",\"1\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int arg1 = parseInteger(args[1]); + string result=s->getPermutation(arg0,arg1); + string resultabc =serializeString(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/60.\346\216\222\345\210\227\345\272\217\345\210\227/solution.h" "b/questions/60.\346\216\222\345\210\227\345\272\217\345\210\227/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..e8484c81dac5d53dcb64b041f46acef4bfa13796 --- /dev/null +++ "b/questions/60.\346\216\222\345\210\227\345\272\217\345\210\227/solution.h" @@ -0,0 +1,18 @@ +// @algorithm @lc id=60 lang=cpp +// @title permutation-sequence + + +// @test(3,3)="213" +// @test(4,9)="2314" +// @test(3,1)="123" +class Solution { +public: + string getPermutation(int n, int k) { + string nstr(n, '1'); + for(int i=1; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + int arg0 = parseInteger(args[0]); + int result=s->findIntegers(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/600.\344\270\215\345\220\253\350\277\236\347\273\2551\347\232\204\351\235\236\350\264\237\346\225\264\346\225\260/solution.h" "b/questions/600.\344\270\215\345\220\253\350\277\236\347\273\2551\347\232\204\351\235\236\350\264\237\346\225\264\346\225\260/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..2c899a2cd49f7d6f596f8df011da93073c20f9e6 --- /dev/null +++ "b/questions/600.\344\270\215\345\220\253\350\277\236\347\273\2551\347\232\204\351\235\236\350\264\237\346\225\264\346\225\260/solution.h" @@ -0,0 +1,29 @@ +// @algorithm @lc id=600 lang=cpp +// @title non-negative-integers-without-consecutive-ones + + +// @test(5)=5 +// @test(1)=2 +// @test(2)=3 +// @test(3)=3 +class Solution { +public: + int findIntegers(int n) { + int N = 30, i = 2; + vector dp(N+1, 1); + for(; i0; i--){ + int b = n & (1<<(i-1)); + if(b){ + ans += dp[i]; + if(II) break; + n -= b; + } + II = b; + } + return ans + (i==0); + } +}; \ No newline at end of file diff --git "a/questions/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.cpp" "b/questions/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..a36e67071160eed121614535071af1cdd3e12e99 --- /dev/null +++ "b/questions/61.\346\227\213\350\275\254\351\223\276\350\241\250/main.cpp" @@ -0,0 +1,19 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[1,2,3,4,5]\",\"2\"],[\"[0,1,2]\",\"4\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + ListNode * arg0 = parseListNode(args[0]); + int arg1 = parseInteger(args[1]); + ListNode * result=s->rotateRight(arg0,arg1); + string resultabc =serializeListNode(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/61.\346\227\213\350\275\254\351\223\276\350\241\250/solution.h" "b/questions/61.\346\227\213\350\275\254\351\223\276\350\241\250/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..d5d7b8aeebd30b8b36d812c08125e8c16bbdf07d --- /dev/null +++ "b/questions/61.\346\227\213\350\275\254\351\223\276\350\241\250/solution.h" @@ -0,0 +1,38 @@ +// @algorithm @lc id=61 lang=cpp +// @title rotate-list + + +// @test([1,2,3,4,5],2)=[4,5,1,2,3] +// @test([0,1,2],4)=[2,0,1] +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* rotateRight(ListNode* head, int k) { + if(!head) return NULL; + auto p = head, pre=p; + int n = 0; + while(p){ + pre = p; + p = p->next; + n++; + } + if(0==k%n) return head; + pre->next = head; + n -= k%n; + while(n--){ + pre = head; + head = head->next; + } + pre->next = NULL; + return head; + } +}; \ No newline at end of file diff --git "a/questions/63.\344\270\215\345\220\214\350\267\257\345\276\204 II/main.cpp" "b/questions/63.\344\270\215\345\220\214\350\267\257\345\276\204 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..5614664e93700af9f8f4bf0b56366bfe87d51b62 --- /dev/null +++ "b/questions/63.\344\270\215\345\220\214\350\267\257\345\276\204 II/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"[[0,0,0],[0,1,0],[0,0,0]]\"],[\"[[0,1],[0,0]]\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector> arg0 = parseIntegerArrArr(args[0]); + int result=s->uniquePathsWithObstacles(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/63.\344\270\215\345\220\214\350\267\257\345\276\204 II/solution.h" "b/questions/63.\344\270\215\345\220\214\350\267\257\345\276\204 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..1eca0f9b63c987cd486f5e8108e68620909ab83b --- /dev/null +++ "b/questions/63.\344\270\215\345\220\214\350\267\257\345\276\204 II/solution.h" @@ -0,0 +1,26 @@ +// @algorithm @lc id=63 lang=cpp +// @title unique-paths-ii + + +// @test([[0,0,0],[0,1,0],[0,0,0]])=2 +// @test([[0,1],[0,0]])=1 +class Solution { +public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + if(obstacleGrid[0][0] || obstacleGrid.back().back()) return 0; + int m = obstacleGrid.size(), n = obstacleGrid[0].size(); + vector dp(n, 0); dp[0] = 1; + for(int i=0; i> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + vector arg0 = parseIntegerArr(args[0]); + vector> arg1 = parseIntegerArrArr(args[1]); + vector arg2 = parseIntegerArr(args[2]); + int result=s->shoppingOffers(arg0,arg1,arg2); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/638.\345\244\247\347\244\274\345\214\205/solution.h" "b/questions/638.\345\244\247\347\244\274\345\214\205/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..7a0f45f666d3a2a095ee73e8756fd5768426f331 --- /dev/null +++ "b/questions/638.\345\244\247\347\244\274\345\214\205/solution.h" @@ -0,0 +1,45 @@ +// @algorithm @lc id=638 lang=cpp +// @title shopping-offers + + +// @test([2,5],[[3,0,5],[1,2,10]],[3,2])=14 +// @test([2,3,4],[[1,1,0,4],[2,2,1,9]],[1,2,1])=11 +class Solution { +public: + int shoppingOffers(vector& price, vector>& special, vector& needs) { + int n = price.size(); + vector> gifts; // 选择有物品,且价格低于物品总价值的礼包 + for(auto & gift : special){ + int itemCnt = 0, itemPrice = 0; + for(int i=0; i/*item*/, int/*cost*/> minPay; + function)> dfs = [&](vector item){ + auto it = minPay.find(item); + if( it != minPay.end() ) + return it->second; + int minCost = 0; // 不购买任何礼包的花费 + for(int i=0; i toBuy; + for(int i=0; i item[i]) break; + toBuy.push_back(item[i]-gift[i]); + } + if(toBuy.size()==n){ // 礼包可买=>对剩余未买物品继续求解 + minCost = min(minCost, gift[n] + dfs(toBuy)); + minPay[item] = minCost; + } + } + return minCost; + }; + return dfs(needs); + } +}; \ No newline at end of file diff --git "a/questions/639.\350\247\243\347\240\201\346\226\271\346\263\225 II/main.cpp" "b/questions/639.\350\247\243\347\240\201\346\226\271\346\263\225 II/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..fafb923b676061c4cb0d4fd842b1083865eab18b --- /dev/null +++ "b/questions/639.\350\247\243\347\240\201\346\226\271\346\263\225 II/main.cpp" @@ -0,0 +1,18 @@ +#include "stl.h" +#include "parse.h" +#include "solution.h" +int main(int argc, char *argv[]) +{ + string str = "[[\"\\\"*\\\"\"],[\"\\\"1*\\\"\"],[\"\\\"2*\\\"\"]]"; + vector> arr = parseStringArrArr(str); + for (int i = 0; i < arr.size(); i++) + { + vector args = arr[i]; + Solution *s = new Solution(); + string arg0 = parseString(args[0]); + int result=s->numDecodings(arg0); + string resultabc =serializeInteger(result); + cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl; + } + return 0; +} \ No newline at end of file diff --git "a/questions/639.\350\247\243\347\240\201\346\226\271\346\263\225 II/solution.h" "b/questions/639.\350\247\243\347\240\201\346\226\271\346\263\225 II/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c793b0f850542b16620bd37aec4216739571e84b --- /dev/null +++ "b/questions/639.\350\247\243\347\240\201\346\226\271\346\263\225 II/solution.h" @@ -0,0 +1,42 @@ +// @algorithm @lc id=639 lang=cpp +// @title decode-ways-ii + + +// @test("*")=9 +// @test("1*")=18 +// @test("2*")=15 +#define mod 1000'000'007 +class Solution { + int64_t d0; + int64_t d1; +public: + int count(char cur){ + return '0'==cur ? 0 : ('*'==cur ? 9 : 1); + } + int count(char pre, char cur){ + if('*'==pre && '*'==cur) + return 15; // [11,19] [21,26] + if('*'==pre) + return cur <= '6' ? 2 : 1; + if('*'==cur){ + if('1'==pre) return 9; + if('2'==pre) return 6; + return 0; + } + return '0'
> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      string arg0 = parseString(args[0]);
+      bool result=s->isNumber(arg0);
+      string resultabc =serializeBool(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/65.\346\234\211\346\225\210\346\225\260\345\255\227/solution.h" "b/questions/65.\346\234\211\346\225\210\346\225\260\345\255\227/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..b2f1ee4b6ad2fbf43ef04108b2aff7c6e24273e9
--- /dev/null
+++ "b/questions/65.\346\234\211\346\225\210\346\225\260\345\255\227/solution.h"
@@ -0,0 +1,116 @@
+// @algorithm @lc id=65 lang=cpp 
+// @title valid-number
+
+
+// @test("0")=true
+// @test("e")=false
+// @test(".")=false
+// @test(".1")=true
+
+/*作者:LeetCode-Solution
+链接:https://leetcode-cn.com/problems/valid-number/solution/you-xiao-shu-zi-by-leetcode-solution-298l/
+来源:力扣(LeetCode)
+著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/
+class Solution {
+public:
+    enum State {
+        STATE_INITIAL,
+        STATE_INT_SIGN,         // 符号位
+        STATE_INTEGER,          // 整数部分
+        STATE_POINT,            // 小数点(左有整数)
+        STATE_POINT_WITHOUT_INT,// 小数点(左无整数)
+        STATE_FRACTION,         // 小数部分
+        STATE_EXP,              // 字符 e
+        STATE_EXP_SIGN,         // 指数符号
+        STATE_EXP_NUMBER,       // 指数数字
+    };
+
+    enum CharType {
+        CHAR_NUMBER,            // 0-9
+        CHAR_EXP,               // eE
+        CHAR_POINT,             // .
+        CHAR_SIGN,              // +-
+        CHAR_ILLEGAL            // 非法字符
+    };
+
+    CharType toCharType(char ch) {
+        if (ch >= '0' && ch <= '9') {
+            return CHAR_NUMBER;
+        } else if (ch == 'e' || ch == 'E') {
+            return CHAR_EXP;
+        } else if (ch == '.') {
+            return CHAR_POINT;
+        } else if (ch == '+' || ch == '-') {
+            return CHAR_SIGN;
+        } else {
+            return CHAR_ILLEGAL;
+        }
+    }
+
+    inline static
+    unordered_map> transfer =
+    {
+        {
+            STATE_INITIAL, {
+                {CHAR_NUMBER, STATE_INTEGER},
+                {CHAR_POINT, STATE_POINT_WITHOUT_INT},
+                {CHAR_SIGN, STATE_INT_SIGN}
+            }
+        }, {
+            STATE_INT_SIGN, {
+                {CHAR_NUMBER, STATE_INTEGER},
+                {CHAR_POINT, STATE_POINT_WITHOUT_INT}
+            }
+        }, {
+            STATE_INTEGER, {
+                {CHAR_NUMBER, STATE_INTEGER},
+                {CHAR_EXP, STATE_EXP},
+                {CHAR_POINT, STATE_POINT}
+            }
+        }, {
+            STATE_POINT, {
+                {CHAR_NUMBER, STATE_FRACTION},
+                {CHAR_EXP, STATE_EXP}
+            }
+        }, {
+            STATE_POINT_WITHOUT_INT, {
+                {CHAR_NUMBER, STATE_FRACTION}
+            }
+        }, {
+            STATE_FRACTION,
+            {
+                {CHAR_NUMBER, STATE_FRACTION},
+                {CHAR_EXP, STATE_EXP}
+            }
+        }, {
+            STATE_EXP,
+            {
+                {CHAR_NUMBER, STATE_EXP_NUMBER},
+                {CHAR_SIGN, STATE_EXP_SIGN}
+            }
+        }, {
+            STATE_EXP_SIGN, {
+                {CHAR_NUMBER, STATE_EXP_NUMBER}
+            }
+        }, {
+            STATE_EXP_NUMBER, {
+                {CHAR_NUMBER, STATE_EXP_NUMBER}
+            }
+        }
+    };
+
+    bool isNumber(string s) {
+        int len = s.length();
+        State st = STATE_INITIAL;
+
+        for (int i = 0; i < len; i++) {
+            CharType typ = toCharType(s[i]);
+            if (transfer[st].find(typ) == transfer[st].end()) {
+                return false;
+            } else {
+                st = transfer[st][typ];
+            }
+        }
+        return st == STATE_INTEGER || st == STATE_POINT || st == STATE_FRACTION || st == STATE_EXP_NUMBER;
+    }
+};
\ No newline at end of file
diff --git "a/questions/65.\346\234\211\346\225\210\346\225\260\345\255\227/state_machine.jpg" "b/questions/65.\346\234\211\346\225\210\346\225\260\345\255\227/state_machine.jpg"
new file mode 100644
index 0000000000000000000000000000000000000000..ce44edc3f8b02f65a2efb55ab97818516f8baa75
Binary files /dev/null and "b/questions/65.\346\234\211\346\225\210\346\225\260\345\255\227/state_machine.jpg" differ
diff --git "a/questions/673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260/main.cpp" "b/questions/673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..3bb4d9090ac2e84004283c6106ccedfe2431357d
--- /dev/null
+++ "b/questions/673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260/main.cpp"
@@ -0,0 +1,18 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"[1,3,5,4,7]\"],[\"[2,2,2,2,2]\"],[\"[1,1,1,2,2,2,3,3,3]\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector arg0 = parseIntegerArr(args[0]);
+      int result=s->findNumberOfLIS(arg0);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260/solution.h" "b/questions/673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..e28fdaf8709aa79d8326f7d056dd7bc762ea1337
--- /dev/null
+++ "b/questions/673.\346\234\200\351\225\277\351\200\222\345\242\236\345\255\220\345\272\217\345\210\227\347\232\204\344\270\252\346\225\260/solution.h"
@@ -0,0 +1,34 @@
+// @algorithm @lc id=673 lang=cpp 
+// @title number-of-longest-increasing-subsequence
+
+
+// @test([1,3,5,4,7])=2
+// @test([2,2,2,2,2])=5
+// @test([1,1,1,2,2,2,3,3,3])=27
+class Solution {
+public:
+    int findNumberOfLIS(vector& nums) {
+        int n = nums.size(), maxLen = 0, ans = 0;
+        vector dp(n, 1), cnt(n, 1);
+        for(int i=0; i> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      string arg0 = parseString(args[0]);
+      bool result=s->checkValidString(arg0);
+      string resultabc =serializeBool(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/678.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262/solution.h" "b/questions/678.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..5cca2ef0b7bc31822a05b1959b5afebf612328b5
--- /dev/null
+++ "b/questions/678.\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267\345\255\227\347\254\246\344\270\262/solution.h"
@@ -0,0 +1,43 @@
+// @algorithm @lc id=678 lang=cpp 
+// @title valid-parenthesis-string
+
+
+#define print(...)
+// @test("()")=true
+// @test("(*)")=true
+// @test("(*))")=true
+// @test("(")=false
+// @test("(((((()*)(*)*))())())(()())())))((**)))))(()())()")=false
+class Solution {
+public:
+    bool checkValidString(string s) {
+        list stkLeft, stkStar;
+        print("input=", s, "\n")
+        for(int i=0; i= stkStar.back())
+                return false;
+            stkLeft.pop_back();
+            stkStar.pop_back();
+        }
+        print(", size:", stkLeft.size(), ", ", stkStar.size(), "\n")
+        return stkLeft.empty();
+    }
+};
\ No newline at end of file
diff --git "a/questions/72.\347\274\226\350\276\221\350\267\235\347\246\273/main.cpp" "b/questions/72.\347\274\226\350\276\221\350\267\235\347\246\273/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..57a86fbe239a4c866b68573cf653e405722e2a8b
--- /dev/null
+++ "b/questions/72.\347\274\226\350\276\221\350\267\235\347\246\273/main.cpp"
@@ -0,0 +1,19 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"\\\"horse\\\"\",\"\\\"ros\\\"\"],[\"\\\"intention\\\"\",\"\\\"execution\\\"\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      string arg0 = parseString(args[0]);
+      string arg1 = parseString(args[1]);
+      int result=s->minDistance(arg0,arg1);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/72.\347\274\226\350\276\221\350\267\235\347\246\273/solution.h" "b/questions/72.\347\274\226\350\276\221\350\267\235\347\246\273/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..6eec67c86b031c3ed4afaa81ae000e0e63115d6d
--- /dev/null
+++ "b/questions/72.\347\274\226\350\276\221\350\267\235\347\246\273/solution.h"
@@ -0,0 +1,23 @@
+// @algorithm @lc id=72 lang=cpp 
+// @title edit-distance
+
+
+// @test("horse","ros")=3
+// @test("intention","execution")=5
+class Solution {
+public:
+    int minDistance(string word1, string word2) {
+        int m = word1.size(), n = word2.size();
+        vector> dp(m+1, vector(n+1, 0));
+        for(int i=0; i<=m; i++) dp[i][0] = i;
+        for(int i=0; i<=n; i++) dp[0][i] = i;
+        for(int i=1; i<=m; i++){
+            for(int j=1; j<=n; j++){
+                int c = dp[i-1][j-1];
+                if(word1[i-1]-word2[j-1]) ++c;
+                dp[i][j] = min(c, min(dp[i-1][j]+1, dp[i][j-1]+1));
+            }
+        }
+        return dp[m][n];
+    }
+};
\ No newline at end of file
diff --git "a/questions/725.\345\210\206\351\232\224\351\223\276\350\241\250/main.cpp" "b/questions/725.\345\210\206\351\232\224\351\223\276\350\241\250/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..1fe69e36256942bd0c3cb9db36b68d36354f0796
--- /dev/null
+++ "b/questions/725.\345\210\206\351\232\224\351\223\276\350\241\250/main.cpp"
@@ -0,0 +1,19 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"[1,2,3]\",\"5\"],[\"[1,2,3,4,5,6,7,8,9,10]\",\"3\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      ListNode * arg0 = parseListNode(args[0]);
+      int arg1 = parseInteger(args[1]);
+      vector result=s->splitListToParts(arg0,arg1);
+      string resultabc =serializeListNodeArr(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/725.\345\210\206\351\232\224\351\223\276\350\241\250/solution.h" "b/questions/725.\345\210\206\351\232\224\351\223\276\350\241\250/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..964a371b7cd9f99cbc911b1055ae0e3c2e82a5dc
--- /dev/null
+++ "b/questions/725.\345\210\206\351\232\224\351\223\276\350\241\250/solution.h"
@@ -0,0 +1,35 @@
+// @algorithm @lc id=725 lang=cpp 
+// @title split-linked-list-in-parts
+
+
+// @test([1,2,3],5)=[[1],[2],[3],[],[]]
+// @test([1,2,3,4,5,6,7,8,9,10],3)=[[1,2,3,4],[5,6,7],[8,9,10]]
+class Solution {
+public:
+    vector splitListToParts(ListNode* head, int k) {
+        vector ans(k, NULL);
+        ListNode dumy(0, head), *p=&dumy;
+        int n = 0;
+        while(p=p->next) n++;
+        int cnt = n/k, extra = n%k;
+        p = &dumy;
+        for(int i=0; inext;
+            p->next = NULL;
+            p = ans[i];
+            for(int j=1; p && jnext;
+        }
+        return ans;
+    }
+};
+/**
+ * Definition for singly-linked list.
+ * struct ListNode {
+ *     int val;
+ *     ListNode *next;
+ *     ListNode() : val(0), next(nullptr) {}
+ *     ListNode(int x) : val(x), next(nullptr) {}
+ *     ListNode(int x, ListNode *next) : val(x), next(next) {}
+ * };
+ */
\ No newline at end of file
diff --git "a/questions/75.\351\242\234\350\211\262\345\210\206\347\261\273/main.cpp" "b/questions/75.\351\242\234\350\211\262\345\210\206\347\261\273/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..099a4ffae28a94eb6822cf131be26a8c66692d75
--- /dev/null
+++ "b/questions/75.\351\242\234\350\211\262\345\210\206\347\261\273/main.cpp"
@@ -0,0 +1,18 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"[2,0,2,1,1,0]\"],[\"[2,0,1]\"],[\"[0]\"],[\"[1]\"],[\"[1,2,0]\"],[\"[0,1,0]\"],[\"[2,0,2,1,1,0]\"],[\"[2,2,1]\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector arg0 = parseIntegerArr(args[0]);
+      s->sortColors(arg0);
+      string resultabc =serializeIntegerArr(arg0);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/75.\351\242\234\350\211\262\345\210\206\347\261\273/solution.h" "b/questions/75.\351\242\234\350\211\262\345\210\206\347\261\273/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..230d974454ea3f6162cdddaa3e6bfea243e85da8
--- /dev/null
+++ "b/questions/75.\351\242\234\350\211\262\345\210\206\347\261\273/solution.h"
@@ -0,0 +1,24 @@
+// @algorithm @lc id=75 lang=cpp 
+// @title sort-colors
+
+
+// @test([2,0,2,1,1,0])=[0,0,1,1,2,2]
+// @test([2,0,1])=[0,1,2]
+// @test([0])=[0]
+// @test([1])=[1]
+// @test([1,2,0])=[0,1,2]
+// @test([0,1,0])=[0,0,1]
+// @test([2,0,2,1,1,0])=[0,0,1,1,2,2]
+// @test([2,2,1])=[1,2,2]
+class Solution {
+public:
+    void sortColors(vector& nums) {
+        int front = 0, back = nums.size() - 1;
+        for(int i=front; i<=back; i++){
+            while(2==nums[i] && i<=back)
+                swap(nums[i], nums[back--]);
+            if(0==nums[i])
+                swap(nums[i], nums[front++]);
+        }
+    }
+};
\ No newline at end of file
diff --git "a/questions/76.\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/main.cpp" "b/questions/76.\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..3f7ba55a046071046ad38f2dd3d6a2bf5ee63268
--- /dev/null
+++ "b/questions/76.\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/main.cpp"
@@ -0,0 +1,19 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"\\\"ADOBECODEBANC\\\"\",\"\\\"ABC\\\"\"],[\"\\\"a\\\"\",\"\\\"a\\\"\"],[\"\\\"a\\\"\",\"\\\"aa\\\"\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      string arg0 = parseString(args[0]);
+      string arg1 = parseString(args[1]);
+      string result=s->minWindow(arg0,arg1);
+      string resultabc =serializeString(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/76.\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.h" "b/questions/76.\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..f2f32a8213e0db177c4e3ef89b12184584e71f27
--- /dev/null
+++ "b/questions/76.\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262/solution.h"
@@ -0,0 +1,34 @@
+// @algorithm @lc id=76 lang=cpp 
+// @title minimum-window-substring
+
+
+// @test("ADOBECODEBANC","ABC")="BANC"
+// @test("a","a")="a"
+// @test("a","aa")=""
+class Solution {
+public:
+    string minWindow(string s, string t) {
+        unordered_map src, dst;
+        for(auto &c : t) ++dst[c];
+        function IsWin = [&]()->bool{
+            for(auto &[c,n] : dst)
+                if(src[c] < n) return false;
+            return true;
+        };
+        int left=0, right=0; // 可能包含 t 的滑动窗口
+        int sub=-1, len=INT_MAX; // 最小子串
+        for(int left=0,right=0; right> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector> arg0 = parseStringArrArr(args[0]);
+      vector> ts(arg0.size(), vector(arg0[0].size()));
+      vv_conv(arg0, ts, [](const string&f){return f[0];});
+      string arg1 = parseString(args[1]);
+      bool result=s->exist(ts,arg1);
+      string resultabc =serializeBool(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/79.\345\215\225\350\257\215\346\220\234\347\264\242/solution.h" "b/questions/79.\345\215\225\350\257\215\346\220\234\347\264\242/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..ae40434e0f5894ae8ea1405bc6ee4ee15adac033
--- /dev/null
+++ "b/questions/79.\345\215\225\350\257\215\346\220\234\347\264\242/solution.h"
@@ -0,0 +1,36 @@
+// @algorithm @lc id=79 lang=cpp 
+// @title word-search
+
+
+// @test([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCCED")=true
+// @test([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"SEE")=true
+// @test([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]],"ABCB")=false
+using it_t = string::iterator;
+class Solution {
+public:
+    bool exist(vector>& board, string word) {
+        int m = board.size(), n = board[0].size();
+        // 字典 char => (i,j)
+        unordered_map< char,vector> > dict;
+        for(int i=0; i> used(m, vector(n, false));
+        function dfs = [&](int i, int j, it_t letter)->bool{
+            if(letter == word.end()) return true;
+            auto it = dict.find(*letter);
+            if(it == dict.end()) return false;
+            for(auto &[x,y] : it->second){
+                if(used[x][y] || (0<=i && abs(x-i)+abs(y-j)!=1))
+                    continue;
+                used[x][y] = true;
+                if(dfs(x, y, letter+1)) return true;
+                used[x][y] = false;
+            }
+            return false;
+        };
+        return dfs(-1, -1, word.begin());
+    }
+};
\ No newline at end of file
diff --git "a/questions/80.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/main.cpp" "b/questions/80.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..46c893d16d5392394085554119906a4abd043dfa
--- /dev/null
+++ "b/questions/80.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/main.cpp"	
@@ -0,0 +1,18 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"[1,1,1,2,2,3]\"],[\"[0,0,1,1,1,1,2,3,3]\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector arg0 = parseIntegerArr(args[0]);
+      int result=s->removeDuplicates(arg0);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/80.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.h" "b/questions/80.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..12ad522f4c39be90c2ade94be67eff2e14558427
--- /dev/null
+++ "b/questions/80.\345\210\240\351\231\244\346\234\211\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271 II/solution.h"	
@@ -0,0 +1,17 @@
+// @algorithm @lc id=80 lang=cpp 
+// @title remove-duplicates-from-sorted-array-ii
+
+
+// @test([1,1,1,2,2,3])=5
+// @test([0,0,1,1,1,1,2,3,3])=7
+class Solution {
+public:
+    int removeDuplicates(vector& nums) {
+        if(nums.size()<3) return nums.size();
+        int j = 2;
+        for(int i=2; i> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector arg0 = parseIntegerArr(args[0]);
+      int result=s->largestRectangleArea(arg0);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/84.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.h" "b/questions/84.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..c538daa6fe82ee350e42b1e58af29725f0fcc962
--- /dev/null
+++ "b/questions/84.\346\237\261\347\212\266\345\233\276\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/solution.h"
@@ -0,0 +1,39 @@
+// @algorithm @lc id=84 lang=cpp 
+// @title largest-rectangle-in-histogram
+
+
+#define print(...)
+// @test([2,1,5,6,2,3])=10
+// @test([2,4])=4
+class Solution {
+public:
+    int largestRectangleArea(vector& heights) {
+        int n = heights.size();
+        list stk;
+        vector leftFirstShort(n, -1);
+        for(int i=0; i= heights[i])
+                stk.pop_back();
+            if(stk.size())
+                leftFirstShort[i] = stk.back();
+            stk.push_back(i);
+        }
+        print("left:", leftFirstShort, "\n")
+        
+        stk.clear();
+        vector rightFirstShort(n, n);
+        for(int i=n-1; i>=0; i--){
+            while(stk.size() && heights[stk.back()] >= heights[i])
+                stk.pop_back();
+            if(stk.size())
+                rightFirstShort[i] = stk.back();
+            stk.push_back(i);
+        }
+        print("right:", rightFirstShort, "\n")
+
+        int ans = 0;
+        for(int i=0; i> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector> arg0 = parseStringArrArr(args[0]);
+      vector> ts;
+      if(arg0.size()>0 && arg0[0].size()>0)
+        ts = vector>(arg0.size(), vector(arg0[0].size()));
+      vv_conv(arg0, ts, [](const string&f){return f[0];});
+      int result=s->maximalRectangle(ts);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/85.\346\234\200\345\244\247\347\237\251\345\275\242/solution.h" "b/questions/85.\346\234\200\345\244\247\347\237\251\345\275\242/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..5c8a57e243a234ebfdf7705a3c85cea1b17f6d73
--- /dev/null
+++ "b/questions/85.\346\234\200\345\244\247\347\237\251\345\275\242/solution.h"
@@ -0,0 +1,60 @@
+// @algorithm @lc id=85 lang=cpp 
+// @title maximal-rectangle
+
+
+#define print(...)
+// @test([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]])=6
+// @test([])=0
+// @test([["0"]])=0
+// @test([["1"]])=1
+// @test([["0","0"]])=0
+class Solution {
+public:
+    int maximalRectangle(vector>& matrix) {
+        int rows = matrix.size();
+        if(0==rows) return 0;
+        int cols = matrix[0].size();
+        if(0==cols) return 0;
+        vector> hmat(cols, vector(rows, 0));
+        for(int j=0; j& heights) {
+        int n = heights.size();
+        list stk;
+        vector leftFirstShort(n, -1);
+        for(int i=0; i= heights[i])
+                stk.pop_back();
+            if(stk.size())
+                leftFirstShort[i] = stk.back();
+            stk.push_back(i);
+        }
+        print("left:", leftFirstShort, "\n")
+        
+        stk.clear();
+        vector rightFirstShort(n, n);
+        for(int i=n-1; i>=0; i--){
+            while(stk.size() && heights[stk.back()] >= heights[i])
+                stk.pop_back();
+            if(stk.size())
+                rightFirstShort[i] = stk.back();
+            stk.push_back(i);
+        }
+        print("right:", rightFirstShort, "\n")
+
+        int ans = 0;
+        for(int i=0; i> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      int arg0 = parseInteger(args[0]);
+      bool result=s->reorderedPowerOf2(arg0);
+      string resultabc =serializeBool(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/869.\351\207\215\346\226\260\346\216\222\345\272\217\345\276\227\345\210\260 2 \347\232\204\345\271\202/solution.h" "b/questions/869.\351\207\215\346\226\260\346\216\222\345\272\217\345\276\227\345\210\260 2 \347\232\204\345\271\202/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..7643b9534608c75b3d61fe64e80940f86ec43615
--- /dev/null
+++ "b/questions/869.\351\207\215\346\226\260\346\216\222\345\272\217\345\276\227\345\210\260 2 \347\232\204\345\271\202/solution.h"	
@@ -0,0 +1,28 @@
+// @algorithm @lc id=900 lang=cpp 
+// @title reordered-power-of-2
+
+
+#define print(...)
+// @test(1)=true
+// @test(10)=false
+// @test(16)=true
+// @test(24)=false
+// @test(46)=true
+class Solution {
+    unordered_set keys;
+public:
+    string hash(int n){
+        string key(10, '0');
+        while(n){
+            ++key[n%10];
+            n = n/10;
+        }
+        return key;
+    }
+    bool reorderedPowerOf2(int n) {
+        for(int i=1; i<=1e9; i<<=1)
+            keys.insert(hash(i));
+        print("hash(", n, ")=", hash(n), "\n")
+        return keys.count(hash(n));
+    }
+};
\ No newline at end of file
diff --git "a/questions/91.\350\247\243\347\240\201\346\226\271\346\263\225/main.cpp" "b/questions/91.\350\247\243\347\240\201\346\226\271\346\263\225/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..aa226fd7b8e1b46ab31d303afd5083687160d90a
--- /dev/null
+++ "b/questions/91.\350\247\243\347\240\201\346\226\271\346\263\225/main.cpp"
@@ -0,0 +1,18 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"\\\"12\\\"\"],[\"\\\"226\\\"\"],[\"\\\"0\\\"\"],[\"\\\"06\\\"\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      string arg0 = parseString(args[0]);
+      int result=s->numDecodings(arg0);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/91.\350\247\243\347\240\201\346\226\271\346\263\225/solution.h" "b/questions/91.\350\247\243\347\240\201\346\226\271\346\263\225/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..f4beb7e0e1f1842532d61adb7ae1c9f580e87d76
--- /dev/null
+++ "b/questions/91.\350\247\243\347\240\201\346\226\271\346\263\225/solution.h"
@@ -0,0 +1,24 @@
+// @algorithm @lc id=91 lang=cpp 
+// @title decode-ways
+
+
+// @test("12")=2
+// @test("226")=3
+// @test("0")=0
+// @test("06")=0
+class Solution {
+public:
+    int numDecodings(string s) {
+        int d0 = 1, d1 = 1, d2 = 0;
+        for(int i=1; i<=s.size(); i++){
+            d2 = 0;
+            if(s[i-1] != '0')
+                d2 += d1;
+            if(1> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      TreeNode * arg0 = parseTreeNode(args[0]);
+      vector result=s->inorderTraversal(arg0);
+      string resultabc =serializeIntegerArr(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/solution.h" "b/questions/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..1c28c2a598dff906b0434d27bf2b25c88c99fe73
--- /dev/null
+++ "b/questions/94.\344\272\214\345\217\211\346\240\221\347\232\204\344\270\255\345\272\217\351\201\215\345\216\206/solution.h"
@@ -0,0 +1,71 @@
+// @algorithm @lc id=94 lang=cpp 
+// @title binary-tree-inorder-traversal
+
+
+// @test([1,null,2,3])=[1,3,2]
+// @test([])=[]
+// @test([1])=[1]
+// @test([1,2])=[2,1]
+// @test([1,null,2])=[1,2]
+class Solution {
+public:
+    static void Recursion(TreeNode* root, vector &res){ // 递归:T(n),M(n)
+        if(!root) return;
+        if(root->left) Recursion(root->left, res);
+        res.push_back(root->val);
+        if(root->right) Recursion(root->right, res);
+    }
+    static void Iteration(TreeNode* cur, vector &res){ // 迭代(栈):T(n),M(n)
+        list stk;
+        while(cur || stk.size()){
+            while(cur){ // iterate cur to find most left
+                stk.push_back(cur);
+                cur = cur->left;
+            }
+            cur = stk.back();
+            res.push_back(cur->val); // visit *root [of most left]
+            cur = cur->right; // iterate right
+            stk.pop_back();
+        }
+    }
+    static void Morris(TreeNode* cur, vector &res){ // 莫里斯遍历:T(n),M(1)
+        TreeNode * pre = NULL;
+        while(cur){
+            if(!cur->left){ // 无左孩子: visit(*cur), then iterate right
+                res.push_back(cur->val);
+                cur = cur->right;
+            }else{ // 有左孩子:
+                pre = cur->left;
+                while(pre->right && pre->right!=cur)
+                    pre = pre->right; // 查找 左子树的most_right
+                if(NULL == pre->right){
+                    pre->right = cur; // 使 [左子树的most_right]->right 指向 cur
+                    cur = cur->left;  // iterate left
+                }else{ // cur->left->right 指向 cur: cur->left是 [左子树的most_right], 左子树访问结束
+                    res.push_back(cur->val);
+                    cur = cur->right;
+                    pre->right = NULL;
+                }
+            }
+        }
+    }
+    vector inorderTraversal(TreeNode* root) {
+        vector res;
+        // Recursion(root, res);
+        // Iteration(root, res);
+        Morris(root, res);
+        // print(res, "\n")
+        return res;
+    }
+};
+/**
+ * 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) {}
+ * };
+ */
\ No newline at end of file
diff --git "a/questions/96.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/main.cpp" "b/questions/96.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..ab08123e378ede351751a876e5ad068721146f88
--- /dev/null
+++ "b/questions/96.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/main.cpp"
@@ -0,0 +1,18 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"3\"],[\"1\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      int arg0 = parseInteger(args[0]);
+      int result=s->numTrees(arg0);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/96.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/solution.h" "b/questions/96.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..4417b35eb97b51ab4fe76fef31c5c4b9f6c79aa2
--- /dev/null
+++ "b/questions/96.\344\270\215\345\220\214\347\232\204\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/solution.h"
@@ -0,0 +1,29 @@
+// @algorithm @lc id=96 lang=cpp 
+// @title unique-binary-search-trees
+
+
+// @test(3)=5
+// @test(1)=1
+class Solution {
+public:
+    int DP(int n){ // 动态规划
+        vector dp(n+1, 0);
+        dp[0] = dp[1] = 1;
+        for(int i=2; i<=n; i++){
+            for(int j=1; j<=i; j++)
+                dp[i] += dp[j-1] * dp[i-j];
+        }
+        return dp[n];
+    }
+
+    int Catalan(int n){ // 卡塔兰数
+        int ct = 1;
+        for(int i=0; i> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      TreeNode * arg0 = parseTreeNode(args[0]);
+      bool result=s->isValidBST(arg0);
+      string resultabc =serializeBool(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/98.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/solution.h" "b/questions/98.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..391c399e94f2309a6a136a533bfc238a64c82369
--- /dev/null
+++ "b/questions/98.\351\252\214\350\257\201\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221/solution.h"
@@ -0,0 +1,32 @@
+// @algorithm @lc id=98 lang=cpp 
+// @title validate-binary-search-tree
+
+
+// @test([2,1,3])=true
+// @test([5,1,4,null,null,3,6])=false
+class Solution {
+    int64_t last = int64_t(INT_MIN) - 1;
+    bool InOrder(TreeNode * node){
+        if(!node) return true;
+        if(node->left && !InOrder(node->left)) return false;
+        if(node->val <= last) return false;
+        last = node->val;
+        if(node->right) return InOrder(node->right);
+        return true;
+    }
+public:
+    bool isValidBST(TreeNode* root) {
+        return InOrder(root);
+    }
+};
+/**
+ * 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) {}
+ * };
+ */
\ No newline at end of file
diff --git "a/questions/\345\211\221\346\214\207 Offer 10- I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/main.cpp" "b/questions/\345\211\221\346\214\207 Offer 10- I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..a220b47e1e59ab49ead31d75c75bd3807d6fd964
--- /dev/null
+++ "b/questions/\345\211\221\346\214\207 Offer 10- I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/main.cpp"	
@@ -0,0 +1,18 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"2\"],[\"5\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      int arg0 = parseInteger(args[0]);
+      int result=s->fib(arg0);
+      string resultabc =serializeInteger(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/\345\211\221\346\214\207 Offer 10- I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/solution.h" "b/questions/\345\211\221\346\214\207 Offer 10- I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..81349b5baa83cdebc54464ac0ffacd78a3fa83cb
--- /dev/null
+++ "b/questions/\345\211\221\346\214\207 Offer 10- I.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/solution.h"	
@@ -0,0 +1,18 @@
+// @algorithm @lc id=100274 lang=cpp 
+// @title fei-bo-na-qi-shu-lie-lcof
+
+// @test(2)=1
+// @test(5)=5
+class Solution {
+public:
+    int fib(int n) {
+        if(n<2) return n;
+        int a=0, b=0, c=1;
+        for(int i=2; i<=n; i++){
+            a = b;
+            b = c;
+            c = (a+b)%1000'000'007;
+        }
+        return c;
+    }
+};
\ No newline at end of file
diff --git "a/questions/\351\235\242\350\257\225\351\242\230 17.14.\346\234\200\345\260\217K\344\270\252\346\225\260/main.cpp" "b/questions/\351\235\242\350\257\225\351\242\230 17.14.\346\234\200\345\260\217K\344\270\252\346\225\260/main.cpp"
new file mode 100644
index 0000000000000000000000000000000000000000..3ff19087ed6cbd30f918f6ab3aadbaeba9df347f
--- /dev/null
+++ "b/questions/\351\235\242\350\257\225\351\242\230 17.14.\346\234\200\345\260\217K\344\270\252\346\225\260/main.cpp"	
@@ -0,0 +1,19 @@
+#include "stl.h"
+#include "parse.h"
+#include "solution.h"
+int main(int argc, char *argv[])
+{
+    string str = "[[\"[1,3,5,7,2,4,6,8]\",\"4\"]]";
+    vector> arr = parseStringArrArr(str);
+    for (int i = 0; i < arr.size(); i++)
+    {
+      vector args = arr[i];
+      Solution *s = new Solution();
+      vector arg0 = parseIntegerArr(args[0]);
+      int arg1 = parseInteger(args[1]);
+      vector result=s->smallestK(arg0,arg1);
+      string resultabc =serializeIntegerArr(result);
+      cout << "resultabc"+to_string(i)+":" << resultabc <<"resultend"<< endl;
+    }
+    return 0;
+}
\ No newline at end of file
diff --git "a/questions/\351\235\242\350\257\225\351\242\230 17.14.\346\234\200\345\260\217K\344\270\252\346\225\260/solution.h" "b/questions/\351\235\242\350\257\225\351\242\230 17.14.\346\234\200\345\260\217K\344\270\252\346\225\260/solution.h"
new file mode 100644
index 0000000000000000000000000000000000000000..22872cc1649595d5a93888ea4351287fb96f4897
--- /dev/null
+++ "b/questions/\351\235\242\350\257\225\351\242\230 17.14.\346\234\200\345\260\217K\344\270\252\346\225\260/solution.h"	
@@ -0,0 +1,27 @@
+// @algorithm @lc id=1000021 lang=cpp 
+// @title smallest-k-lcci
+
+
+// #define print(...)
+// @test([1,3,5,7,2,4,6,8],4)=[1,2,3,4]
+class Solution {
+public:
+    vector smallestK(vector& arr, int k) {
+        vector ans;
+        if(!k) return ans;
+        priority_queue q; // 大顶堆
+        int i=0;
+        while(i arr[i]){
+                q.pop();
+                q.push(arr[i]);
+            }
+            i++;
+        }
+        while(!q.empty())
+            ans.push_back(q.top()), q.pop();
+        return ans;
+    }
+};
\ No newline at end of file