forked from apachecn/apachecn-algo-zh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 856dfab
Showing
250 changed files
with
16,931 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
###1. Two Sum | ||
|
||
题目: | ||
<https://leetcode.com/problems/two-sum/> | ||
|
||
|
||
难度: | ||
|
||
Easy | ||
|
||
|
||
思路 | ||
|
||
可以用O(n^2) loop | ||
|
||
但是也可以牺牲空间换取时间,异常聪明的AC解法 | ||
|
||
``` | ||
2 7 11 15 | ||
不存在 存在之中 | ||
lookup {2:0} [0,1] | ||
``` | ||
|
||
一点字典有了这个 `target - 当前数字`,找到它的index和当前index一起返回。 | ||
|
||
|
||
``` | ||
class Solution(object): | ||
def twoSum(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
lookup = {} | ||
for i, num in enumerate(nums): | ||
if target - num in lookup: | ||
return [lookup[target - num],i] | ||
lookup[num] = i | ||
return [] | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
###2. Add Two Numbers | ||
|
||
题目: | ||
<https://leetcode.com/problems/add-two-numbers/> | ||
|
||
|
||
难度 : Medium | ||
|
||
|
||
跟plus One, add Binary 玩的同一种花样 | ||
|
||
|
||
``` | ||
class Solution(object): | ||
def addTwoNumbers(self, l1, l2): | ||
""" | ||
:type l1: ListNode | ||
:type l2: ListNode | ||
:rtype: ListNode | ||
""" | ||
#easiest case | ||
if l1 == None: | ||
return l2 | ||
if l2 == None: | ||
return l1 | ||
if l1.val + l2.val < 10: | ||
l3 = ListNode(l1.val + l2.val) | ||
l3.next = self.addTwoNumbers(l1.next, l2.next) | ||
elif l1.val + l2.val >= 10: | ||
l3 = ListNode(l1.val + l2.val - 10) | ||
tmp = ListNode(1) | ||
tmp.next = None | ||
l3.next = self.addTwoNumbers(l1.next, self.addTwoNumbers(l2.next ,tmp)) | ||
return l3 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
###3. Longest Substring Without Repeating Characters | ||
|
||
|
||
题目: | ||
<https://leetcode.com/problems/longest-substring-without-repeating-characters/> | ||
|
||
|
||
难度: | ||
|
||
Medium | ||
|
||
|
||
|
||
思路 | ||
|
||
粗一看是dp,细一看是greedy | ||
|
||
|
||
idx 0 1 2 3 4 5 6 7 | ||
a b c a b c b b | ||
rnd ↑ stop↑ | ||
↑ stop↑ | ||
↑ stop↑ | ||
|
||
|
||
因为其实只要每次记录下重复开始的位置,就可以解决问题。 | ||
|
||
|
||
注意最后还有一个 n - start 和 l 来比较,这相当于是最后一个round | ||
|
||
|
||
如果当前重复的这个s[i]取值是限定大于start,就是在start之后再出现重复 | ||
|
||
|
||
``` | ||
class Solution(object): | ||
def lengthOfLongestSubstring(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
n = len(s) | ||
l = 0 | ||
maps = {} | ||
start = 0 | ||
for i in range(n): | ||
if maps.get(s[i],-1) >= start: | ||
l = max(i - start, l) | ||
start = maps.get(s[i]) + 1 | ||
print start | ||
maps[s[i]] = i | ||
return max(n - start, l) | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.