Skip to content

Commit 543e1f8

Browse files
author
wuduhren
committed
updates
1 parent 1029696 commit 543e1f8

6 files changed

+124
-0
lines changed

problems/python3/add-two-numbers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
3+
carry = 0
4+
dummy = ListNode()
5+
curr = dummy
6+
7+
while l1 or l2 or carry:
8+
n = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
9+
if n>=10:
10+
curr.next = ListNode(n-10)
11+
carry = 1
12+
else:
13+
curr.next = ListNode(n)
14+
carry = 0
15+
curr = curr.next
16+
if l1: l1 = l1.next
17+
if l2: l2 = l2.next
18+
19+
return dummy.next
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findDuplicate(self, nums: List[int]) -> int:
3+
head = nums[0]
4+
slow = head
5+
fast = head
6+
7+
while True:
8+
slow = nums[slow]
9+
fast = nums[nums[fast]]
10+
if slow==fast: break
11+
12+
slow = head
13+
while slow!=fast:
14+
slow = nums[slow]
15+
fast = nums[fast]
16+
return slow
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class KthLargest:
2+
3+
def __init__(self, k: int, nums: List[int]):
4+
self.k = k
5+
self.nums = nums
6+
7+
heapq.heapify(self.nums)
8+
while len(self.nums)>self.k: heapq.heappop(self.nums)
9+
10+
11+
def add(self, val: int) -> int:
12+
heapq.heappush(self.nums, val)
13+
if len(self.nums)>self.k: heapq.heappop(self.nums)
14+
return self.nums[0]

problems/python3/last-stone-weight.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def lastStoneWeight(self, stones: List[int]) -> int:
3+
stones = [-stone for stone in stones]
4+
heapq.heapify(stones)
5+
6+
while len(stones)>=2:
7+
w1 = -heapq.heappop(stones)
8+
w2 = -heapq.heappop(stones)
9+
10+
if w1-w2>0: heapq.heappush(stones, -(w1-w2))
11+
12+
return -stones[0] if stones else 0

problems/python3/linked-list-cycle.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def hasCycle(self, head: Optional[ListNode]) -> bool:
3+
if not head: return False
4+
5+
slow = head
6+
fast = head
7+
8+
while fast:
9+
slow = slow.next
10+
11+
if not fast.next: return False
12+
fast = fast.next.next
13+
14+
if slow==fast: return True
15+
16+
return False

problems/python3/lru-cache.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Node:
2+
def __init__(self, key: int, val: int):
3+
self.key = key
4+
self.val = val
5+
self.next = None
6+
self.prev = None
7+
8+
class LRUCache:
9+
10+
def __init__(self, capacity: int):
11+
self.capacity = capacity
12+
self.dic = {} #[0]
13+
self.head = Node(0, 0) #[3]
14+
self.tail = Node(0, 0) #[3]
15+
self.head.next = self.tail
16+
self.tail.prev = self.head
17+
18+
def remove(self, node):
19+
node.prev.next = node.next
20+
node.next.prev = node.prev
21+
22+
def promote(self, node): #[1]
23+
#set the node next to head
24+
temp = self.head.next
25+
node.next = temp
26+
temp.prev = node
27+
self.head.next = node
28+
node.prev = self.head
29+
30+
def get(self, key: int) -> int:
31+
if key in self.dic:
32+
node = self.dic[key]
33+
self.remove(node)
34+
self.promote(node)
35+
return node.val
36+
return -1
37+
38+
def put(self, key: int, value: int) -> None:
39+
if key in self.dic:
40+
self.remove(self.dic[key])
41+
node = Node(key, value)
42+
self.promote(node)
43+
self.dic[key] = node
44+
45+
if len(self.dic)>self.capacity: #[2]
46+
del self.dic[self.tail.prev.key]
47+
self.remove(self.tail.prev)

0 commit comments

Comments
 (0)