Skip to content

Commit 7e129ba

Browse files
authored
Merge pull request #1906 from devyejin/main
[devyejin] WEEK 08 solutions
2 parents b4eb4ca + 5b01c43 commit 7e129ba

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# space complexity O(n), time complexity O(1)
2+
class Solution:
3+
def lengthOfLongestSubstring(self, s: str) -> int:
4+
seen = {}
5+
left = 0
6+
max_length = 0
7+
8+
for right, char in enumerate(s):
9+
if char in seen and seen[char] >= left:
10+
left = seen[char] + 1
11+
seen[char] = right
12+
max_length = max(max_length, right - left + 1)
13+
14+
return max_length

โ€Žreverse-bits/devyejin.pyโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# time complexity O(1)
2+
class Solution:
3+
def reverseBits(self, n: int) -> int:
4+
binary = bin(n)[2:].zfill(32)
5+
reversed_binary = binary[::-1]
6+
return int(reversed_binary, 2)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Definition for singly-linked list.
2+
from typing import Optional
3+
4+
5+
class ListNode:
6+
def __init__(self, val=0, next=None):
7+
self.val = val
8+
self.next = next
9+
10+
# time complexity O(n), space complexity O(1)
11+
class Solution:
12+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
13+
prev = None # ๋’ค์ง‘ํžŒ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋…ธ๋“œ
14+
curr = head # ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘์ธ ๋…ธ๋“œ
15+
16+
while curr:
17+
temp = curr.next # ๋‹ค์Œ ๋…ธ๋“œ ๊ธฐ์–ต
18+
curr.next = prev
19+
# ๋‹ค์Œ ํฌ์ธํ„ฐ๋กœ ์ด๋™ํ•˜๊ธฐ ์ „ ํ˜„์žฌ๋…ธ๋“œ๋ฅผ ์ด์ „ ๋…ธ๋“œ๋กœ ๋ณ€๊ฒฝ
20+
prev = curr
21+
# ํฌ์ธํ„ฐ ์ด๋™
22+
curr = temp
23+
24+
return prev
25+
26+
# time complexity O(n), space complexity O(n)
27+
# class Solution:
28+
# def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
29+
# stack = []
30+
# while head:
31+
# stack.append(head)
32+
# head = head.next
33+
#
34+
# dummy = ListNode()
35+
# curr = dummy
36+
# while stack:
37+
# node = stack.pop()
38+
# node.next = None
39+
# curr.next = node
40+
# curr = curr.next
41+
#
42+
# return dummy.next

0 commit comments

Comments
ย (0)