diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 7040182f..94136565 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -420,6 +420,40 @@ var letterCombinations = function(digits) { }; ``` +## TypeScript + +```typescript +function letterCombinations(digits: string): string[] { + if (digits === '') return []; + const strMap: { [index: string]: string[] } = { + 1: [], + 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'], + } + const resArr: string[] = []; + function backTracking(digits: string, curIndex: number, route: string[]): void { + if (curIndex === digits.length) { + resArr.push(route.join('')); + return; + } + let tempArr: string[] = strMap[digits[curIndex]]; + for (let i = 0, length = tempArr.length; i < length; i++) { + route.push(tempArr[i]); + backTracking(digits, curIndex + 1, route); + route.pop(); + } + } + backTracking(digits, 0, []); + return resArr; +}; +``` + ## C ```c diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md index 407596c0..584bcb2a 100644 --- a/problems/0127.单词接龙.md +++ b/problems/0127.单词接龙.md @@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List wordList) ``` ## Python - +``` +class Solution: + def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: + wordSet = set(wordList) + if len(wordSet)== 0 or endWord not in wordSet: + return 0 + mapping = {beginWord:1} + queue = deque([beginWord]) + while queue: + word = queue.popleft() + path = mapping[word] + for i in range(len(word)): + word_list = list(word) + for j in range(26): + word_list[i] = chr(ord('a')+j) + newWord = "".join(word_list) + if newWord == endWord: + return path+1 + if newWord in wordSet and newWord not in mapping: + mapping[newWord] = path+1 + queue.append(newWord) + return 0 +``` ## Go ## JavaScript diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md index 0712a2a2..ddd83c94 100644 --- a/problems/0141.环形链表.md +++ b/problems/0141.环形链表.md @@ -106,6 +106,21 @@ class Solution: ## Go ```go +func hasCycle(head *ListNode) bool { + if head==nil{ + return false + } //空链表一定不会有环 + fast:=head + slow:=head //快慢指针 + for fast.Next!=nil&&fast.Next.Next!=nil{ + fast=fast.Next.Next + slow=slow.Next + if fast==slow{ + return true //快慢指针相遇则有环 + } + } + return false +} ``` ### JavaScript diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index 1efe9446..3ffed877 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -124,6 +124,19 @@ class Solution: ## Go ```go +func rotate(nums []int, k int) { + l:=len(nums) + index:=l-k%l + reverse(nums) + reverse(nums[:l-index]) + reverse(nums[l-index:]) +} +func reverse(nums []int){ + l:=len(nums) + for i:=0;i