mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -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
|
||||
|
@ -240,7 +240,29 @@ var combine = function(n, k) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function combine(n: number, k: number): number[][] {
|
||||
let resArr: number[][] = [];
|
||||
function backTracking(n: number, k: number, startIndex: number, tempArr: number[]): void {
|
||||
if (tempArr.length === k) {
|
||||
resArr.push(tempArr.slice());
|
||||
return;
|
||||
}
|
||||
for (let i = startIndex; i <= n - k + 1 + tempArr.length; i++) {
|
||||
tempArr.push(i);
|
||||
backTracking(n, k, i + 1, tempArr);
|
||||
tempArr.pop();
|
||||
}
|
||||
}
|
||||
backTracking(n, k, 1, []);
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
C:
|
||||
|
||||
```c
|
||||
int* path;
|
||||
int pathTop;
|
||||
|
@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List<String> 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
|
||||
|
@ -239,6 +239,30 @@ class Solution {
|
||||
|
||||
### Python
|
||||
```python
|
||||
# 解法1
|
||||
class Solution:
|
||||
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||
n = len(gas)
|
||||
cur_sum = 0
|
||||
min_sum = float('inf')
|
||||
|
||||
for i in range(n):
|
||||
cur_sum += gas[i] - cost[i]
|
||||
min_sum = min(min_sum, cur_sum)
|
||||
|
||||
if cur_sum < 0: return -1
|
||||
if min_sum >= 0: return 0
|
||||
|
||||
for j in range(n - 1, 0, -1):
|
||||
min_sum += gas[j] - cost[j]
|
||||
if min_sum >= 0:
|
||||
return j
|
||||
|
||||
return -1
|
||||
```
|
||||
|
||||
```python
|
||||
# 解法2
|
||||
class Solution:
|
||||
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
|
||||
start = 0
|
||||
|
@ -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
|
||||
|
@ -222,7 +222,44 @@ public:
|
||||
效率:
|
||||
<img src='https://code-thinking.cdn.bcebos.com/pics/151_翻转字符串里的单词.png' width=600> </img></div>
|
||||
|
||||
```CPP
|
||||
//版本二:
|
||||
//原理同版本1,更简洁实现。
|
||||
class Solution {
|
||||
public:
|
||||
void reverse(string& s, int start, int end){ //翻转,区间写法:闭区间 []
|
||||
for (int i = start, j = end; i < j; i++, j--) {
|
||||
swap(s[i], s[j]);
|
||||
}
|
||||
}
|
||||
|
||||
void removeExtraSpaces(string& s) {//去除所有空格并在相邻单词之间添加空格, 快慢指针。
|
||||
int slow = 0; //整体思想参考Leetcode: 27. 移除元素:https://leetcode-cn.com/problems/remove-element/
|
||||
for (int i = 0; i < s.size(); ++i) { //
|
||||
if (s[i] != ' ') { //遇到非空格就处理,即删除所有空格。
|
||||
if (slow != 0) s[slow++] = ' '; //手动控制空格,给单词之间添加空格。slow != 0说明不是第一个单词,需要在单词前添加空格。
|
||||
while (i < s.size() && s[i] != ' ') { //补上该单词,遇到空格说明单词结束。
|
||||
s[slow++] = s[i++];
|
||||
}
|
||||
}
|
||||
}
|
||||
s.resize(slow); //slow的大小即为去除多余空格后的大小。
|
||||
}
|
||||
|
||||
string reverseWords(string s) {
|
||||
removeExtraSpaces(s); //去除多余空格,保证单词之间之只有一个空格,且字符串首尾没空格。
|
||||
reverse(s, 0, s.size() - 1);
|
||||
int start = 0; //removeExtraSpaces后保证第一个单词的开始下标一定是0。
|
||||
for (int i = 0; i <= s.size(); ++i) {
|
||||
if (i == s.size() || s[i] == ' ') { //到达空格或者串尾,说明一个单词结束。进行翻转。
|
||||
reverse(s, start, i - 1); //翻转,注意是左闭右闭 []的翻转。
|
||||
start = i + 1; //更新下一个单词的开始下标start
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
@ -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<l/2;i++{
|
||||
nums[i],nums[l-1-i]=nums[l-1-i],nums[i]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## JavaScript
|
||||
|
@ -396,6 +396,30 @@ var combinationSum3 = function(k, n) {
|
||||
};
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
```typescript
|
||||
function combinationSum3(k: number, n: number): number[][] {
|
||||
const resArr: number[][] = [];
|
||||
function backTracking(k: number, n: number, sum: number, startIndex: number, tempArr: number[]): void {
|
||||
if (sum > n) return;
|
||||
if (tempArr.length === k) {
|
||||
if (sum === n) {
|
||||
resArr.push(tempArr.slice());
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (let i = startIndex; i <= 9 - (k - tempArr.length) + 1; i++) {
|
||||
tempArr.push(i);
|
||||
backTracking(k, n, sum + i, i + 1, tempArr);
|
||||
tempArr.pop();
|
||||
}
|
||||
}
|
||||
backTracking(k, n, 0, 1, []);
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
## C
|
||||
|
||||
```c
|
||||
|
@ -342,6 +342,64 @@ class Solution:
|
||||
return path
|
||||
```
|
||||
|
||||
### Go
|
||||
```go
|
||||
type pair struct {
|
||||
target string
|
||||
visited bool
|
||||
}
|
||||
type pairs []*pair
|
||||
|
||||
func (p pairs) Len() int {
|
||||
return len(p)
|
||||
}
|
||||
func (p pairs) Swap(i, j int) {
|
||||
p[i], p[j] = p[j], p[i]
|
||||
}
|
||||
func (p pairs) Less(i, j int) bool {
|
||||
return p[i].target < p[j].target
|
||||
}
|
||||
|
||||
func findItinerary(tickets [][]string) []string {
|
||||
result := []string{}
|
||||
// map[出发机场] pair{目的地,是否被访问过}
|
||||
targets := make(map[string]pairs)
|
||||
for _, ticket := range tickets {
|
||||
if targets[ticket[0]] == nil {
|
||||
targets[ticket[0]] = make(pairs, 0)
|
||||
}
|
||||
targets[ticket[0]] = append(targets[ticket[0]], &pair{target: ticket[1], visited: false})
|
||||
}
|
||||
for k, _ := range targets {
|
||||
sort.Sort(targets[k])
|
||||
}
|
||||
result = append(result, "JFK")
|
||||
var backtracking func() bool
|
||||
backtracking = func() bool {
|
||||
if len(tickets)+1 == len(result) {
|
||||
return true
|
||||
}
|
||||
// 取出起飞航班对应的目的地
|
||||
for _, pair := range targets[result[len(result)-1]] {
|
||||
if pair.visited == false {
|
||||
result = append(result, pair.target)
|
||||
pair.visited = true
|
||||
if backtracking() {
|
||||
return true
|
||||
}
|
||||
result = result[:len(result)-1]
|
||||
pair.visited = false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
backtracking()
|
||||
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
### C语言
|
||||
|
||||
```C
|
||||
|
Reference in New Issue
Block a user