From 4959bd4c8aa78d401f5a1f14c2d267a4d046baf5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 28 Mar 2022 17:15:46 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880039.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8C.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index 7a2084dd..98b37b84 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -392,7 +392,34 @@ var combinationSum = function(candidates, target) { }; ``` +## TypeScript + +```typescript +function combinationSum(candidates: number[], target: number): number[][] { + const resArr: number[][] = []; + function backTracking( + candidates: number[], target: number, + startIndex: number, route: number[], curSum: number + ): void { + if (curSum > target) return; + if (curSum === target) { + resArr.push(route.slice()); + return + } + for (let i = startIndex, length = candidates.length; i < length; i++) { + let tempVal: number = candidates[i]; + route.push(tempVal); + backTracking(candidates, target, i, route, curSum + tempVal); + route.pop(); + } + } + backTracking(candidates, target, 0, [], 0); + return resArr; +}; +``` + ## C + ```c int* path; int pathTop; From a021470215aa859a4d5b0e9fe48dd775e6c0ed51 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 29 Mar 2022 14:38:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880040.=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=80=BB=E5=92=8CII.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0040.组合总和II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0040.组合总和II.md b/problems/0040.组合总和II.md index 49acb8d6..de13e031 100644 --- a/problems/0040.组合总和II.md +++ b/problems/0040.组合总和II.md @@ -532,6 +532,7 @@ var combinationSum2 = function(candidates, target) { }; ``` **使用used去重** + ```js var combinationSum2 = function(candidates, target) { let res = []; @@ -562,6 +563,37 @@ var combinationSum2 = function(candidates, target) { }; ``` +## TypeScript + +```typescript +function combinationSum2(candidates: number[], target: number): number[][] { + candidates.sort((a, b) => a - b); + const resArr: number[][] = []; + function backTracking( + candidates: number[], target: number, + curSum: number, startIndex: number, route: number[] + ) { + if (curSum > target) return; + if (curSum === target) { + resArr.push(route.slice()); + return; + } + for (let i = startIndex, length = candidates.length; i < length; i++) { + if (i > startIndex && candidates[i] === candidates[i - 1]) { + continue; + } + let tempVal: number = candidates[i]; + route.push(tempVal); + backTracking(candidates, target, curSum + tempVal, i + 1, route); + route.pop(); + + } + } + backTracking(candidates, target, 0, 0, []); + return resArr; +}; +``` + ## C ```c From c569dc505b59552f1435cecc1b584d88417a4cac Mon Sep 17 00:00:00 2001 From: zhujs Date: Tue, 29 Mar 2022 23:33:34 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200332.=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=AE=89=E6=8E=92=E8=A1=8C=E7=A8=8B=20Go=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0332.重新安排行程.md | 72 +++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/problems/0332.重新安排行程.md b/problems/0332.重新安排行程.md index 01f81c4d..e65786c6 100644 --- a/problems/0332.重新安排行程.md +++ b/problems/0332.重新安排行程.md @@ -568,5 +568,77 @@ for line in tickets { } ``` +### Go +```Go + +// 先排序,然后找到第一条路径即可返回 +func findItinerary(tickets [][]string) []string { + var path []string // 用来保存搜索的路径 + data := make(map[string]ticketSlice) // 用来保存tickets排序后的结果 + + var search func(airport string) bool + search = func(airport string) bool { + if len(path) == len(tickets) { + path = append(path, airport) + return true + } + to := data[airport] + for _, item := range to { + if item.Count == 0 { + // 已用完 + continue + } + + path = append(path, airport) + item.Count-- + if search(item.To) { return true } + item.Count++ + path = path[:len(path) - 1] + } + + return false + } + + // 排序 + // 感觉这段代码有点啰嗦,不知道能不能简化一下 + tmp := make(map[string]map[string]int) + for _, ticket := range tickets { + if to, ok := tmp[ticket[0]]; ok { + if _, ok2 := to[ticket[1]]; ok2 { + to[ticket[1]]++ + } else { + to[ticket[1]] = 1 + } + } else { + tmp[ticket[0]] = map[string]int{ + ticket[1]: 1, + } + } + } + for from, to := range tmp { + var tmp ticketSlice + for to, num := range to { + tmp = append(tmp, &ticketStat{To: to, Count: num}) + } + sort.Sort(tmp) + data[from] = tmp + } + + search("JFK") + return path +} + +type ticketStat struct { + To string + Count int +} +type ticketSlice []*ticketStat + +func (p ticketSlice) Len() int { return len(p) } +func (p ticketSlice) Less(i, j int) bool { return strings.Compare(p[i].To, p[j].To) == -1 } +func (p ticketSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +``` + -----------------------