Skip to content

Commit dd0fa00

Browse files
committed
2 parents cfb8ab8 + fc98246 commit dd0fa00

39 files changed

+412
-124
lines changed

problems/0017.电话号码的字母组合.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,40 @@ var letterCombinations = function(digits) {
420420
};
421421
```
422422

423+
## TypeScript
424+
425+
```typescript
426+
function letterCombinations(digits: string): string[] {
427+
if (digits === '') return [];
428+
const strMap: { [index: string]: string[] } = {
429+
1: [],
430+
2: ['a', 'b', 'c'],
431+
3: ['d', 'e', 'f'],
432+
4: ['g', 'h', 'i'],
433+
5: ['j', 'k', 'l'],
434+
6: ['m', 'n', 'o'],
435+
7: ['p', 'q', 'r', 's'],
436+
8: ['t', 'u', 'v'],
437+
9: ['w', 'x', 'y', 'z'],
438+
}
439+
const resArr: string[] = [];
440+
function backTracking(digits: string, curIndex: number, route: string[]): void {
441+
if (curIndex === digits.length) {
442+
resArr.push(route.join(''));
443+
return;
444+
}
445+
let tempArr: string[] = strMap[digits[curIndex]];
446+
for (let i = 0, length = tempArr.length; i < length; i++) {
447+
route.push(tempArr[i]);
448+
backTracking(digits, curIndex + 1, route);
449+
route.pop();
450+
}
451+
}
452+
backTracking(digits, 0, []);
453+
return resArr;
454+
};
455+
```
456+
423457
## C
424458

425459
```c

problems/0039.组合总和.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,34 @@ var combinationSum = function(candidates, target) {
392392
};
393393
```
394394

395+
## TypeScript
396+
397+
```typescript
398+
function combinationSum(candidates: number[], target: number): number[][] {
399+
const resArr: number[][] = [];
400+
function backTracking(
401+
candidates: number[], target: number,
402+
startIndex: number, route: number[], curSum: number
403+
): void {
404+
if (curSum > target) return;
405+
if (curSum === target) {
406+
resArr.push(route.slice());
407+
return
408+
}
409+
for (let i = startIndex, length = candidates.length; i < length; i++) {
410+
let tempVal: number = candidates[i];
411+
route.push(tempVal);
412+
backTracking(candidates, target, i, route, curSum + tempVal);
413+
route.pop();
414+
}
415+
}
416+
backTracking(candidates, target, 0, [], 0);
417+
return resArr;
418+
};
419+
```
420+
395421
## C
422+
396423
```c
397424
int* path;
398425
int pathTop;

problems/0040.组合总和II.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ var combinationSum2 = function(candidates, target) {
532532
};
533533
```
534534
**使用used去重**
535+
535536
```js
536537
var combinationSum2 = function(candidates, target) {
537538
let res = [];
@@ -562,6 +563,37 @@ var combinationSum2 = function(candidates, target) {
562563
};
563564
```
564565

566+
## TypeScript
567+
568+
```typescript
569+
function combinationSum2(candidates: number[], target: number): number[][] {
570+
candidates.sort((a, b) => a - b);
571+
const resArr: number[][] = [];
572+
function backTracking(
573+
candidates: number[], target: number,
574+
curSum: number, startIndex: number, route: number[]
575+
) {
576+
if (curSum > target) return;
577+
if (curSum === target) {
578+
resArr.push(route.slice());
579+
return;
580+
}
581+
for (let i = startIndex, length = candidates.length; i < length; i++) {
582+
if (i > startIndex && candidates[i] === candidates[i - 1]) {
583+
continue;
584+
}
585+
let tempVal: number = candidates[i];
586+
route.push(tempVal);
587+
backTracking(candidates, target, curSum + tempVal, i + 1, route);
588+
route.pop();
589+
590+
}
591+
}
592+
backTracking(candidates, target, 0, 0, []);
593+
return resArr;
594+
};
595+
```
596+
565597
## C
566598

567599
```c

problems/0063.不同路径II.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ public:
184184
};
185185
```
186186

187-
* 时间复杂度:$O(n × m)$,n、m 分别为obstacleGrid 长度和宽度
188-
* 空间复杂度:$O(m)$
187+
* 时间复杂度:O(n × m),n、m 分别为obstacleGrid 长度和宽度
188+
* 空间复杂度:O(m)
189189

190190

191191
## 总结

problems/0123.买卖股票的最佳时机III.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public:
148148
};
149149
```
150150
151-
* 时间复杂度:$O(n)$
152-
* 空间复杂度:$O(n × 5)$
151+
* 时间复杂度:O(n)
152+
* 空间复杂度:O(n × 5)
153153
154154
当然,大家可以看到力扣官方题解里的一种优化空间写法,我这里给出对应的C++版本:
155155
@@ -173,8 +173,8 @@ public:
173173
};
174174
```
175175

176-
* 时间复杂度:$O(n)$
177-
* 空间复杂度:$O(1)$
176+
* 时间复杂度:O(n)
177+
* 空间复杂度:O(1)
178178

179179
大家会发现dp[2]利用的是当天的dp[1]。 但结果也是对的。
180180

problems/0127.单词接龙.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,29 @@ public int ladderLength(String beginWord, String endWord, List<String> wordList)
134134
```
135135

136136
## Python
137-
137+
```
138+
class Solution:
139+
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
140+
wordSet = set(wordList)
141+
if len(wordSet)== 0 or endWord not in wordSet:
142+
return 0
143+
mapping = {beginWord:1}
144+
queue = deque([beginWord])
145+
while queue:
146+
word = queue.popleft()
147+
path = mapping[word]
148+
for i in range(len(word)):
149+
word_list = list(word)
150+
for j in range(26):
151+
word_list[i] = chr(ord('a')+j)
152+
newWord = "".join(word_list)
153+
if newWord == endWord:
154+
return path+1
155+
if newWord in wordSet and newWord not in mapping:
156+
mapping[newWord] = path+1
157+
queue.append(newWord)
158+
return 0
159+
```
138160
## Go
139161

140162
## JavaScript

problems/0134.加油站.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ class Solution {
239239

240240
### Python
241241
```python
242+
# 解法1
243+
class Solution:
244+
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
245+
n = len(gas)
246+
cur_sum = 0
247+
min_sum = float('inf')
248+
249+
for i in range(n):
250+
cur_sum += gas[i] - cost[i]
251+
min_sum = min(min_sum, cur_sum)
252+
253+
if cur_sum < 0: return -1
254+
if min_sum >= 0: return 0
255+
256+
for j in range(n - 1, 0, -1):
257+
min_sum += gas[j] - cost[j]
258+
if min_sum >= 0:
259+
return j
260+
261+
return -1
262+
```
263+
264+
```python
265+
# 解法2
242266
class Solution:
243267
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
244268
start = 0

problems/0139.单词拆分.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public:
207207
};
208208
```
209209

210-
* 时间复杂度:O(n^3),因为substr返回子串的副本是$O(n)$的复杂度(这里的n是substring的长度)
210+
* 时间复杂度:O(n^3),因为substr返回子串的副本是O(n)的复杂度(这里的n是substring的长度)
211211
* 空间复杂度:O(n)
212212

213213

problems/0141.环形链表.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ class Solution:
106106
## Go
107107

108108
```go
109+
func hasCycle(head *ListNode) bool {
110+
if head==nil{
111+
return false
112+
} //空链表一定不会有环
113+
fast:=head
114+
slow:=head //快慢指针
115+
for fast.Next!=nil&&fast.Next.Next!=nil{
116+
fast=fast.Next.Next
117+
slow=slow.Next
118+
if fast==slow{
119+
return true //快慢指针相遇则有环
120+
}
121+
}
122+
return false
123+
}
109124
```
110125

111126
### JavaScript

problems/0151.翻转字符串里的单词.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void removeExtraSpaces(string& s) {
7979
8080
逻辑很简单,从前向后遍历,遇到空格了就erase。
8181
82-
如果不仔细琢磨一下erase的时间复杂读,还以为以上的代码是$O(n)$的时间复杂度呢。
82+
如果不仔细琢磨一下erase的时间复杂读,还以为以上的代码是O(n)的时间复杂度呢。
8383
8484
想一下真正的时间复杂度是多少,一个erase本来就是O(n)的操作,erase实现原理题目:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html),最优的算法来移除元素也要O(n)。
8585
@@ -222,7 +222,44 @@ public:
222222
效率:
223223
<img src='https://code-thinking.cdn.bcebos.com/pics/151_翻转字符串里的单词.png' width=600> </img></div>
224224

225+
```CPP
226+
//版本二:
227+
//原理同版本1,更简洁实现。
228+
class Solution {
229+
public:
230+
void reverse(string& s, int start, int end){ //翻转,区间写法:闭区间 []
231+
for (int i = start, j = end; i < j; i++, j--) {
232+
swap(s[i], s[j]);
233+
}
234+
}
235+
236+
void removeExtraSpaces(string& s) {//去除所有空格并在相邻单词之间添加空格, 快慢指针。
237+
int slow = 0; //整体思想参考Leetcode: 27. 移除元素:https://leetcode-cn.com/problems/remove-element/
238+
for (int i = 0; i < s.size(); ++i) { //
239+
if (s[i] != ' ') { //遇到非空格就处理,即删除所有空格。
240+
if (slow != 0) s[slow++] = ' '; //手动控制空格,给单词之间添加空格。slow != 0说明不是第一个单词,需要在单词前添加空格。
241+
while (i < s.size() && s[i] != ' ') { //补上该单词,遇到空格说明单词结束。
242+
s[slow++] = s[i++];
243+
}
244+
}
245+
}
246+
s.resize(slow); //slow的大小即为去除多余空格后的大小。
247+
}
225248

249+
string reverseWords(string s) {
250+
removeExtraSpaces(s); //去除多余空格,保证单词之间之只有一个空格,且字符串首尾没空格。
251+
reverse(s, 0, s.size() - 1);
252+
int start = 0; //removeExtraSpaces后保证第一个单词的开始下标一定是0。
253+
for (int i = 0; i <= s.size(); ++i) {
254+
if (i == s.size() || s[i] == ' ') { //到达空格或者串尾,说明一个单词结束。进行翻转。
255+
reverse(s, start, i - 1); //翻转,注意是左闭右闭 []的翻转。
256+
start = i + 1; //更新下一个单词的开始下标start
257+
}
258+
}
259+
return s;
260+
}
261+
};
262+
```
226263
227264
## 其他语言版本
228265

0 commit comments

Comments
 (0)