Skip to content

Commit

Permalink
Merge pull request algorithm004-05#1083 from 1181406961/master
Browse files Browse the repository at this point in the history
525-Week 07
  • Loading branch information
melody-li authored Dec 4, 2019
2 parents 148a4b9 + 535a15b commit 2f48cf3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Week 7/id_525/LeetCode_1122_525.py
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
66 changes: 66 additions & 0 deletions Week 7/id_525/LeetCode_146_525.py
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)
8 changes: 8 additions & 0 deletions Week 7/id_525/LeetCode_242_525.py
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)
20 changes: 20 additions & 0 deletions Week 7/id_525/LeetCode_56_525.py
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

0 comments on commit 2f48cf3

Please sign in to comment.