63 Commits

Author SHA1 Message Date
YDZ
cc1cc0ddbc Change to zdoc 2020-11-14 15:54:43 +08:00
YDZ
dd72560ce2 Fix head link 2020-11-14 15:49:46 +08:00
YDZ
8401264952 Update EN Desc 2020-11-13 16:54:22 +08:00
YDZ
ea38a91202 Add weekly-contest-214 solution 2020-11-10 08:34:52 +08:00
YDZ
f3888ff449 Add weekly-contest-214 2020-11-08 11:59:51 +08:00
YDZ
6d2472da5f micro fix 2020-10-18 15:38:31 +08:00
8b6dd34148 Merge pull request #76 from halfrost/add_solution_0785
Add solution 0785
2020-10-06 13:27:14 +08:00
YDZ
7ccb69a920 Add solution 0785 2020-10-06 13:26:32 +08:00
5f4219ca84 Merge pull request #74 from born69confused/patch-1
785.Is Graph Bipartite?
2020-10-06 11:08:05 +08:00
YDZ
f88b33b047 Update README 2020-10-06 10:53:15 +08:00
26a8308c72 Merge pull request #75 from tejasdobariya7/patch-1
Update README.md
2020-10-06 10:50:33 +08:00
59380b26ed Update README.md 2020-10-05 22:54:53 +05:30
e85635e6f9 785.Is Graph Bipartite?
Adding a new solution,
Leetcode Q.785 solution in Go lang using Depth First Search,
faster than 100 % ( 20ms ) with a memory usage of 12 MB.
2020-10-04 15:38:27 +05:30
YDZ
e6a965f767 Refresh goreportcard 2020-10-03 01:31:59 +08:00
YDZ
c39c65dfce Hidden goreportcard 2020-10-02 02:18:31 +08:00
YDZ
aa8d1801de Display goreportcard 2020-10-01 09:46:23 +08:00
YDZ
80a641546a Fix goreportcard 2020-10-01 09:42:46 +08:00
YDZ
2af7be3791 Fix problem 82 gofmt 2020-10-01 09:27:18 +08:00
YDZ
deb2aa1dc1 Change problem 1079 image 2020-09-30 14:22:17 +08:00
YDZ
3e7996d371 Fix go report card 2020-09-30 10:47:46 +08:00
c1f421273b Merge pull request #71 from harryleonardo/master
Add problem 1480 & 1512
2020-09-24 16:42:52 +08:00
44f8b5b8a6 [ADD] 1480 & 1512 2020-09-24 00:23:54 +07:00
YDZ
5830c8dbfc Badge add link 2020-09-21 00:37:45 +08:00
YDZ
e2a31f2a92 Add word count 2020-09-20 16:04:49 +08:00
YDZ
fb9da5f885 micro fix 2020-09-20 10:44:47 +08:00
adf980425c Merge pull request #70 from halfrost/fix_1300
Due to problem 1300 test case change, so solution should modify
2020-09-18 12:23:18 +08:00
YDZ
f56f6889bc Due to problem 1300 test case change, so solution should modify 2020-09-18 12:15:02 +08:00
3e301996c9 Merge pull request #69 from Janetyu/janetyu
leetcode82 提供更多的解法
2020-09-18 09:35:44 +08:00
8ab50ac930 leetcode82 提供更多的解法 2020-09-16 23:46:08 +08:00
YDZ
ec09c8d37b Problem 113 add solutions 2020-09-15 20:19:05 +08:00
f38c70a6c8 Merge pull request #68 from halfrost/add_qr_code
Add QR code
2020-09-10 17:14:54 +08:00
YDZ
2461c246fb Add QR code 2020-09-10 17:09:30 +08:00
b45568ba96 Merge pull request #67 from halfrost/fix_925
Due to problem 925 test case change, so solution should modify
2020-09-09 21:06:29 +08:00
YDZ
0e424f7105 Due to problem 925 test case change, so solution should modify 2020-09-09 20:51:49 +08:00
YDZ
e02e370d4e micro fix 2020-09-06 13:31:37 +08:00
YDZ
5fa44b9f6d remove go mod 2020-09-03 09:44:05 +08:00
b504737c19 Merge pull request #63 from lyricslee/bits
增加位运算
2020-09-03 09:25:55 +08:00
3158b6706d 增加位运算 2020-09-02 17:34:45 +08:00
2363839e94 Merge pull request #61 from xxgail/patch-1
Update 0017.Letter-Combinations-of-a-Phone-Number.md
2020-08-28 11:21:22 +08:00
04bcb27275 Merge branch 'master' into patch-1 2020-08-28 10:39:46 +08:00
46faa66642 Merge pull request #60 from halfrost/code_quality_improvement
optimization code quality level from A to A+
2020-08-27 17:41:27 +08:00
fa6c758004 Update 0017.Letter-Combinations-of-a-Phone-Number.md
Update 0017.Letter-Combinations-of-a-Phone-Number 参考回溯模板,增加回溯的解法(类似于DFS)
2020-08-27 15:37:41 +08:00
d2dfc2d634 Merge pull request #60 from halfrost/code_quality_improvement
optimization code quality level from A to A+
2020-08-27 13:21:57 +08:00
YDZ
96c36de45b optimization code level from A to A+ 2020-08-27 00:58:22 +08:00
7b7307761c Merge pull request #58 from halfrost/supplement_chapterone
完成第一章中少的 2 章节
2020-08-26 11:11:19 +08:00
YDZ
9531d78820 supplement ChapterOne 2020-08-26 03:13:48 +08:00
YDZ
db8c8979fd Problem0017 add solution two 2020-08-25 00:26:12 +08:00
YDZ
d7ed4c32cf Dark mode micro fix 2020-08-25 00:17:00 +08:00
YDZ
64be02e6b7 Update README 2020-08-24 12:03:09 +08:00
YDZ
30f483e769 HD image 2020-08-24 09:57:18 +08:00
YDZ
eb3c71bafa Delete public 2020-08-24 02:06:57 +08:00
3c9d679a53 Merge pull request #57 from halfrost/dark_theme
Add dark theme
2020-08-24 01:44:59 +08:00
YDZ
6f7e2e9582 Add dark theme 2020-08-24 01:35:54 +08:00
91c51977af Merge pull request #53 from kadefor/patch-1
精简代码
2020-08-23 08:25:01 +08:00
8075f0696f 精简代码 2020-08-23 00:00:42 +08:00
YDZ
5e6111e107 Add starchart 2020-08-22 08:19:39 +08:00
YDZ
e9dcc1e2fe Add PWA description 2020-08-21 04:22:17 +08:00
YDZ
bb1a101d38 fix iOS / Android PWA icon 2020-08-21 03:51:02 +08:00
YDZ
28e8d2a8e0 web add banner 2020-08-20 13:53:38 +08:00
YDZ
b64292975a Update README 2020-08-20 10:01:55 +08:00
bc7c289512 Merge pull request #52 from Janetyu/janetyu
finish 0724 solution
2020-08-20 08:06:38 +08:00
9df3022c4b Merge branch 'master' into janetyu 2020-08-19 23:18:52 +08:00
aa5ef0b9ac finish 724 solution 2020-08-19 23:12:50 +08:00
1931 changed files with 18351 additions and 59658 deletions

14
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"go.formatTool": "gofmt",
"go.formatFlags": [
"-s"
],
"go.autocompleteUnimportedPackages": true,
"[go]": {
"editor.insertSpaces": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}

128
README.md
View File

@ -1,6 +1,6 @@
# LeetCode in Go
[LeetCode Online Judge](https://leetcode.com/) is a website containing many **algorithm questions**. Most of them are real interview questions of **Google, Facebook, LinkedIn, Apple**, etc. This repo shows my solutions in Go with the code style strictly follows the [Google Golang Style Guide](https://github.com/golang/go/wiki/CodeReviewComments). Please feel free to reference and **STAR** to support this repo, thank you!
[LeetCode Online Judge](https://leetcode.com/) is a website containing many **algorithm questions**. Most of them are real interview questions of **Google, Facebook, LinkedIn, Apple**, etc. and it always help to sharp our algorithm Skills. Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. This repo shows my solutions in Go with the code style strictly follows the [Google Golang Style Guide](https://github.com/golang/go/wiki/CodeReviewComments). Please feel free to reference and **STAR** to support this repo, thank you!
<p align='center'>
@ -8,35 +8,52 @@
</p>
<p align='center'>
<a href="https://github.com/halfrost/LeetCode-Go/releases/" rel="nofollow"><img alt="GitHub All Releases" src="https://img.shields.io/github/downloads/halfrost/LeetCode-Go/total?label=PDF%20downloads"></a>
<img src="https://img.shields.io/badge/Total%20Word%20Count-738884-success">
<a href="https://github.com/halfrost/LeetCode-Go/actions" rel="nofollow"><img src="https://github.com/halfrost/LeetCode-Go/workflows/Deploy%20leetcode-cookbook/badge.svg?branch=master"></a>
<a href="https://travis-ci.org/github/halfrost/LeetCode-Go" rel="nofollow"><img src="https://travis-ci.org/halfrost/LeetCode-Go.svg?branch=master"></a>
<a href="https://goreportcard.com/report/github.com/halfrost/LeetCode-Go" rel="nofollow"><img src="https://goreportcard.com/badge/github.com/halfrost/LeetCode-Go"></a>
<img src="https://img.shields.io/badge/runtime%20beats-100%25-success">
<!--<img alt="GitHub go.mod Go version" src="https://img.shields.io/github/go-mod/go-version/halfrost/LeetCode-Go?color=26C2F0">-->
<img alt="Support Go version" src="https://img.shields.io/badge/Go-v1.15-26C2F0">
<img src="https://visitor-badge.laobi.icu/badge?page_id=halfrost.LeetCode-Go">
</p>
<p align='center'>
<a href="https://github.com/halfrost/LeetCode-Go/blob/master/LICENSE"><img alt="GitHub" src="https://img.shields.io/github/license/halfrost/LeetCode-Go?label=License"></a>
<img src="https://img.shields.io/badge/License-CC-000000.svg">
<a href="https://leetcode.com/halfrost/"><img src="https://img.shields.io/badge/@halfrost-8751-yellow.svg">
<img src="https://img.shields.io/badge/language-Golang-abcdef.svg">
<img src="https://img.shields.io/badge/language-Golang-26C2F0.svg">
<a href="https://halfrost.com"><img src="https://img.shields.io/badge/Blog-Halfrost--Field-80d4f9.svg?style=flat"></a>
<a href="http://weibo.com/halfrost"><img src="https://img.shields.io/badge/weibo-@halfrost-f974ce.svg?style=flat&colorA=f4292e"></a>
<a href="https://twitter.com/halffrost"><img src="https://img.shields.io/badge/twitter-@halfrost-F8E81C.svg?style=flat&colorA=009df2"></a>
<a href="http://www.jianshu.com/u/12201cdd5d7a"><img src="https://img.shields.io/badge/%E7%AE%80%E4%B9%A6-@%E4%B8%80%E7%BC%95%E6%AE%87%E6%B5%81%E5%8C%96%E9%9A%90%E5%8D%8A%E8%BE%B9%E5%86%B0%E9%9C%9C-b561fe.svg?style=flat&colorA=ed6f59"></a>
<a href="https://juejin.im/user/5692e1e000b09aa2e8dcd422"><img src="https://img.shields.io/badge/%E6%8E%98%E9%87%91-@%E4%B8%80%E7%BC%95%E6%AE%87%E6%B5%81%E5%8C%96%E9%9A%90%E5%8D%8A%E8%BE%B9%E5%86%B0%E9%9C%9C-fd6f32.svg?style=flat&colorA=1970fe"></a>
<a href="https://www.zhihu.com/people/halfrost/activities"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%8E-@halfrost-50E3C2.svg?style=flat&colorA=0083ea"></a>
<img src="https://img.shields.io/badge/PR-welcome%20!-brightgreen.svg?colorA=a0cd34">
<a href=""><img src="https://img.shields.io/badge/license-CC-000000.svg"></a>
<a href="https://twitter.com/halffrost"><img src="https://img.shields.io/badge/twitter-@halffrost-F8E81C.svg?style=flat&colorA=009df2"></a>
<a href="https://www.zhihu.com/people/halfrost/activities"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%8E-@halfrost-fd6f32.svg?style=flat&colorA=0083ea"></a>
<img src="https://img.shields.io/badge/made%20with-=1-blue.svg">
<img src="https://visitor-badge.laobi.icu/badge?page_id=halfrost.LeetCode-Go">
<img src="https://github.com/halfrost/LeetCode-Go/workflows/Deploy%20leetcode-cookbook/badge.svg?branch=master">
<img src="https://travis-ci.org/halfrost/LeetCode-Go.svg?branch=master">
<a href="https://github.com/halfrost/LeetCode-Go/pulls"><img src="https://img.shields.io/badge/PR-Welcome-brightgreen.svg"></a>
</p>
支持 Progressive Web Apps 和 Dark Mode 的题解电子书《LeetCode Cookbook》 <a href="https://books.halfrost.com/leetcode/" rel="nofollow">Online Reading</a>
<p align='center'>
支持 Progressive Web Apps 的题解电子书《LeetCode Cookbook》 <a href="https://books.halfrost.com/leetcode/" rel="nofollow">Online Reading</a>
<a href="https://books.halfrost.com/leetcode/"><img src="https://img.halfrost.com/Leetcode/Cookbook_Safari_0.png"></a>
<a href="https://books.halfrost.com/leetcode/"><img src="https://img.halfrost.com/Leetcode/Cookbook_Chrome_PWA.png"></a>
</p>
<p align='center'>
离线版本的电子书《LeetCode Cookbook》PDF <a href="https://github.com/halfrost/LeetCode-Go/releases/" rel="nofollow">Download here</a>
</p>
<p align='center'>
<a href="https://github.com/halfrost/LeetCode-Go/releases/"><img src="https://img.halfrost.com/Leetcode/Cookbook.png"></a>
</p>
通过 iOS / Android 浏览器安装 PWA 版《LeetCode Cookbook》至设备桌面随时学习
<p align='center'>
<a href="https://books.halfrost.com/leetcode/"><img src="https://img.halfrost.com/Leetcode/Cookbook_PWA_iPad.png"></a>
<a href="https://books.halfrost.com/leetcode/"><img src="https://img.halfrost.com/Leetcode/Cookbook_PWA_iPad_example1__.png"></a>
<a href="https://books.halfrost.com/leetcode/"><img src="https://img.halfrost.com/Leetcode/Cookbook_PWA_iPad_example2__.png"></a>
</p>
## Data Structures
> 标识了 ✅ 的专题是完成所有题目了的,没有标识的是还没有做完所有题目的
@ -45,39 +62,59 @@
* [Array](#array)
* [String](#string)
* [Two Pointers](#two-pointers)
* [Linked List](#linked-list)
* [Stack](#stack)
* [Two Pointers](#two-pointers)
* [Linked List](#linked-list)
* [Stack](#stack)
* [Tree](#tree)
* [Dynamic programming](#dynamic-programming)
* [Backtracking](#backtracking)
* [Backtracking](#backtracking)
* [Depth First Search](#depth-first-search)
* [Breadth First Search](#breadth-first-search)
* [Binary Search](#binary-search)
* [Math](#math)
* [Hash Table](#hash-table)
* [Sort](#sort)
* [Bit Manipulation](#bit-manipulation)
* [Union Find](#union-find)
* [Sliding Window](#sliding-window)
* [Segment Tree](#segment-tree)
* [Binary Indexed Tree](#binary-indexed-tree)
* [Sort](#sort)
* [Bit Manipulation](#bit-manipulation)
* [Union Find](#union-find)
* [Sliding Window](#sliding-window)
* [Segment Tree](#segment-tree)
* [Binary Indexed Tree](#binary-indexed-tree)
| 数据结构 | 变种 | 相关题目 |
|:-------:|:-------|:------|
|顺序线性表:向量|||
|单链表|1.链表<br>2.静态链表<br>3.对称矩阵<br>4.稀疏矩阵||
|栈|广义栈||
|队列|1.链表实现<br>2.循环数组实现<br>3.双端队列||
|字符串|1.KMP算法<br>2.有限状态自动机<br>3.模式匹配有限状态自动机<br>4.BM模式匹配算法<br>5.BM-KMP算法||
|树|1.二叉树<br>2.并查集<br>3.Huffman数||
|数组实现的堆|1.极大堆和极小堆<br>2.极大极小堆<br>3.双端堆<br>4.d叉堆||
|实现的堆|1.<br>2.<br>3.二项式<br>4.斐波那契堆<br>5.配对堆||
|查找|1.哈希表<br>2.跳跃表<br>3.排序二叉树<br>4.AVL树<br>5.B树<br>6.AA树<br>7.红黑树<br>8.排序二叉堆<br>9.Splay树<br>10.双链树<br>11.Trie树||
| 数据结构 | 变种 | 相关题目 | 讲解文章 |
|:-------:|:-------|:------|:------|
|顺序线性表:向量||||
|单链表|1. 双向链表<br>2. 静态链表<br>3. 对称矩阵<br>4. 稀疏矩阵|||
|哈希表|1. 散列函数<br>2. 解决碰撞/填充因子<br>|||
|栈和队列|1. 广义栈<br>2. 双端队列<br>|||
|队列|1. 链表实现<br>2. 循环数组实现<br>3. 双端队列|||
|字符串|1. KMP算法<br>2. 有限状态自动机<br>3. 模式匹配有限状态自动机<br>4. BM 模式匹配算法<br>5. BM-KMP 算法<br>6. BF 算法|||
|树|1. 二叉树<br>2. 并查集<br>3. Huffman 树|||
|数组实现的堆|1. 极大堆和极小<br>2. 极大极小<br>3. 双端<br>4. d 叉堆|||
|树实现的堆|1. 左堆<br>2. 扁堆<br>3. 二项式堆<br>4. 斐波那契堆<br>5. 配对堆|||
|查找|1. 哈希表<br>2. 跳跃表<br>3. 排序二叉树<br>4. AVL 树<br>5. B 树 / B+ 树 / B* 树<br>6. AA 树<br>7. 红黑树<br>8. 排序二叉堆<br>9. Splay 树<br>10. 双链树<br>11. Trie 树<br>12. R 树|||
|--------------------------------------------|--------------------------------------------------------------------------------------------|---------------------------|-----------------------------------|
## Algorithm
## 算法
| 算法 | 具体类型 | 相关题目 | 讲解文章 |
|:-------:|:-------|:------|:------|
|排序算法|1. 冒泡排序<br>2. 插入排序<br>3. 选择排序<br>4. 希尔 Shell 排序<br>5. 快速排序<br>6. 归并排序<br>7. 堆排序<br>8. 线性排序算法<br>9. 自省排序<br>10. 间接排序<br>11. 计数排序<br>12. 基数排序<br>13. 桶排序<br>14. 外部排序 - k 路归并败者树<br>15. 外部排序 - 最佳归并树|||
|递归与分治||1. 二分搜索/查找<br>2. 大整数的乘法<br>3. Strassen 矩阵乘法<br>4. 棋盘覆盖<br>5. 合并排序<br>6. 快速排序<br>7. 线性时间选择<br>8. 最接近点对问题<br>9. 循环赛日程表<br>||
|动态规划||1. 矩阵连乘问题<br>2. 最长公共子序列<br>3. 最大子段和<br>4. 凸多边形最优三角剖分<br>5. 多边形游戏<br>6. 图像压缩<br>7. 电路布线<br>8. 流水作业调度<br>9. 0-1 背包问题/背包九讲<br>10. 最优二叉搜索树<br>11. 动态规划加速原理<br>12. 树型 DP<br>||
|贪心||1. 活动安排问题<br>2. 最优装载<br>3. 哈夫曼编码<br>4. 单源最短路径<br>5. 最小生成树<br>6. 多机调度问题<br>||
|回溯法||1. 装载问题<br>2. 批处理作业调度<br>3. 符号三角形问题<br>4. n 后问题<br>5. 0-1 背包问题<br>6. 最大团问题<br>7. 图的 m 着色问题<br>8. 旅行售货员问题<br>9. 圆排列问题<br>10. 电路板排列问题<br>11. 连续邮资问题<br>||
|搜索|1. 枚举<br>2. DFS<br>3. BFS<br>4. 启发式搜索<br>|||
|随机化|1. 随机数<br>2. 数值随机化算法<br>3. Sherwood 舍伍德算法<br>4. Las Vegas 拉斯维加斯算法<br>5. Monte Carlo 蒙特卡罗算法<br>|1. 计算 π 值<br>2. 计算定积分<br>3. 解非线性方程组<br>4. 线性时间选择算法<br>5. 跳跃表<br>6. n 后问题<br>7. 整数因子分解<br>8. 主元素问题<br>9. 素数测试<br>||
|图论|1. 遍历 DFS / BFS<br>2. AOV / AOE 网络<br>3. Kruskal 算法(最小生成树)<br>4. Prim 算法(最小生成树)<br>5. Boruvka 算法(最小生成树)<br>6. Dijkstra 算法(单源最短路径)<br>7. Bellman-Ford 算法(单源最短路径)<br>8. SPFA 算法(单源最短路径)<br>9. Floyd 算法(多源最短路径)<br>10. Johnson 算法(多源最短路径)<br>11. Fleury 算法(欧拉回路)<br>12. Ford-Fulkerson 算法(最大网络流增广路)<br>13. Edmonds-Karp 算法(最大网络流)<br>14. Dinic 算法(最大网络流)<br>15. 一般预流推进算法<br>16. 最高标号预流推进 HLPP 算法<br>17. Primal-Dual 原始对偶算法(最小费用流)18. Kosaraju 算法(有向图强连通分量)<br>19. Tarjan 算法(有向图强连通分量)<br>20. Gabow 算法(有向图强连通分量)<br>21. 匈牙利算法(二分图匹配)<br>22. HopcroftKarp 算法(二分图匹配)<br>23. kuhn munkras 算法(二分图最佳匹配)<br>24. Edmonds Blossom-Contraction 算法(一般图匹配)<br>|1. 图遍历<br>2. 有向图和无向图的强弱连通性<br>3. 割点/割边<br>3. AOV 网络和拓扑排序<br>4. AOE 网络和关键路径<br>5. 最小代价生成树/次小生成树<br>6. 最短路径问题/第 K 短路问题<br>7. 最大网络流问题<br>8. 最小费用流问题<br>9. 图着色问题<br>10. 差分约束系统<br>11. 欧拉回路<br>12. 中国邮递员问题<br>13. 汉密尔顿回路<br>14. 最佳边割集/最佳点割集/最小边割集/最小点割集/最小路径覆盖/最小点集覆盖 <br>15. 边覆盖集<br>16. 二分图完美匹配和最大匹配问题<br>17. 仙人掌图<br>18. 弦图<br>19. 稳定婚姻问题<br>20. 最大团问题<br>||
|数论||1. 最大公约数<br> 2. 最小公倍数<br>3. 分解质因数<br>4. 素数判定<br>5. 进制转换<br>6. 高精度计算<br>7. 整除问题<br>8. 同余问题<br>9. 欧拉函数<br>10. 扩展欧几里得<br>11. 置换群<br>12. 母函数<br>13. 离散变换<br>14. 康托展开<br>15. 矩阵<br>16. 向量<br>17. 线性方程组<br>18. 线性规划<br> ||
|几何||1. 凸包 - Gift wrapping<br>2. 凸包 - Graham scan<br>3. 线段问题<br> 4. 多边形和多面体相关问题<br>||
|NP 完全|1. 计算模型<br>2. P 类与 NP 类问题<br>3. NP 完全问题<br>4. NP 完全问题的近似算法<br>|1. 随机存取机 RAM<br>2. 随机存取存储程序机 RASP<br>3. 图灵机<br>4. 非确定性图灵机<br>5. P 类与 NP 类语言<br>6. 多项式时间验证<br>7. 多项式时间变换<br>8. Cook定理<br>9. 合取范式的可满足性问题 CNF-SAT<br>10. 3 元合取范式的可满足性问题 3-SAT<br>11. 团问题 CLIQUE<br>12. 顶点覆盖问题 VERTEX-COVER<br>13. 子集和问题 SUBSET-SUM<br>14. 哈密顿回路问题 HAM-CYCLE<br>15. 旅行售货员问题 TSP<br>16. 顶点覆盖问题的近似算法<br>17. 旅行售货员问题近似算法<br>18. 具有三角不等式性质的旅行售货员问题<br>19. 一般的旅行售货员问题<br>20. 集合覆盖问题的近似算法<br>21. 子集和问题的近似算法<br>22. 子集和问题的指数时间算法<br>23. 子集和问题的多项式时间近似格式<br>||
|------------|------------------------------------------------------------------|-----------------------------------------------------------------|--------------------|
## LeetCode Problems
## 一. 目录
@ -2083,7 +2120,7 @@ Problems List in [there](https://books.halfrost.com/leetcode/ChapterTwo/Sliding_
![](./topic/Segment_Tree.png)
- 线段的经典数组实现写法。将合并两个节点 pushUp 逻辑抽象出来了,可以实现任意操作(常见的操作有:加法,取 maxmin 等等)。第 218 题,第 303 题,第 307 题,第 699 题。
- 线段的经典数组实现写法。将合并两个节点 pushUp 逻辑抽象出来了,可以实现任意操作(常见的操作有:加法,取 maxmin 等等)。第 218 题,第 303 题,第 307 题,第 699 题。
- 计数线段树的经典写法。第 315 题,第 327 题,第 493 题。
- 线段树的树的实现写法。第 715 题,第 732 题。
- 区间懒惰更新。第 218 题,第 699 题。
@ -2120,3 +2157,18 @@ Problems List in [there](https://books.halfrost.com/leetcode/ChapterTwo/Binary_I
----------------------------------------------------------------------------------------
<p align='center'>
<a href="https://github.com/halfrost/LeetCode-Go/releases/tag/Special"><img src="https://img.halfrost.com/Leetcode/ACM-ICPC_Algorithm_Template.png"></a>
</p>
Thank you for reading here. This is bonus. You can download my [《ACM-ICPC Algorithm Template》](https://github.com/halfrost/LeetCode-Go/releases/tag/Special/)
## ♥️ Thanks
Thanks for your Star
[![Stargazers over time](https://starchart.cc/halfrost/LeetCode-Go.svg)](https://starchart.cc/halfrost/LeetCode-Go)

View File

@ -26,32 +26,30 @@ type ans1 struct {
func Test_Problem1(t *testing.T) {
qs := []question1{
question1{
{
para1{[]int{3, 2, 4}, 6},
ans1{[]int{1, 2}},
},
question1{
{
para1{[]int{3, 2, 4}, 5},
ans1{[]int{0, 1}},
},
question1{
{
para1{[]int{0, 8, 7, 3, 3, 4, 2}, 11},
ans1{[]int{1, 3}},
},
question1{
{
para1{[]int{0, 1}, 1},
ans1{[]int{0, 1}},
},
question1{
{
para1{[]int{0, 3}, 5},
ans1{[]int{}},
},
// 如需多个测试,可以复制上方元素。
}

View File

@ -16,36 +16,24 @@ type ListNode = structures.ListNode
*/
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil || l2 == nil {
return nil
}
head := &ListNode{Val: 0, Next: nil}
current := head
carry := 0
for l1 != nil || l2 != nil {
var x, y int
head := &ListNode{Val: 0}
n1, n2, carry, current := 0, 0, 0, head
for l1 != nil || l2 != nil || carry != 0 {
if l1 == nil {
x = 0
n1 = 0
} else {
x = l1.Val
}
if l2 == nil {
y = 0
} else {
y = l2.Val
}
current.Next = &ListNode{Val: (x + y + carry) % 10, Next: nil}
current = current.Next
carry = (x + y + carry) / 10
if l1 != nil {
n1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
if l2 == nil {
n2 = 0
} else {
n2 = l2.Val
l2 = l2.Next
}
}
if carry > 0 {
current.Next = &ListNode{Val: carry % 10, Next: nil}
current.Next = &ListNode{Val: (n1 + n2 + carry) % 10}
current = current.Next
carry = (n1 + n2 + carry) / 10
}
return head.Next
}

View File

@ -29,42 +29,42 @@ func Test_Problem2(t *testing.T) {
qs := []question2{
question2{
{
para2{[]int{}, []int{}},
ans2{[]int{}},
},
question2{
{
para2{[]int{1}, []int{1}},
ans2{[]int{2}},
},
question2{
{
para2{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
ans2{[]int{2, 4, 6, 8}},
},
question2{
{
para2{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
ans2{[]int{2, 4, 6, 8, 0, 1}},
},
question2{
{
para2{[]int{1}, []int{9, 9, 9, 9, 9}},
ans2{[]int{0, 0, 0, 0, 0, 1}},
},
question2{
{
para2{[]int{9, 9, 9, 9, 9}, []int{1}},
ans2{[]int{0, 0, 0, 0, 0, 1}},
},
question2{
{
para2{[]int{2, 4, 3}, []int{5, 6, 4}},
ans2{[]int{7, 0, 8}},
},
question2{
{
para2{[]int{1, 8, 3}, []int{7, 1}},
ans2{[]int{8, 9, 3}},
},

View File

@ -26,22 +26,22 @@ func Test_Problem3(t *testing.T) {
qs := []question3{
question3{
{
para3{"abcabcbb"},
ans3{3},
},
question3{
{
para3{"bbbbb"},
ans3{1},
},
question3{
{
para3{"pwwkew"},
ans3{3},
},
question3{
{
para3{""},
ans3{0},
},

View File

@ -27,12 +27,12 @@ func Test_Problem4(t *testing.T) {
qs := []question4{
question4{
{
para4{[]int{1, 3}, []int{2}},
ans4{2.0},
},
question4{
{
para4{[]int{1, 2}, []int{3, 4}},
ans4{2.5},
},

View File

@ -26,22 +26,22 @@ func Test_Problem7(t *testing.T) {
qs := []question7{
question7{
{
para7{321},
ans7{123},
},
question7{
{
para7{-123},
ans7{-321},
},
question7{
{
para7{120},
ans7{21},
},
question7{
{
para7{1534236469},
ans7{0},
},

View File

@ -26,37 +26,37 @@ func Test_Problem9(t *testing.T) {
qs := []question9{
question9{
{
para9{121},
ans9{true},
},
question9{
{
para9{-121},
ans9{false},
},
question9{
{
para9{10},
ans9{false},
},
question9{
{
para9{321},
ans9{false},
},
question9{
{
para9{-123},
ans9{false},
},
question9{
{
para9{120},
ans9{false},
},
question9{
{
para9{1534236469},
ans9{false},
},

View File

@ -26,12 +26,12 @@ func Test_Problem11(t *testing.T) {
qs := []question11{
question11{
{
para11{[]int{1, 8, 6, 2, 5, 4, 8, 3, 7}},
ans11{49},
},
question11{
{
para11{[]int{1, 1}},
ans11{1},
},

View File

@ -26,32 +26,32 @@ func Test_Problem13(t *testing.T) {
qs := []question13{
question13{
{
para13{"III"},
ans13{3},
},
question13{
{
para13{"IV"},
ans13{4},
},
question13{
{
para13{"IX"},
ans13{9},
},
question13{
{
para13{"LVIII"},
ans13{58},
},
question13{
{
para13{"MCMXCIV"},
ans13{1994},
},
question13{
{
para13{"MCMXICIVI"},
ans13{2014},
},

View File

@ -26,26 +26,26 @@ func Test_Problem15(t *testing.T) {
qs := []question15{
question15{
{
para15{[]int{0, 0, 0}},
ans15{[][]int{[]int{0, 0, 0}}},
ans15{[][]int{{0, 0, 0}}},
},
question15{
{
para15{[]int{-1, 0, 1, 2, -1, -4}},
ans15{[][]int{[]int{-1, 0, 1}, []int{-1, -1, 2}}},
ans15{[][]int{{-1, 0, 1}, {-1, -1, 2}}},
},
question15{
{
para15{[]int{-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6}},
ans15{[][]int{[]int{-4, -2, 6}, []int{-4, 0, 4}, []int{-4, 1, 3}, []int{-4, 2, 2}, []int{-2, -2, 4}, []int{-2, 0, 2}}},
ans15{[][]int{{-4, -2, 6}, {-4, 0, 4}, {-4, 1, 3}, {-4, 2, 2}, {-2, -2, 4}, {-2, 0, 2}}},
},
question15{
{
para15{[]int{5, -7, 3, -3, 5, -10, 4, 8, -3, -8, -3, -3, -1, -8, 6, 4, -4, 7, 2, -5, -2, -7, -3, 7, 2, 4, -6, 5}},
ans15{[][]int{[]int{-10, 2, 8}, []int{-10, 3, 7}, []int{-10, 4, 6}, []int{-10, 5, 5}, []int{-8, 2, 6}, []int{-8, 3, 5}, []int{-8, 4, 4}, []int{-7, -1, 8},
[]int{-7, 2, 5}, []int{-7, 3, 4}, []int{-6, -2, 8}, []int{-6, -1, 7}, []int{-6, 2, 4}, []int{-5, -3, 8}, []int{-5, -2, 7}, []int{-5, -1, 6}, []int{-5, 2, 3},
[]int{-4, -3, 7}, []int{-4, -2, 6}, []int{-4, -1, 5}, []int{-4, 2, 2}, []int{-3, -3, 6}, []int{-3, -2, 5}, []int{-3, -1, 4}, []int{-2, -1, 3}}},
ans15{[][]int{{-10, 2, 8}, {-10, 3, 7}, {-10, 4, 6}, {-10, 5, 5}, {-8, 2, 6}, {-8, 3, 5}, {-8, 4, 4}, {-7, -1, 8},
{-7, 2, 5}, {-7, 3, 4}, {-6, -2, 8}, {-6, -1, 7}, {-6, 2, 4}, {-5, -3, 8}, {-5, -2, 7}, {-5, -1, 6}, {-5, 2, 3},
{-4, -3, 7}, {-4, -2, 6}, {-4, -1, 5}, {-4, 2, 2}, {-3, -3, 6}, {-3, -2, 5}, {-3, -1, 4}, {-2, -1, 3}}},
},
}

View File

@ -27,27 +27,27 @@ func Test_Problem16(t *testing.T) {
qs := []question16{
question16{
{
para16{[]int{-1, 0, 1, 1, 55}, 3},
ans16{2},
},
question16{
{
para16{[]int{0, 0, 0}, 1},
ans16{0},
},
question16{
{
para16{[]int{-1, 2, 1, -4}, 1},
ans16{2},
},
question16{
{
para16{[]int{1, 1, -1}, 0},
ans16{1},
},
question16{
{
para16{[]int{-1, 2, 1, -4}, 1},
ans16{2},
},

View File

@ -13,9 +13,11 @@ var (
"tuv", //8
"wxyz", //9
}
res = []string{}
res = []string{}
final = 0
)
// 解法一 DFS
func letterCombinations(digits string) []string {
if digits == "" {
return []string{}
@ -37,3 +39,66 @@ func findCombination(digits *string, index int, s string) {
}
return
}
// 解法二 非递归
func letterCombinations_(digits string) []string {
if digits == "" {
return []string{}
}
index := digits[0] - '0'
letter := letterMap[index]
tmp := []string{}
for i := 0; i < len(letter); i++ {
if len(res) == 0 {
res = append(res, "")
}
for j := 0; j < len(res); j++ {
tmp = append(tmp, res[j]+string(letter[i]))
}
}
res = tmp
final++
letterCombinations(digits[1:])
final--
if final == 0 {
tmp = res
res = []string{}
}
return tmp
}
// 解法三 回溯参考回溯模板类似DFS
var result []string
var dict = map[string][]string{
"2": {"a", "b", "c"},
"3": {"d", "e", "f"},
"4": {"g", "h", "i"},
"5": {"j", "k", "l"},
"6": {"m", "n", "o"},
"7": {"p", "q", "r", "s"},
"8": {"t", "u", "v"},
"9": {"w", "x", "y", "z"},
}
func letterCombinationsBT(digits string) []string {
result = []string{}
if digits == "" {
return result
}
letterFunc("", digits)
return result
}
func letterFunc(res string, digits string) {
if digits == "" {
result = append(result, res)
return
}
k := digits[0:1]
digits = digits[1:]
for i := 0; i < len(dict[k]); i++ {
res += dict[k][i]
letterFunc(res, digits)
res = res[0 : len(res)-1]
}
}

View File

@ -26,7 +26,7 @@ func Test_Problem17(t *testing.T) {
qs := []question17{
question17{
{
para17{"23"},
ans17{[]string{"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"}},
},

View File

@ -27,29 +27,29 @@ func Test_Problem18(t *testing.T) {
qs := []question18{
question18{
{
para18{[]int{1, 1, 1, 1}, 4},
ans18{[][]int{[]int{1, 1, 1, 1}}},
ans18{[][]int{{1, 1, 1, 1}}},
},
question18{
{
para18{[]int{0, 1, 5, 0, 1, 5, 5, -4}, 11},
ans18{[][]int{[]int{-4, 5, 5, 5}, []int{0, 1, 5, 5}}},
ans18{[][]int{{-4, 5, 5, 5}, {0, 1, 5, 5}}},
},
question18{
{
para18{[]int{1, 0, -1, 0, -2, 2}, 0},
ans18{[][]int{[]int{-1, 0, 0, 1}, []int{-2, -1, 1, 2}, []int{-2, 0, 0, 2}}},
ans18{[][]int{{-1, 0, 0, 1}, {-2, -1, 1, 2}, {-2, 0, 0, 2}}},
},
question18{
{
para18{[]int{1, 0, -1, 0, -2, 2, 0, 0, 0, 0}, 0},
ans18{[][]int{[]int{-1, 0, 0, 1}, []int{-2, -1, 1, 2}, []int{-2, 0, 0, 2}, []int{0, 0, 0, 0}}},
ans18{[][]int{{-1, 0, 0, 1}, {-2, -1, 1, 2}, {-2, 0, 0, 2}, {0, 0, 0, 0}}},
},
question18{
{
para18{[]int{1, 0, -1, 0, -2, 2, 0, 0, 0, 0}, 1},
ans18{[][]int{[]int{-1, 0, 0, 2}, []int{-2, 0, 1, 2}, []int{0, 0, 0, 1}}},
ans18{[][]int{{-1, 0, 0, 2}, {-2, 0, 1, 2}, {0, 0, 0, 1}}},
},
}

View File

@ -29,26 +29,26 @@ func Test_Problem19(t *testing.T) {
qs := []question19{
question19{
{
para19{[]int{1, 2, 3, 4, 5}, 1},
ans19{[]int{1, 2, 3, 4}},
},
question19{
{
para19{[]int{1, 2, 3, 4, 5}, 2},
ans19{[]int{1, 2, 3, 5}},
},
question19{
{
para19{[]int{1, 2, 3, 4, 5}, 3},
ans19{[]int{1, 2, 4, 5}},
},
question19{
{
para19{[]int{1, 2, 3, 4, 5}, 4},
ans19{[]int{1, 3, 4, 5}},
},
question19{
{
para19{[]int{1, 2, 3, 4, 5}, 5},
ans19{[]int{2, 3, 4, 5}},
},

View File

@ -26,35 +26,35 @@ func Test_Problem20(t *testing.T) {
qs := []question20{
question20{
{
para20{"()[]{}"},
ans20{true},
},
question20{
{
para20{"(]"},
ans20{false},
},
question20{
{
para20{"({[]})"},
ans20{true},
},
question20{
{
para20{"(){[({[]})]}"},
ans20{true},
},
question20{
{
para20{"((([[[{{{"},
ans20{false},
},
question20{
{
para20{"(())]]"},
ans20{false},
},
question20{
{
para20{""},
ans20{true},
},
question20{
{
para20{"["},
ans20{false},
},

View File

@ -29,42 +29,42 @@ func Test_Problem21(t *testing.T) {
qs := []question21{
question21{
{
para21{[]int{}, []int{}},
ans21{[]int{}},
},
question21{
{
para21{[]int{1}, []int{1}},
ans21{[]int{1, 1}},
},
question21{
{
para21{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
ans21{[]int{1, 1, 2, 2, 3, 3, 4, 4}},
},
question21{
{
para21{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
ans21{[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}},
},
question21{
{
para21{[]int{1}, []int{9, 9, 9, 9, 9}},
ans21{[]int{1, 9, 9, 9, 9, 9}},
},
question21{
{
para21{[]int{9, 9, 9, 9, 9}, []int{1}},
ans21{[]int{1, 9, 9, 9, 9, 9}},
},
question21{
{
para21{[]int{2, 3, 4}, []int{4, 5, 6}},
ans21{[]int{2, 3, 4, 4, 5, 6}},
},
question21{
{
para21{[]int{1, 3, 8}, []int{1, 7}},
ans21{[]int{1, 1, 3, 7, 8}},
},

View File

@ -26,7 +26,7 @@ func Test_Problem22(t *testing.T) {
qs := []question22{
question22{
{
para22{3},
ans22{[]string{
"((()))",

View File

@ -28,63 +28,63 @@ func Test_Problem23(t *testing.T) {
qs := []question23{
question23{
{
para23{[][]int{}},
ans23{[]int{}},
},
question23{
{
para23{[][]int{
[]int{1},
[]int{1},
{1},
{1},
}},
ans23{[]int{1, 1}},
},
question23{
{
para23{[][]int{
[]int{1, 2, 3, 4},
[]int{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
}},
ans23{[]int{1, 1, 2, 2, 3, 3, 4, 4}},
},
question23{
{
para23{[][]int{
[]int{1, 2, 3, 4, 5},
[]int{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5},
}},
ans23{[]int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}},
},
question23{
{
para23{[][]int{
[]int{1},
[]int{9, 9, 9, 9, 9},
{1},
{9, 9, 9, 9, 9},
}},
ans23{[]int{1, 9, 9, 9, 9, 9}},
},
question23{
{
para23{[][]int{
[]int{9, 9, 9, 9, 9},
[]int{1},
{9, 9, 9, 9, 9},
{1},
}},
ans23{[]int{1, 9, 9, 9, 9, 9}},
},
question23{
{
para23{[][]int{
[]int{2, 3, 4},
[]int{4, 5, 6},
{2, 3, 4},
{4, 5, 6},
}},
ans23{[]int{2, 3, 4, 4, 5, 6}},
},
question23{
{
para23{[][]int{
[]int{1, 3, 8},
[]int{1, 7},
{1, 3, 8},
{1, 7},
}},
ans23{[]int{1, 1, 3, 7, 8}},
},

View File

@ -28,22 +28,22 @@ func Test_Problem24(t *testing.T) {
qs := []question24{
question24{
{
para24{[]int{}},
ans24{[]int{}},
},
question24{
{
para24{[]int{1}},
ans24{[]int{1}},
},
question24{
{
para24{[]int{1, 2, 3, 4}},
ans24{[]int{2, 1, 4, 3}},
},
question24{
{
para24{[]int{1, 2, 3, 4, 5}},
ans24{[]int{2, 1, 4, 3, 5}},
},

View File

@ -29,7 +29,7 @@ func Test_Problem25(t *testing.T) {
qs := []question25{
question25{
{
para25{
[]int{1, 2, 3, 4, 5},
3,
@ -37,7 +37,7 @@ func Test_Problem25(t *testing.T) {
ans25{[]int{3, 2, 1, 4, 5}},
},
question25{
{
para25{
[]int{1, 2, 3, 4, 5},
1,

View File

@ -26,22 +26,22 @@ func Test_Problem26(t *testing.T) {
qs := []question26{
question26{
{
para26{[]int{1, 1, 2}},
ans26{2},
},
question26{
{
para26{[]int{0, 0, 1, 1, 1, 1, 2, 3, 4, 4}},
ans26{5},
},
question26{
{
para26{[]int{0, 0, 0, 0, 0}},
ans26{1},
},
question26{
{
para26{[]int{1}},
ans26{1},
},

View File

@ -27,32 +27,32 @@ func Test_Problem27(t *testing.T) {
qs := []question27{
question27{
{
para27{[]int{1, 0, 1}, 1},
ans27{1},
},
question27{
{
para27{[]int{0, 1, 0, 3, 0, 12}, 0},
ans27{3},
},
question27{
{
para27{[]int{0, 1, 0, 3, 0, 0, 0, 0, 1, 12}, 0},
ans27{4},
},
question27{
{
para27{[]int{0, 0, 0, 0, 0}, 0},
ans27{0},
},
question27{
{
para27{[]int{1}, 1},
ans27{0},
},
question27{
{
para27{[]int{0, 1, 2, 2, 3, 0, 4, 2}, 2},
ans27{5},
},

View File

@ -27,32 +27,32 @@ func Test_Problem28(t *testing.T) {
qs := []question28{
question28{
{
para28{"abab", "ab"},
ans28{0},
},
question28{
{
para28{"hello", "ll"},
ans28{2},
},
question28{
{
para28{"", "abc"},
ans28{0},
},
question28{
{
para28{"abacbabc", "abc"},
ans28{5},
},
question28{
{
para28{"abacbabc", "abcd"},
ans28{-1},
},
question28{
{
para28{"abacbabc", ""},
ans28{0},
},

View File

@ -27,27 +27,27 @@ func Test_Problem29(t *testing.T) {
qs := []question29{
question29{
{
para29{10, 3},
ans29{3},
},
question29{
{
para29{7, -3},
ans29{-2},
},
question29{
{
para29{-1, 1},
ans29{-1},
},
question29{
{
para29{1, -1},
ans29{-1},
},
question29{
{
para29{2147483647, 3},
ans29{715827882},
},

View File

@ -27,57 +27,57 @@ func Test_Problem30(t *testing.T) {
qs := []question30{
question30{
{
para30{"aaaaaaaa", []string{"aa", "aa", "aa"}},
ans30{[]int{0, 1, 2}},
},
question30{
{
para30{"barfoothefoobarman", []string{"foo", "bar"}},
ans30{[]int{0, 9}},
},
question30{
{
para30{"wordgoodgoodgoodbestword", []string{"word", "good", "best", "word"}},
ans30{[]int{}},
},
question30{
{
para30{"goodgoodgoodgoodgood", []string{"good"}},
ans30{[]int{0, 4, 8, 12, 16}},
},
question30{
{
para30{"barofoothefoolbarman", []string{"foo", "bar"}},
ans30{[]int{}},
},
question30{
{
para30{"bbarffoothefoobarman", []string{"foo", "bar"}},
ans30{[]int{}},
},
question30{
{
para30{"ooroodoofoodtoo", []string{"foo", "doo", "roo", "tee", "oo"}},
ans30{[]int{}},
},
question30{
{
para30{"abc", []string{"a", "b", "c"}},
ans30{[]int{0}},
},
question30{
{
para30{"a", []string{"b"}},
ans30{[]int{}},
},
question30{
{
para30{"ab", []string{"ba"}},
ans30{[]int{}},
},
question30{
{
para30{"n", []string{}},
ans30{[]int{}},
},

View File

@ -27,17 +27,17 @@ func Test_Problem33(t *testing.T) {
qs := []question33{
question33{
{
para33{[]int{3, 1}, 1},
ans33{1},
},
question33{
{
para33{[]int{4, 5, 6, 7, 0, 1, 2}, 0},
ans33{4},
},
question33{
{
para33{[]int{4, 5, 6, 7, 0, 1, 2}, 3},
ans33{-1},
},

View File

@ -27,12 +27,12 @@ func Test_Problem34(t *testing.T) {
qs := []question34{
question34{
{
para34{[]int{5, 7, 7, 8, 8, 10}, 8},
ans34{[]int{3, 4}},
},
question34{
{
para34{[]int{5, 7, 7, 8, 8, 10}, 6},
ans34{[]int{-1, -1}},
},

View File

@ -27,22 +27,22 @@ func Test_Problem35(t *testing.T) {
qs := []question35{
question35{
{
para35{[]int{1, 3, 5, 6}, 5},
ans35{2},
},
question35{
{
para35{[]int{1, 3, 5, 6}, 2},
ans35{1},
},
question35{
{
para35{[]int{1, 3, 5, 6}, 7},
ans35{4},
},
question35{
{
para35{[]int{1, 3, 5, 6}, 0},
ans35{0},
},

View File

@ -26,45 +26,45 @@ func Test_Problem36(t *testing.T) {
qs := []question36{
question36{
{
para36{[][]byte{
[]byte{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
ans36{true},
},
question36{
{
para36{[][]byte{
[]byte{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
ans36{false},
},
question36{
{
para36{[][]byte{
[]byte{'.', '8', '7', '6', '5', '4', '3', '2', '1'},
[]byte{'2', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'3', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'4', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'5', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'7', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'8', '.', '.', '.', '.', '.', '.', '.', '.'},
[]byte{'9', '.', '.', '.', '.', '.', '.', '.', '.'}}},
{'.', '8', '7', '6', '5', '4', '3', '2', '1'},
{'2', '.', '.', '.', '.', '.', '.', '.', '.'},
{'3', '.', '.', '.', '.', '.', '.', '.', '.'},
{'4', '.', '.', '.', '.', '.', '.', '.', '.'},
{'5', '.', '.', '.', '.', '.', '.', '.', '.'},
{'6', '.', '.', '.', '.', '.', '.', '.', '.'},
{'7', '.', '.', '.', '.', '.', '.', '.', '.'},
{'8', '.', '.', '.', '.', '.', '.', '.', '.'},
{'9', '.', '.', '.', '.', '.', '.', '.', '.'}}},
ans36{true},
},
}

View File

@ -26,27 +26,27 @@ func Test_Problem37(t *testing.T) {
qs := []question37{
question37{
{
para37{[][]byte{
[]byte{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
[]byte{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
[]byte{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
[]byte{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
[]byte{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
[]byte{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
[]byte{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
[]byte{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
[]byte{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}}},
ans37{[][]byte{
[]byte{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
[]byte{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
[]byte{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
[]byte{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
[]byte{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
[]byte{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
[]byte{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
[]byte{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
[]byte{'3', '4', '5', '2', '8', '6', '1', '7', '9'}}},
{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}}},
},
}

View File

@ -27,14 +27,14 @@ func Test_Problem39(t *testing.T) {
qs := []question39{
question39{
{
para39{[]int{2, 3, 6, 7}, 7},
ans39{[][]int{[]int{7}, []int{2, 2, 3}}},
ans39{[][]int{{7}, {2, 2, 3}}},
},
question39{
{
para39{[]int{2, 3, 5}, 8},
ans39{[][]int{[]int{2, 2, 2, 2}, []int{2, 3, 3}, []int{3, 5}}},
ans39{[][]int{{2, 2, 2, 2}, {2, 3, 3}, {3, 5}}},
},
}

View File

@ -27,14 +27,14 @@ func Test_Problem40(t *testing.T) {
qs := []question40{
question40{
{
para40{[]int{10, 1, 2, 7, 6, 1, 5}, 8},
ans40{[][]int{[]int{1, 7}, []int{1, 2, 5}, []int{2, 6}, []int{1, 1, 6}}},
ans40{[][]int{{1, 7}, {1, 2, 5}, {2, 6}, {1, 1, 6}}},
},
question40{
{
para40{[]int{2, 5, 2, 1, 2}, 5},
ans40{[][]int{[]int{1, 2, 2}, []int{5}}},
ans40{[][]int{{1, 2, 2}, {5}}},
},
}

View File

@ -26,37 +26,37 @@ func Test_Problem41(t *testing.T) {
qs := []question41{
question41{
{
para41{[]int{10, -1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, -3}},
ans41{6},
},
question41{
{
para41{[]int{10, -1, 8, 6, 7, 3, -2, 5, 4, 2, 1, -3}},
ans41{9},
},
question41{
{
para41{[]int{1}},
ans41{2},
},
question41{
{
para41{[]int{0, 2, 2, 1, 1}},
ans41{3},
},
question41{
{
para41{[]int{}},
ans41{1},
},
question41{
{
para41{[]int{1, 2, 0}},
ans41{3},
},
question41{
{
para41{[]int{3, 4, -1, 1}},
ans41{2},
},

View File

@ -26,7 +26,7 @@ func Test_Problem42(t *testing.T) {
qs := []question42{
question42{
{
para42{[]int{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}},
ans42{6},
},

View File

@ -26,9 +26,9 @@ func Test_Problem46(t *testing.T) {
qs := []question46{
question46{
{
para46{[]int{1, 2, 3}},
ans46{[][]int{[]int{1, 2, 3}, []int{1, 3, 2}, []int{2, 1, 3}, []int{2, 3, 1}, []int{3, 1, 2}, []int{3, 2, 1}}},
ans46{[][]int{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}},
},
}

View File

@ -26,19 +26,19 @@ func Test_Problem47(t *testing.T) {
qs := []question47{
question47{
{
para47{[]int{1, 1, 2}},
ans47{[][]int{[]int{1, 1, 2}, []int{1, 2, 1}, []int{2, 1, 1}}},
ans47{[][]int{{1, 1, 2}, {1, 2, 1}, {2, 1, 1}}},
},
question47{
{
para47{[]int{1, 2, 2}},
ans47{[][]int{[]int{1, 2, 2}, []int{2, 2, 1}, []int{2, 1, 2}}},
ans47{[][]int{{1, 2, 2}, {2, 2, 1}, {2, 1, 2}}},
},
question47{
{
para47{[]int{2, 2, 2}},
ans47{[][]int{[]int{2, 2, 2}}},
ans47{[][]int{{2, 2, 2}}},
},
}

View File

@ -26,14 +26,14 @@ func Test_Problem48(t *testing.T) {
qs := []question48{
question48{
para48{[][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}}},
ans48{[][]int{[]int{7, 4, 1}, []int{8, 5, 2}, []int{9, 6, 3}}},
{
para48{[][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}},
ans48{[][]int{{7, 4, 1}, {8, 5, 2}, {9, 6, 3}}},
},
question48{
para48{[][]int{[]int{5, 1, 9, 11}, []int{2, 4, 8, 10}, []int{13, 3, 6, 7}, []int{15, 14, 12, 16}}},
ans48{[][]int{[]int{15, 13, 2, 5}, []int{14, 3, 4, 1}, []int{12, 6, 8, 9}, []int{16, 7, 10, 11}}},
{
para48{[][]int{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}}},
ans48{[][]int{{15, 13, 2, 5}, {14, 3, 4, 1}, {12, 6, 8, 9}, {16, 7, 10, 11}}},
},
}

View File

@ -26,9 +26,9 @@ func Test_Problem49(t *testing.T) {
qs := []question49{
question49{
{
para49{[]string{"eat", "tea", "tan", "ate", "nat", "bat"}},
ans49{[][]string{[]string{"ate", "eat", "tea"}, []string{"nat", "tan"}, []string{"bat"}}},
ans49{[][]string{{"ate", "eat", "tea"}, {"nat", "tan"}, {"bat"}}},
},
}

View File

@ -27,17 +27,17 @@ func Test_Problem50(t *testing.T) {
qs := []question50{
question50{
{
para50{2.00000, 10},
ans50{1024.00000},
},
question50{
{
para50{2.10000, 3},
ans50{9.26100},
},
question50{
{
para50{2.00000, -2},
ans50{0.25000},
},

View File

@ -26,14 +26,14 @@ func Test_Problem51(t *testing.T) {
qs := []question51{
question51{
{
para51{4},
ans51{[][]string{
[]string{".Q..",
{".Q..",
"...Q",
"Q...",
"..Q."},
[]string{"..Q.",
{"..Q.",
"Q...",
"...Q",
".Q.."},

View File

@ -26,51 +26,51 @@ func Test_Problem52(t *testing.T) {
qs := []question52{
question52{
{
para52{1},
ans52{1},
},
question52{
{
para52{2},
ans52{0},
},
question52{
{
para52{3},
ans52{0},
},
question52{
{
para52{4},
ans52{2},
},
question52{
{
para52{5},
ans52{10},
},
question52{
{
para52{6},
ans52{4},
},
question52{
{
para52{7},
ans52{40},
},
question52{
{
para52{8},
ans52{92},
},
question52{
{
para52{9},
ans52{352},
},
question52{
{
para52{10},
ans52{724},
},

View File

@ -26,21 +26,21 @@ func Test_Problem53(t *testing.T) {
qs := []question53{
question53{
{
para53{[]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}},
ans53{6},
},
question53{
{
para53{[]int{2, 7, 9, 3, 1}},
ans53{22},
},
question53{
{
para53{[]int{2}},
ans53{2},
},
question53{
{
para53{[]int{-1, -2}},
ans53{-1},
},

View File

@ -19,10 +19,10 @@ func spiralOrder(matrix [][]int) []int {
return res
}
visit, m, n, round, x, y, spDir := make([][]int, len(matrix)), len(matrix), len(matrix[0]), 0, 0, 0, [][]int{
[]int{0, 1}, // 朝右
[]int{1, 0}, // 朝下
[]int{0, -1}, // 朝左
[]int{-1, 0}, // 朝上
{0, 1}, // 朝右
{1, 0}, // 朝下
{0, -1}, // 朝左
{-1, 0}, // 朝上
}
for i := 0; i < m; i++ {
visit[i] = make([]int, n)

View File

@ -26,27 +26,27 @@ func Test_Problem54(t *testing.T) {
qs := []question54{
question54{
para54{[][]int{[]int{3}, []int{2}}},
{
para54{[][]int{{3}, {2}}},
ans54{[]int{3, 2}},
},
question54{
para54{[][]int{[]int{2, 3}}},
{
para54{[][]int{{2, 3}}},
ans54{[]int{2, 3}},
},
question54{
para54{[][]int{[]int{1}}},
{
para54{[][]int{{1}}},
ans54{[]int{1}},
},
question54{
para54{[][]int{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}}},
{
para54{[][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}},
ans54{[]int{1, 2, 3, 6, 9, 8, 7, 4, 5}},
},
question54{
para54{[][]int{[]int{1, 2, 3, 4}, []int{5, 6, 7, 8}, []int{9, 10, 11, 12}}},
{
para54{[][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}},
ans54{[]int{1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7}},
},
}

View File

@ -26,11 +26,11 @@ func Test_Problem55(t *testing.T) {
qs := []question55{
question55{
{
para55{[]int{2, 3, 1, 1, 4}},
ans55{true},
},
question55{
{
para55{[]int{3, 2, 1, 0, 4}},
ans55{false},
},

View File

@ -26,39 +26,39 @@ func Test_Problem56(t *testing.T) {
qs := []question56{
question56{
{
para56{[]Interval{}},
ans56{[]Interval{}},
},
question56{
para56{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 2, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
ans56{[]Interval{Interval{Start: 1, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
{
para56{[]Interval{{Start: 1, End: 3}, {Start: 2, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}},
ans56{[]Interval{{Start: 1, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}},
},
question56{
para56{[]Interval{Interval{Start: 1, End: 4}, Interval{Start: 4, End: 5}}},
ans56{[]Interval{Interval{Start: 1, End: 5}}},
{
para56{[]Interval{{Start: 1, End: 4}, {Start: 4, End: 5}}},
ans56{[]Interval{{Start: 1, End: 5}}},
},
question56{
para56{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 3, End: 6}, Interval{Start: 5, End: 10}, Interval{Start: 9, End: 18}}},
ans56{[]Interval{Interval{Start: 1, End: 18}}},
{
para56{[]Interval{{Start: 1, End: 3}, {Start: 3, End: 6}, {Start: 5, End: 10}, {Start: 9, End: 18}}},
ans56{[]Interval{{Start: 1, End: 18}}},
},
question56{
para56{[]Interval{Interval{Start: 15, End: 18}, Interval{Start: 8, End: 10}, Interval{Start: 2, End: 6}, Interval{Start: 1, End: 3}}},
ans56{[]Interval{Interval{Start: 1, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
{
para56{[]Interval{{Start: 15, End: 18}, {Start: 8, End: 10}, {Start: 2, End: 6}, {Start: 1, End: 3}}},
ans56{[]Interval{{Start: 1, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}},
},
question56{
para56{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 2, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
ans56{[]Interval{Interval{Start: 1, End: 6}, Interval{Start: 8, End: 10}, Interval{Start: 15, End: 18}}},
{
para56{[]Interval{{Start: 1, End: 3}, {Start: 2, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}},
ans56{[]Interval{{Start: 1, End: 6}, {Start: 8, End: 10}, {Start: 15, End: 18}}},
},
question56{
para56{[]Interval{Interval{Start: 1, End: 4}, Interval{Start: 1, End: 5}}},
ans56{[]Interval{Interval{Start: 1, End: 5}}},
{
para56{[]Interval{{Start: 1, End: 4}, {Start: 1, End: 5}}},
ans56{[]Interval{{Start: 1, End: 5}}},
},
}

View File

@ -27,34 +27,34 @@ func Test_Problem57(t *testing.T) {
qs := []question57{
question57{
{
para57{[]Interval{}, Interval{}},
ans57{[]Interval{}},
},
question57{
para57{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 6, End: 9}}, Interval{Start: 4, End: 8}},
ans57{[]Interval{Interval{Start: 1, End: 5}, Interval{Start: 6, End: 9}}},
{
para57{[]Interval{{Start: 1, End: 3}, {Start: 6, End: 9}}, Interval{Start: 4, End: 8}},
ans57{[]Interval{{Start: 1, End: 5}, {Start: 6, End: 9}}},
},
question57{
para57{[]Interval{Interval{Start: 1, End: 3}, Interval{Start: 6, End: 9}}, Interval{Start: 2, End: 5}},
ans57{[]Interval{Interval{Start: 1, End: 5}, Interval{Start: 6, End: 9}}},
{
para57{[]Interval{{Start: 1, End: 3}, {Start: 6, End: 9}}, Interval{Start: 2, End: 5}},
ans57{[]Interval{{Start: 1, End: 5}, {Start: 6, End: 9}}},
},
question57{
para57{[]Interval{Interval{Start: 1, End: 2}, Interval{Start: 3, End: 5}, Interval{Start: 6, End: 7}, Interval{Start: 8, End: 10}, Interval{Start: 12, End: 16}}, Interval{Start: 4, End: 8}},
ans57{[]Interval{Interval{Start: 1, End: 2}, Interval{Start: 3, End: 10}, Interval{Start: 12, End: 16}}},
{
para57{[]Interval{{Start: 1, End: 2}, {Start: 3, End: 5}, {Start: 6, End: 7}, {Start: 8, End: 10}, {Start: 12, End: 16}}, Interval{Start: 4, End: 8}},
ans57{[]Interval{{Start: 1, End: 2}, {Start: 3, End: 10}, {Start: 12, End: 16}}},
},
question57{
para57{[]Interval{Interval{Start: 1, End: 5}}, Interval{Start: 5, End: 7}},
ans57{[]Interval{Interval{Start: 1, End: 7}}},
{
para57{[]Interval{{Start: 1, End: 5}}, Interval{Start: 5, End: 7}},
ans57{[]Interval{{Start: 1, End: 7}}},
},
question57{
para57{[]Interval{Interval{Start: 1, End: 2}, Interval{Start: 3, End: 5}, Interval{Start: 6, End: 7}, Interval{Start: 8, End: 10}, Interval{Start: 12, End: 16}}, Interval{Start: 9, End: 12}},
ans57{[]Interval{Interval{Start: 1, End: 2}, Interval{Start: 3, End: 5}, Interval{Start: 6, End: 7}, Interval{Start: 8, End: 16}}},
{
para57{[]Interval{{Start: 1, End: 2}, {Start: 3, End: 5}, {Start: 6, End: 7}, {Start: 8, End: 10}, {Start: 12, End: 16}}, Interval{Start: 9, End: 12}},
ans57{[]Interval{{Start: 1, End: 2}, {Start: 3, End: 5}, {Start: 6, End: 7}, {Start: 8, End: 16}}},
},
}

View File

@ -5,13 +5,13 @@ func generateMatrix(n int) [][]int {
return [][]int{}
}
if n == 1 {
return [][]int{[]int{1}}
return [][]int{{1}}
}
res, visit, round, x, y, spDir := make([][]int, n), make([][]int, n), 0, 0, 0, [][]int{
[]int{0, 1}, // 朝右
[]int{1, 0}, // 朝下
[]int{0, -1}, // 朝左
[]int{-1, 0}, // 朝上
{0, 1}, // 朝右
{1, 0}, // 朝下
{0, -1}, // 朝左
{-1, 0}, // 朝上
}
for i := 0; i < n; i++ {
visit[i] = make([]int, n)

View File

@ -26,14 +26,14 @@ func Test_Problem59(t *testing.T) {
qs := []question59{
question59{
{
para59{3},
ans59{[][]int{[]int{1, 2, 3}, []int{8, 9, 4}, []int{7, 6, 5}}},
ans59{[][]int{{1, 2, 3}, {8, 9, 4}, {7, 6, 5}}},
},
question59{
{
para59{4},
ans59{[][]int{[]int{1, 2, 3, 4}, []int{12, 13, 14, 5}, []int{11, 16, 15, 6}, []int{10, 9, 8, 7}}},
ans59{[][]int{{1, 2, 3, 4}, {12, 13, 14, 5}, {11, 16, 15, 6}, {10, 9, 8, 7}}},
},
}

View File

@ -27,12 +27,12 @@ func Test_Problem60(t *testing.T) {
qs := []question60{
question60{
{
para60{3, 3},
ans60{"213"},
},
question60{
{
para60{4, 9},
ans60{"2314"},
},

View File

@ -29,32 +29,32 @@ func Test_Problem61(t *testing.T) {
qs := []question61{
question61{
{
para61{[]int{1, 2, 3, 4, 5}, 2},
ans61{[]int{4, 5, 1, 2, 3}},
},
question61{
{
para61{[]int{1, 2, 3, 4, 5}, 3},
ans61{[]int{4, 5, 1, 2, 3}},
},
question61{
{
para61{[]int{0, 1, 2}, 4},
ans61{[]int{2, 0, 1}},
},
question61{
{
para61{[]int{1, 1, 1, 2}, 3},
ans61{[]int{1, 1, 2, 1}},
},
question61{
{
para61{[]int{1}, 10},
ans61{[]int{1}},
},
question61{
{
para61{[]int{}, 100},
ans61{[]int{}},
},

View File

@ -27,17 +27,17 @@ func Test_Problem62(t *testing.T) {
qs := []question62{
question62{
{
para62{3, 2},
ans62{3},
},
question62{
{
para62{7, 3},
ans62{28},
},
question62{
{
para62{1, 2},
ans62{1},
},

View File

@ -26,29 +26,29 @@ func Test_Problem63(t *testing.T) {
qs := []question63{
question63{
{
para63{[][]int{
[]int{0, 0, 0},
[]int{0, 1, 0},
[]int{0, 0, 0},
{0, 0, 0},
{0, 1, 0},
{0, 0, 0},
}},
ans63{2},
},
question63{
{
para63{[][]int{
[]int{0, 0},
[]int{1, 1},
[]int{0, 0},
{0, 0},
{1, 1},
{0, 0},
}},
ans63{0},
},
question63{
{
para63{[][]int{
[]int{0, 1, 0, 0},
[]int{1, 0, 0, 0},
[]int{0, 0, 0, 0},
{0, 1, 0, 0},
{1, 0, 0, 0},
{0, 0, 0, 0},
}},
ans63{0},
},

View File

@ -26,11 +26,11 @@ func Test_Problem64(t *testing.T) {
qs := []question64{
question64{
{
para64{[][]int{
[]int{1, 3, 1},
[]int{1, 5, 1},
[]int{4, 2, 1},
{1, 3, 1},
{1, 5, 1},
{4, 2, 1},
}},
ans64{7},
},

View File

@ -26,22 +26,22 @@ func Test_Problem66(t *testing.T) {
qs := []question66{
question66{
{
para66{[]int{1, 2, 3}},
ans66{[]int{1, 2, 4}},
},
question66{
{
para66{[]int{4, 3, 2, 1}},
ans66{[]int{4, 3, 2, 2}},
},
question66{
{
para66{[]int{9, 9}},
ans66{[]int{1, 0, 0}},
},
question66{
{
para66{[]int{0}},
ans66{[]int{0}},
},

View File

@ -27,12 +27,12 @@ func Test_Problem67(t *testing.T) {
qs := []question67{
question67{
{
para67{"11", "1"},
ans67{"100"},
},
question67{
{
para67{"1010", "1011"},
ans67{"10101"},
},

View File

@ -26,12 +26,12 @@ func Test_Problem69(t *testing.T) {
qs := []question69{
question69{
{
para69{4},
ans69{2},
},
question69{
{
para69{8},
ans69{2},
},

View File

@ -26,12 +26,12 @@ func Test_Problem70(t *testing.T) {
qs := []question70{
question70{
{
para70{2},
ans70{2},
},
question70{
{
para70{3},
ans70{3},
},

View File

@ -26,42 +26,42 @@ func Test_Problem71(t *testing.T) {
qs := []question71{
question71{
{
para71{"/.hidden"},
ans71{"/.hidden"},
},
question71{
{
para71{"/..hidden"},
ans71{"/..hidden"},
},
question71{
{
para71{"/abc/..."},
ans71{"/abc/..."},
},
question71{
{
para71{"/home/"},
ans71{"/home"},
},
question71{
{
para71{"/..."},
ans71{"/..."},
},
question71{
{
para71{"/../"},
ans71{"/"},
},
question71{
{
para71{"/home//foo/"},
ans71{"/home/foo"},
},
question71{
{
para71{"/a/./b/../../c/"},
ans71{"/c"},
},

View File

@ -27,13 +27,13 @@ func Test_Problem74(t *testing.T) {
qs := []question74{
question74{
para74{[][]int{[]int{1, 3, 5, 7}, []int{10, 11, 16, 20}, []int{23, 30, 34, 50}}, 3},
{
para74{[][]int{{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50}}, 3},
ans74{true},
},
question74{
para74{[][]int{[]int{1, 3, 5, 7}, []int{10, 11, 16, 20}, []int{23, 30, 34, 50}}, 13},
{
para74{[][]int{{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50}}, 13},
ans74{false},
},
}

View File

@ -26,22 +26,22 @@ func Test_Problem75(t *testing.T) {
qs := []question75{
question75{
{
para75{[]int{}},
ans75{[]int{}},
},
question75{
{
para75{[]int{1}},
ans75{[]int{1}},
},
question75{
{
para75{[]int{2, 0, 2, 1, 1, 0}},
ans75{[]int{0, 0, 1, 1, 2, 2}},
},
question75{
{
para75{[]int{2, 0, 1, 1, 2, 0, 2, 1, 2, 0, 0, 0, 1, 2, 2, 2, 0, 1, 1}},
ans75{[]int{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2}},
},

View File

@ -27,17 +27,17 @@ func Test_Problem76(t *testing.T) {
qs := []question76{
question76{
{
para76{"ADOBECODEBANC", "ABC"},
ans76{"BANC"},
},
question76{
{
para76{"a", "aa"},
ans76{""},
},
question76{
{
para76{"a", "a"},
ans76{"a"},
},

View File

@ -27,9 +27,9 @@ func Test_Problem77(t *testing.T) {
qs := []question77{
question77{
{
para77{4, 2},
ans77{[][]int{[]int{2, 4}, []int{3, 4}, []int{2, 3}, []int{1, 2}, []int{1, 3}, []int{1, 4}}},
ans77{[][]int{{2, 4}, {3, 4}, {2, 3}, {1, 2}, {1, 3}, {1, 4}}},
},
}

View File

@ -26,14 +26,14 @@ func Test_Problem78(t *testing.T) {
qs := []question78{
question78{
{
para78{[]int{}},
ans78{[][]int{[]int{}}},
ans78{[][]int{{}}},
},
question78{
{
para78{[]int{1, 2, 3}},
ans78{[][]int{[]int{}, []int{1}, []int{2}, []int{3}, []int{1, 2}, []int{2, 3}, []int{1, 3}, []int{1, 2, 3}}},
ans78{[][]int{{}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}}},
},
}

View File

@ -1,10 +1,10 @@
package leetcode
var dir = [][]int{
[]int{-1, 0},
[]int{0, 1},
[]int{1, 0},
[]int{0, -1},
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
func exist(board [][]byte, word string) bool {

View File

@ -27,69 +27,69 @@ func Test_Problem79(t *testing.T) {
qs := []question79{
question79{
{
para79{[][]byte{
[]byte{'A', 'B', 'C', 'E'},
[]byte{'S', 'F', 'C', 'S'},
[]byte{'A', 'D', 'E', 'E'},
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'},
}, "ABCCED"},
ans79{true},
},
question79{
{
para79{[][]byte{
[]byte{'A', 'B', 'C', 'E'},
[]byte{'S', 'F', 'C', 'S'},
[]byte{'A', 'D', 'E', 'E'},
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'},
}, "SEE"},
ans79{true},
},
question79{
{
para79{[][]byte{
[]byte{'A', 'B', 'C', 'E'},
[]byte{'S', 'F', 'C', 'S'},
[]byte{'A', 'D', 'E', 'E'},
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'},
}, "ABCB"},
ans79{false},
},
question79{
{
para79{[][]byte{
[]byte{'o', 'a', 'a', 'n'},
[]byte{'e', 't', 'a', 'e'},
[]byte{'i', 'h', 'k', 'r'},
[]byte{'i', 'f', 'l', 'v'},
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'},
}, "oath"},
ans79{true},
},
question79{
{
para79{[][]byte{
[]byte{'o', 'a', 'a', 'n'},
[]byte{'e', 't', 'a', 'e'},
[]byte{'i', 'h', 'k', 'r'},
[]byte{'i', 'f', 'l', 'v'},
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'},
}, "pea"},
ans79{false},
},
question79{
{
para79{[][]byte{
[]byte{'o', 'a', 'a', 'n'},
[]byte{'e', 't', 'a', 'e'},
[]byte{'i', 'h', 'k', 'r'},
[]byte{'i', 'f', 'l', 'v'},
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'},
}, "eat"},
ans79{true},
},
question79{
{
para79{[][]byte{
[]byte{'o', 'a', 'a', 'n'},
[]byte{'e', 't', 'a', 'e'},
[]byte{'i', 'h', 'k', 'r'},
[]byte{'i', 'f', 'l', 'v'},
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'},
}, "rain"},
ans79{false},
},

View File

@ -4,9 +4,9 @@ func removeDuplicates80(nums []int) int {
if len(nums) == 0 {
return 0
}
last, finder, startFinder := 0, 0, -1
last, finder := 0, 0
for last < len(nums)-1 {
startFinder = -1
startFinder := -1
for nums[finder] == nums[last] {
if startFinder == -1 || startFinder > finder {
startFinder = finder

View File

@ -26,32 +26,32 @@ func Test_Problem80(t *testing.T) {
qs := []question80{
question80{
{
para80{[]int{1, 1, 2}},
ans80{3},
},
question80{
{
para80{[]int{0, 0, 1, 1, 1, 1, 2, 3, 4, 4}},
ans80{8},
},
question80{
{
para80{[]int{0, 0, 0, 0, 0}},
ans80{2},
},
question80{
{
para80{[]int{1}},
ans80{1},
},
question80{
{
para80{[]int{0, 0, 1, 1, 1, 1, 2, 3, 3}},
ans80{7},
},
question80{
{
para80{[]int{1, 1, 1, 1, 2, 2, 3}},
ans80{5},
},

View File

@ -27,12 +27,12 @@ func Test_Problem81(t *testing.T) {
qs := []question81{
question81{
{
para81{[]int{2, 5, 6, 0, 0, 1, 2}, 0},
ans81{true},
},
question81{
{
para81{[]int{2, 5, 6, 0, 0, 1, 2}, 3},
ans81{false},
},

View File

@ -91,3 +91,66 @@ func deleteDuplicates(head *ListNode) *ListNode {
}
return head
}
// 双循环简单解法 O(n*m)
func deleteDuplicates3(head *ListNode) *ListNode {
if head == nil {
return head
}
nilNode := &ListNode{Val: 0, Next: head}
head = nilNode
lastVal := 0
for head.Next != nil && head.Next.Next != nil {
if head.Next.Val == head.Next.Next.Val {
lastVal = head.Next.Val
for head.Next != nil && lastVal == head.Next.Val {
head.Next = head.Next.Next
}
} else {
head = head.Next
}
}
return nilNode.Next
}
// 双指针+删除标志位,单循环解法 O(n)
func deleteDuplicates4(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
nilNode := &ListNode{Val: 0, Next: head}
// 上次遍历有删除操作的标志位
lastIsDel := false
// 虚拟空结点
head = nilNode
// 前后指针用于判断
pre, back := head.Next, head.Next.Next
// 每次只删除前面的一个重复的元素,留一个用于下次遍历判重
// pre, back 指针的更新位置和值比较重要和巧妙
for head.Next != nil && head.Next.Next != nil {
if pre.Val != back.Val && lastIsDel {
head.Next = head.Next.Next
pre, back = head.Next, head.Next.Next
lastIsDel = false
continue
}
if pre.Val == back.Val {
head.Next = head.Next.Next
pre, back = head.Next, head.Next.Next
lastIsDel = true
} else {
head = head.Next
pre, back = head.Next, head.Next.Next
lastIsDel = false
}
}
// 处理 [1,1] 这种删除还剩一个的情况
if lastIsDel && head.Next != nil {
head.Next = nil
}
return nilNode.Next
}

View File

@ -28,47 +28,47 @@ func Test_Problem82(t *testing.T) {
qs := []question82{
question82{
{
para82{[]int{1, 1, 2, 2, 3, 4, 4, 4}},
ans82{[]int{3}},
},
question82{
{
para82{[]int{1, 1, 1, 1, 1, 1}},
ans82{[]int{}},
},
question82{
{
para82{[]int{1, 1, 1, 2, 3}},
ans82{[]int{2, 3}},
},
question82{
{
para82{[]int{1}},
ans82{[]int{1}},
},
question82{
{
para82{[]int{}},
ans82{[]int{}},
},
question82{
{
para82{[]int{1, 2, 2, 2, 2}},
ans82{[]int{1}},
},
question82{
{
para82{[]int{1, 1, 2, 3, 3, 4, 5, 5, 6}},
ans82{[]int{2, 4, 6}},
},
question82{
{
para82{[]int{1, 1, 2, 3, 3, 4, 5, 6}},
ans82{[]int{2, 4, 5, 6}},
},
question82{
{
para82{[]int{0, 1, 2, 2, 3, 4}},
ans82{[]int{0, 1, 2, 2, 3, 4}},
},

View File

@ -28,17 +28,17 @@ func Test_Problem83(t *testing.T) {
qs := []question83{
question83{
{
para83{[]int{1, 1, 2}},
ans83{[]int{1, 2}},
},
question83{
{
para83{[]int{1, 1, 2, 2, 3, 3, 3}},
ans83{[]int{1, 2, 3}},
},
question83{
{
para83{[]int{1, 1, 1, 1, 1, 1, 1, 1}},
ans83{[]int{1}},
},

View File

@ -26,17 +26,17 @@ func Test_Problem84(t *testing.T) {
qs := []question84{
question84{
{
para84{[]int{2, 1, 5, 6, 2, 3}},
ans84{10},
},
question84{
{
para84{[]int{1}},
ans84{1},
},
question84{
{
para84{[]int{1, 1}},
ans84{2},
},

View File

@ -29,37 +29,37 @@ func Test_Problem86(t *testing.T) {
qs := []question86{
question86{
{
para86{[]int{1, 4, 3, 2, 5, 2}, 3},
ans86{[]int{1, 2, 2, 4, 3, 5}},
},
question86{
{
para86{[]int{1, 1, 2, 2, 3, 3, 3}, 2},
ans86{[]int{1, 1, 2, 2, 3, 3, 3}},
},
question86{
{
para86{[]int{1, 4, 3, 2, 5, 2}, 0},
ans86{[]int{1, 4, 3, 2, 5, 2}},
},
question86{
{
para86{[]int{4, 3, 2, 5, 2}, 3},
ans86{[]int{2, 2, 4, 3, 5}},
},
question86{
{
para86{[]int{1, 1, 1, 1, 1, 1}, 1},
ans86{[]int{1, 1, 1, 1, 1, 1}},
},
question86{
{
para86{[]int{3, 1}, 2},
ans86{[]int{1, 3}},
},
question86{
{
para86{[]int{1, 2}, 3},
ans86{[]int{1, 2}},
},

View File

@ -44,7 +44,7 @@ func Test_Problem88(t *testing.T) {
// ans{[]int{1, 2, 2, 3}},
// },
question88{
{
para88{[]int{1, 2, 3, 0, 0, 0}, 3, []int{2, 5, 6}, 3},
ans88{[]int{1, 2, 2, 3, 5, 6}},
},

View File

@ -26,17 +26,17 @@ func Test_Problem89(t *testing.T) {
qs := []question89{
question89{
{
para89{2},
ans89{[]int{0, 1, 3, 2}},
},
question89{
{
para89{0},
ans89{[]int{0}},
},
question89{
{
para89{3},
ans89{[]int{0, 1, 3, 2, 6, 7, 5, 4}},
},

View File

@ -26,14 +26,14 @@ func Test_Problem90(t *testing.T) {
qs := []question90{
question90{
{
para90{[]int{}},
ans90{[][]int{[]int{}}},
ans90{[][]int{{}}},
},
question90{
{
para90{[]int{1, 2, 2}},
ans90{[][]int{[]int{}, []int{1}, []int{2}, []int{1, 2}, []int{2, 2}, []int{1, 2, 2}}},
ans90{[][]int{{}, {1}, {2}, {1, 2}, {2, 2}, {1, 2, 2}}},
},
}

View File

@ -26,12 +26,12 @@ func Test_Problem91(t *testing.T) {
qs := []question91{
question91{
{
para91{"12"},
ans91{2},
},
question91{
{
para91{"226"},
ans91{3},
},

View File

@ -29,32 +29,32 @@ func Test_Problem92(t *testing.T) {
qs := []question92{
question92{
{
para92{[]int{1, 2, 3, 4, 5}, 2, 4},
ans92{[]int{1, 4, 3, 2, 5}},
},
question92{
{
para92{[]int{1, 2, 3, 4, 5}, 2, 2},
ans92{[]int{1, 2, 3, 4, 5}},
},
question92{
{
para92{[]int{1, 2, 3, 4, 5}, 1, 5},
ans92{[]int{5, 4, 3, 2, 1}},
},
question92{
{
para92{[]int{1, 2, 3, 4, 5, 6}, 3, 4},
ans92{[]int{1, 2, 4, 3, 5, 6}},
},
question92{
{
para92{[]int{3, 5}, 1, 2},
ans92{[]int{5, 3}},
},
question92{
{
para92{[]int{3}, 3, 5},
ans92{[]int{3}},
},

View File

@ -26,17 +26,17 @@ func Test_Problem93(t *testing.T) {
qs := []question93{
question93{
{
para93{"25525511135"},
ans93{[]string{"255.255.11.135", "255.255.111.35"}},
},
question93{
{
para93{"0000"},
ans93{[]string{"0.0.0.0"}},
},
question93{
{
para93{"010010"},
ans93{[]string{"0.10.0.10", "0.100.1.0"}},
},

View File

@ -28,17 +28,17 @@ func Test_Problem94(t *testing.T) {
qs := []question94{
question94{
{
para94{[]int{}},
ans94{[]int{}},
},
question94{
{
para94{[]int{1}},
ans94{[]int{1}},
},
question94{
{
para94{[]int{1, structures.NULL, 2, 3}},
ans94{[]int{1, 2, 3}},
},

View File

@ -29,10 +29,9 @@ func generateBSTree(start, end int) []*TreeNode {
tree = append(tree, nil)
return tree
}
left, right := []*TreeNode{}, []*TreeNode{}
for i := start; i <= end; i++ {
left = generateBSTree(start, i-1)
right = generateBSTree(i+1, end)
left := generateBSTree(start, i-1)
right := generateBSTree(i+1, end)
for _, l := range left {
for _, r := range right {
root := &TreeNode{Val: i, Left: l, Right: r}

View File

@ -28,19 +28,19 @@ func Test_Problem95(t *testing.T) {
qs := []question95{
question95{
{
para95{1},
ans95{[]*TreeNode{&TreeNode{Val: 1, Left: nil, Right: nil}}},
ans95{[]*TreeNode{{Val: 1, Left: nil, Right: nil}}},
},
question95{
{
para95{3},
ans95{[]*TreeNode{
&TreeNode{Val: 1, Left: nil, Right: &TreeNode{Val: 3, Left: &TreeNode{Val: 2, Left: nil, Right: nil}, Right: nil}},
&TreeNode{Val: 1, Left: nil, Right: &TreeNode{Val: 2, Left: nil, Right: &TreeNode{Val: 3, Left: nil, Right: nil}}},
&TreeNode{Val: 3, Left: &TreeNode{Val: 2, Left: &TreeNode{Val: 1, Left: nil, Right: nil}, Right: nil}, Right: nil},
&TreeNode{Val: 3, Left: &TreeNode{Val: 1, Left: nil, Right: &TreeNode{Val: 2, Left: nil, Right: nil}}, Right: nil},
&TreeNode{Val: 2, Left: &TreeNode{Val: 1, Left: nil, Right: nil}, Right: &TreeNode{Val: 3, Left: nil, Right: nil}},
{Val: 1, Left: nil, Right: &TreeNode{Val: 3, Left: &TreeNode{Val: 2, Left: nil, Right: nil}, Right: nil}},
{Val: 1, Left: nil, Right: &TreeNode{Val: 2, Left: nil, Right: &TreeNode{Val: 3, Left: nil, Right: nil}}},
{Val: 3, Left: &TreeNode{Val: 2, Left: &TreeNode{Val: 1, Left: nil, Right: nil}, Right: nil}, Right: nil},
{Val: 3, Left: &TreeNode{Val: 1, Left: nil, Right: &TreeNode{Val: 2, Left: nil, Right: nil}}, Right: nil},
{Val: 2, Left: &TreeNode{Val: 1, Left: nil, Right: nil}, Right: &TreeNode{Val: 3, Left: nil, Right: nil}},
}}},
}

View File

@ -26,46 +26,46 @@ func Test_Problem96(t *testing.T) {
qs := []question96{
question96{
{
para96{1},
ans96{1},
},
question96{
{
para96{3},
ans96{5},
},
question96{
{
para96{4},
ans96{14},
},
question96{
{
para96{5},
ans96{42},
},
question96{
{
para96{6},
ans96{132},
},
question96{
{
para96{7},
ans96{429},
},
question96{
{
para96{8},
ans96{1430},
},
question96{
{
para96{9},
ans96{4862},
},
question96{
{
para96{10},
ans96{16796},
},

View File

@ -28,22 +28,22 @@ func Test_Problem98(t *testing.T) {
qs := []question98{
question98{
{
para98{[]int{10, 5, 15, structures.NULL, structures.NULL, 6, 20}},
ans98{false},
},
question98{
{
para98{[]int{}},
ans98{true},
},
question98{
{
para98{[]int{2, 1, 3}},
ans98{true},
},
question98{
{
para98{[]int{5, 1, 4, structures.NULL, structures.NULL, 3, 6}},
ans98{false},
},

View File

@ -28,12 +28,12 @@ func Test_Problem99(t *testing.T) {
qs := []question99{
question99{
{
para99{[]int{1, 3, structures.NULL, structures.NULL, 2}},
ans99{[]int{3, 1, structures.NULL, structures.NULL, 2}},
},
question99{
{
para99{[]int{3, 1, 4, structures.NULL, structures.NULL, 2}},
ans99{[]int{2, 1, 4, structures.NULL, structures.NULL, 3}},
},

View File

@ -29,32 +29,32 @@ func Test_Problem100(t *testing.T) {
qs := []question100{
question100{
{
para100{[]int{}, []int{}},
ans100{true},
},
question100{
{
para100{[]int{}, []int{1}},
ans100{false},
},
question100{
{
para100{[]int{1}, []int{1}},
ans100{true},
},
question100{
{
para100{[]int{1, 2, 3}, []int{1, 2, 3}},
ans100{true},
},
question100{
{
para100{[]int{1, 2}, []int{1, structures.NULL, 2}},
ans100{false},
},
question100{
{
para100{[]int{1, 2, 1}, []int{1, 1, 2}},
ans100{false},
},

View File

@ -28,37 +28,37 @@ func Test_Problem101(t *testing.T) {
qs := []question101{
question101{
{
para101{[]int{3, 4, 4, 5, structures.NULL, structures.NULL, 5, 6, structures.NULL, structures.NULL, 6}},
ans101{true},
},
question101{
{
para101{[]int{1, 2, 2, structures.NULL, 3, 3}},
ans101{true},
},
question101{
{
para101{[]int{}},
ans101{true},
},
question101{
{
para101{[]int{1}},
ans101{true},
},
question101{
{
para101{[]int{1, 2, 3}},
ans101{false},
},
question101{
{
para101{[]int{1, 2, 2, 3, 4, 4, 3}},
ans101{true},
},
question101{
{
para101{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
ans101{false},
},

View File

@ -28,19 +28,19 @@ func Test_Problem102(t *testing.T) {
qs := []question102{
question102{
{
para102{[]int{}},
ans102{[][]int{}},
},
question102{
{
para102{[]int{1}},
ans102{[][]int{[]int{1}}},
ans102{[][]int{{1}}},
},
question102{
{
para102{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans102{[][]int{[]int{3}, []int{9, 20}, []int{15, 7}}},
ans102{[][]int{{3}, {9, 20}, {15, 7}}},
},
}

View File

@ -16,6 +16,7 @@ type TreeNode = structures.TreeNode
* }
*/
// 解法一
func zigzagLevelOrder(root *TreeNode) [][]int {
if root == nil {
return [][]int{}
@ -57,3 +58,26 @@ func zigzagLevelOrder(root *TreeNode) [][]int {
}
return res
}
// 解法二 递归
func zigzagLevelOrder0(root *TreeNode) [][]int {
var res [][]int
search(root, 0, &res)
return res
}
func search(root *TreeNode, depth int, res *[][]int) {
if root == nil {
return
}
for len(*res) < depth+1 {
*res = append(*res, []int{})
}
if depth%2 == 0 {
(*res)[depth] = append((*res)[depth], root.Val)
} else {
(*res)[depth] = append([]int{root.Val}, (*res)[depth]...)
}
search(root.Left, depth+1, res)
search(root.Right, depth+1, res)
}

View File

@ -28,24 +28,24 @@ func Test_Problem103(t *testing.T) {
qs := []question103{
question103{
{
para103{[]int{}},
ans103{[][]int{}},
},
question103{
{
para103{[]int{1}},
ans103{[][]int{[]int{1}}},
ans103{[][]int{{1}}},
},
question103{
{
para103{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans103{[][]int{[]int{3}, []int{9, 20}, []int{15, 7}}},
ans103{[][]int{{3}, {9, 20}, {15, 7}}},
},
question103{
{
para103{[]int{1, 2, 3, 4, structures.NULL, structures.NULL, 5}},
ans103{[][]int{[]int{1}, []int{3, 2}, []int{4, 5}}},
ans103{[][]int{{1}, {3, 2}, {4, 5}}},
},
}

View File

@ -28,12 +28,12 @@ func Test_Problem104(t *testing.T) {
qs := []question104{
question104{
{
para104{[]int{}},
ans104{0},
},
question104{
{
para104{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans104{3},
},

View File

@ -29,7 +29,7 @@ func Test_Problem105(t *testing.T) {
qs := []question105{
question105{
{
para105{[]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7}},
ans105{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
},

View File

@ -29,7 +29,7 @@ func Test_Problem106(t *testing.T) {
qs := []question106{
question106{
{
para106{[]int{9, 3, 15, 20, 7}, []int{9, 15, 7, 20, 3}},
ans106{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
},

View File

@ -28,19 +28,19 @@ func Test_Problem107(t *testing.T) {
qs := []question107{
question107{
{
para107{[]int{}},
ans107{[][]int{}},
},
question107{
{
para107{[]int{1}},
ans107{[][]int{[]int{1}}},
ans107{[][]int{{1}}},
},
question107{
{
para107{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
ans107{[][]int{[]int{15, 7}, []int{9, 20}, []int{3}}},
ans107{[][]int{{15, 7}, {9, 20}, {3}}},
},
}

View File

@ -28,7 +28,7 @@ func Test_Problem108(t *testing.T) {
qs := []question108{
question108{
{
para108{[]int{-10, -3, 0, 5, 9}},
ans108{[]int{0, -3, 9, -10, structures.NULL, 5}},
},

View File

@ -28,22 +28,22 @@ func Test_Problem109(t *testing.T) {
qs := []question109{
question109{
{
para109{[]int{-10, -3, 0, 5, 9}},
ans109{[]int{0, -10, 5, structures.NULL, -3, structures.NULL, 9}},
},
question109{
{
para109{[]int{-10}},
ans109{[]int{-10}},
},
question109{
{
para109{[]int{1, 2}},
ans109{[]int{1, 2}},
},
question109{
{
para109{[]int{1, 2, 3}},
ans109{[]int{2, 1, 3}},
},

Some files were not shown because too many files have changed in this diff Show More