LC200 Number of Islands

```python class Solution: def numIslands(self, grid: List[List[str]]) -> int: # time O(MN) space O(H) worst case O(NM) for grid filled with lands # edge case if not grid: return 0 cnt = 0 for i in range(len(grid)): for j in range(len(grid[0])): # loop each cell to find cell == 1... [Read More]

61 Rotate List

```python Definition for singly-linked list. class ListNode: def init(self, val=0, next=None): self.val = val self.next = next class Solution: time O(2N) space O(1) ””” main idea:connect tail with head to a circle and get the length of LList. find tail and set tail.next to None to cut off the connection... [Read More]

LC18 4Sum

```python class Solution: “”” main idea: for sorted arr, convert general N sum to two pointers + backtracking; base case: two sum, termination condition: check if arr[l] + arr[r] == target arr size == 2 ; general case: N sum """ # time O(N^3) space O(N) # main idea: use... [Read More]

8 String to Integer (atoi)

```python class Solution: def myAtoi(self, s: str) -> int: if len(s) == 0 : return 0 ls = list(s.strip()) if len(ls) == 0:return 0 sign = -1 if ls[0] == ‘-‘ else 1 if ls[0] in [’-‘,’+’] : del ls[0] ret, i = 0, 0 while i < len(ls) and... [Read More]
Tags: Math String