LC46 Permutaions

recursive solution ```python time O(N*N!) space O(H) H recursion depth class Solution: def permute(self, nums: List[int]) -> List[List[int]]: self.size = len(nums) self.res = [] self._backtrack(nums,0) return self.res def _backtrack(self,arr,start): if start == self.size: self.res.append(arr[:]) for i in range(start,self.size): arr[start],arr[i] = arr[i], arr[start] self._backtrack(arr,start+1) arr[start],arr[i] = arr[i], arr[start] [Read More]
Tags: Backtracking

LC98 Validate Binary Search Tree

recursive approach # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: # time O(N) space O(H) # main idea: find cases that not meet valid criteria at each node... [Read More]
Tags: Tree DFS

LC205 Isomorphic Strings

refer to Leetcode 290 ```python class Solution: def isIsomorphic(self, s: str, t: str) -> bool: # time O(N) space O(2K) : unique char d1 = {} d2 = {} for i in range(len(s)): if s[i] not in d1: if t[i] not in d2: d1[s[i]] = t[i] d2[t[i]] = s[i] else:... [Read More]
Tags: HashTable

LC143 Reorder List

# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reorderList(self, head: ListNode) -> None: """ tc O(N) sc O(1) Do not return anything, modify head in-place instead. main idea: second half of LList reverse zigzag... [Read More]
Tags: LinkedList