diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index c0f3e039..a8f3c324 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -408,7 +408,28 @@ class Solution: return True ``` -## Go +```python +# 遵循Carl的写法,只添加了节点判断的部分 +class Solution: + def isValidBST(self, root: TreeNode) -> bool: + # method 2 + que, pre = [], None + while root or que: + while root: + que.append(root) + root = root.left + root = que.pop() + # 对第一个节点只做记录,对后面的节点进行比较 + if pre is None: + pre = root.val + else: + if pre >= root.val: return False + pre = root.val + root = root.right + return True +``` + +## Go ```Go import "math" diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 0007b4d4..e4e232c8 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -437,41 +437,6 @@ class Solution: return True ``` -层序遍历 - -```python -class Solution: - def isSymmetric(self, root: TreeNode) -> bool: - if not root: return True - que, cnt = [[root.left, root.right]], 1 - while que: - nodes, tmp, sign = que.pop(), [], False - for node in nodes: - if not node: - tmp.append(None) - tmp.append(None) - else: - if node.left: - tmp.append(node.left) - sign = True - else: - tmp.append(None) - if node.right: - tmp.append(node.right) - sign = True - else: - tmp.append(None) - p1, p2 = 0, len(nodes) - 1 - while p1 < p2: - if (not nodes[p1] and nodes[p2]) or (nodes[p1] and not nodes[p2]): return False - elif nodes[p1] and nodes[p2] and nodes[p1].val != nodes[p2].val: return False - p1 += 1 - p2 -= 1 - if sign: que.append(tmp) - cnt += 1 - return True -``` - ## Go ```go diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 395f991e..f7ef8cf7 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -506,6 +506,50 @@ var findItinerary = function(tickets) { ``` +### TypeScript + +```typescript +function findItinerary(tickets: string[][]): string[] { + /** + TicketsMap 实例: + { NRT: Map(1) { 'JFK' => 1 }, JFK: Map(2) { 'KUL' => 1, 'NRT' => 1 } } + 这里选择Map数据结构的原因是:与Object类型的一个主要差异是,Map实例会维护键值对的插入顺序。 + */ + type TicketsMap = { + [index: string]: Map + }; + tickets.sort((a, b) => { + return a[1] < b[1] ? -1 : 1; + }); + const ticketMap: TicketsMap = {}; + for (const [from, to] of tickets) { + if (ticketMap[from] === undefined) { + ticketMap[from] = new Map(); + } + ticketMap[from].set(to, (ticketMap[from].get(to) || 0) + 1); + } + const resRoute = ['JFK']; + backTracking(tickets.length, ticketMap, resRoute); + return resRoute; + function backTracking(ticketNum: number, ticketMap: TicketsMap, route: string[]): boolean { + if (route.length === ticketNum + 1) return true; + const targetMap = ticketMap[route[route.length - 1]]; + if (targetMap !== undefined) { + for (const [to, count] of targetMap.entries()) { + if (count > 0) { + route.push(to); + targetMap.set(to, count - 1); + if (backTracking(ticketNum, ticketMap, route) === true) return true; + targetMap.set(to, count); + route.pop(); + } + } + } + return false; + } +}; +``` + ### Swift 直接迭代tickets数组: diff --git a/problems/回溯算法去重问题的另一种写法.md b/problems/回溯算法去重问题的另一种写法.md index b4bdda00..f48097e1 100644 --- a/problems/回溯算法去重问题的另一种写法.md +++ b/problems/回溯算法去重问题的另一种写法.md @@ -365,6 +365,87 @@ class Solution: return res ``` +TypeScript: + +**90.子集II** + +```typescript +function subsetsWithDup(nums: number[]): number[][] { + nums.sort((a, b) => a - b); + const resArr: number[][] = []; + backTraking(nums, 0, []); + return resArr; + function backTraking(nums: number[], startIndex: number, route: number[]): void { + resArr.push(route.slice()); + const helperSet: Set = new Set(); + for (let i = startIndex, length = nums.length; i < length; i++) { + if (helperSet.has(nums[i])) continue; + helperSet.add(nums[i]); + route.push(nums[i]); + backTraking(nums, i + 1, route); + route.pop(); + } + } +}; +``` + +**40. 组合总和 II** + +```typescript +function combinationSum2(candidates: number[], target: number): number[][] { + candidates.sort((a, b) => a - b); + const resArr: number[][] = []; + backTracking(candidates, target, 0, 0, []); + return resArr; + function backTracking( + candidates: number[], target: number, + curSum: number, startIndex: number, route: number[] + ) { + if (curSum > target) return; + if (curSum === target) { + resArr.push(route.slice()); + return; + } + const helperSet: Set = new Set(); + for (let i = startIndex, length = candidates.length; i < length; i++) { + let tempVal: number = candidates[i]; + if (helperSet.has(tempVal)) continue; + helperSet.add(tempVal); + route.push(tempVal); + backTracking(candidates, target, curSum + tempVal, i + 1, route); + route.pop(); + + } + } +}; +``` + +**47. 全排列 II** + +```typescript +function permuteUnique(nums: number[]): number[][] { + const resArr: number[][] = []; + const usedArr: boolean[] = []; + backTracking(nums, []); + return resArr; + function backTracking(nums: number[], route: number[]): void { + if (nums.length === route.length) { + resArr.push(route.slice()); + return; + } + const usedSet: Set = new Set(); + for (let i = 0, length = nums.length; i < length; i++) { + if (usedArr[i] === true || usedSet.has(nums[i])) continue; + usedSet.add(nums[i]); + route.push(nums[i]); + usedArr[i] = true; + backTracking(nums, route); + usedArr[i] = false; + route.pop(); + } + } +}; +``` Go: