forked from algorithm004-05/algorithm004-05
-
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.
Merge pull request algorithm004-05#1083 from 1181406961/master
525-Week 07
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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,20 @@ | ||
class Solution(object): | ||
def relativeSortArray(self, arr1, arr2): | ||
""" | ||
:type arr1: List[int] | ||
:type arr2: List[int] | ||
:rtype: List[int] | ||
""" | ||
arr = [0 for _ in range(1001)] | ||
res = [] | ||
for i in arr1: | ||
arr[i] +=1 | ||
for i in arr2: | ||
while arr[i] > 0: | ||
res.append(i) | ||
arr[i] -=1 | ||
for i in range(len(arr)): | ||
while arr[i] > 0: | ||
res.append(i) | ||
arr[i] -=1 | ||
return res |
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,66 @@ | ||
class Node(object): | ||
def __init__(self, key, value): | ||
self.key = key | ||
self.value = value | ||
self.pre = None | ||
self.next = None | ||
|
||
|
||
class LRUCache(object): | ||
|
||
def __init__(self, capacity): | ||
""" | ||
:type capacity: int | ||
""" | ||
self.capacity = capacity | ||
self.hkeys = {} | ||
# 设置一个头尾指针,和尾指针 | ||
self.head = Node(None, -1) | ||
self.tail = Node(None, -1) | ||
self.size = 0 | ||
self.head.next, self.tail.pre = self.tail, self.head | ||
|
||
def get(self, key): | ||
""" | ||
:type key: int | ||
:rtype: int | ||
""" | ||
|
||
if key not in self.hkeys: | ||
return -1 | ||
cur = self.hkeys[key] | ||
cur.pre.next, cur.next.pre = cur.next, cur.pre | ||
node = self.head.next | ||
self.head.next, cur.pre, cur.next, node.pre = cur, self.head, self.head.next, cur | ||
return self.hkeys.get(key).value | ||
|
||
def put(self, key, value): | ||
""" | ||
:type key: int | ||
:type value: int | ||
:rtype: None | ||
""" | ||
# 存在时修改值,并放到头的位置 | ||
if key in self.hkeys: | ||
cur = self.hkeys[key] | ||
cur.value = value | ||
cur.pre.next, cur.next.pre = cur.next, cur.pre | ||
node = self.head.next | ||
self.head.next, cur.pre, cur.next, node.pre = cur, self.head, node, cur | ||
else: | ||
if self.size == self.capacity: | ||
tail = self.tail.pre | ||
self.hkeys.pop(tail.key) | ||
tail.pre.next, tail.next.pre = tail.next, tail.pre | ||
self.size -= 1 | ||
cur = Node(key, value) | ||
self.hkeys[key] = cur | ||
node = self.head.next | ||
self.head.next, cur.next, cur.pre, node.pre = cur, node, self.head, cur | ||
self.size += 1 | ||
# 删除最后一个元素 | ||
|
||
# Your LRUCache object will be instantiated and called as such: | ||
# obj = LRUCache(capacity) | ||
# param_1 = obj.get(key) | ||
# obj.put(key,value) |
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,8 @@ | ||
class Solution(object): | ||
def isAnagram(self, s, t): | ||
""" | ||
:type s: str | ||
:type t: str | ||
:rtype: bool | ||
""" | ||
return sorted(s) == sorted(t) |
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,20 @@ | ||
class Solution(object): | ||
def merge(self, intervals): | ||
""" | ||
:type intervals: List[List[int]] | ||
:rtype: List[List[int]] | ||
""" | ||
intervals.sort(key=lambda x: x[0]) | ||
n = len(intervals) | ||
i = 0 | ||
res = [] | ||
while i < n: | ||
left = intervals[i][0] | ||
right = intervals[i][1] | ||
while i < n - 1 and intervals[i + 1][0] <= right: | ||
right = max(right, intervals[i + 1][1]) | ||
i += 1 | ||
res.append([left, right]) | ||
i += 1 | ||
return res | ||
|