Merge branch 'youngyangyang04:master' into remote

This commit is contained in:
Arthur Pan
2022-04-12 10:42:29 +01:00
committed by GitHub
4 changed files with 85 additions and 1 deletions

View File

@ -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
```c ```c

View File

@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List<String> wordList)
``` ```
## Python ## 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 ## Go
## JavaScript ## JavaScript

View File

@ -106,6 +106,21 @@ class Solution:
## Go ## Go
```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 ### JavaScript

View File

@ -124,6 +124,19 @@ class Solution:
## Go ## Go
```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<l/2;i++{
nums[i],nums[l-1-i]=nums[l-1-i],nums[i]
}
}
``` ```
## JavaScript ## JavaScript