File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ TC: O(n)
3+ SC: O(n)
4+ """
5+ class Solution :
6+ def lengthOfLongestSubstring (self , s : str ) -> int :
7+ str_list = []
8+ max_length = 0
9+ for i in range (len (s )):
10+ if s [i ] not in str_list :
11+ str_list .append (s [i ])
12+ else :
13+ if max_length < len (str_list ):
14+ max_length = len (str_list )
15+ str_list = str_list [str_list .index (s [i ])+ 1 :]
16+ str_list .append (s [i ])
17+
18+ if max_length < len (str_list ):
19+ max_length = len (str_list )
20+
21+ return max_length
22+
Original file line number Diff line number Diff line change 1+ """
2+ TC: O(m*n)
3+ SC: O(m*n)
4+ """
5+ class Solution :
6+ def numIslands (self , grid : List [List [str ]]) -> int :
7+ def dfs (i , j ):
8+ if i < 0 or j < 0 or i >= len (grid ) or j >= len (grid [0 ]) or grid [i ][j ] == "-1" or grid [i ][j ] == "0" :
9+ return
10+
11+ grid [i ][j ] = "-1"
12+ dfs (i + 1 , j )
13+ dfs (i , j + 1 )
14+ dfs (i - 1 , j )
15+ dfs (i , j - 1 )
16+
17+ if not grid :
18+ return 0
19+
20+ count = 0
21+ for i in range (len (grid )):
22+ for j in range (len (grid [0 ])):
23+ if grid [i ][j ] == "1" :
24+ count += 1
25+ dfs (i , j )
26+ return count
Original file line number Diff line number Diff line change 1+ """
2+ TC: O(n)
3+ SC: O(1)
4+
5+ """
6+
7+ # Definition for singly-linked list.
8+ # class ListNode:
9+ # def __init__(self, val=0, next=None):
10+ # self.val = val
11+ # self.next = next
12+ class Solution :
13+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
14+ node , prev = head , None
15+
16+ while node :
17+ next , node .next = node .next , prev
18+ prev , node = node , next
19+
20+ return prev
You can’t perform that action at this time.
0 commit comments