def rotate_in_place(a: np.array): """ Rotate for N*N array, the result is equivalent to numpy.rot90(a, 3) :array: N*N numpy array :return: rotated numpy array Complexity: Time : O(rc) Space : O(rc) """ # 4-way edge swap if not a.all(): raise ArgError() row_num, col_num = a.shape if row_num != col_num: raise ArgError("only support N*N array") dim = row_num layers = dim // 2 for layer in range(layers): start = layer end = dim - 1 - layer for offset in range(end - start): tmp = a[start, start + offset] a[start, start + offset] = a[end - offset, start] a[end - offset, start] = a[end, end - offset] a[end, end - offset] = a[start + offset, end] a[start + offset, end] = tmp return a
def is_permutation(s1, s2, *, case_sensitive=False): """ Determine if str1 is the permutation of str2 Complexity: Time : O(n) Space : O(c) """ if not s1 or not s2: raise ArgError() if len(s1) != len(s2): return False if not case_sensitive: s1, s2 = s1.lower(), s2.lower() m = collections.defaultdict(int) for c in s1: m[c] += 1 for c in s2: m[c] -= 1 if m[c] < 0: return False return True
def rotate_in_place_v2(a): """ Rotate for N*N array, the result is equivalent to numpy.rot90(a, 3) :array: non np array :return: rotated array Complexity: Time : O(rc) Space : O(rc) """ row_num = len(a) col_num = len(a[0]) if row_num != col_num: raise ArgError() dim = row_num layers = row_num // 2 for layer in range(layers): start = layer end = dim - 1 - layer for offset in range(end - start): tmp = a[start][start + offset] a[start][start + offset] = a[end - offset][start] a[end - offset][start] = a[end][end - offset] a[end][end - offset] = a[start + offset][end] a[start + offset][end] = tmp return a
def replace_spaces(s, *, pattern="%20"): """ In fact, s.replace(" ", %20) can solve this issue Complexity: Time : O(n) Space : O(n) """ if not s: raise ArgError() buf = list() for c in s: if c == " ": buf.append(pattern) else: buf.append(c) return "".join(buf)
def rotate(a: np.array, *, dtype=int): """ Rotate for M*N array, this is equivalent to numpy.rot90(a, 3) :array: M*N numpy array :return: rotated numpy array Complexity: Time : O(rc) Space : O(rc) """ if not a.all(): raise ArgError() row_num, col_num = a.shape[::-1] rotated_array = np.empty(shape=[row_num, col_num], dtype=dtype) for index, v in np.ndenumerate(a): r, c = index rotated_array[c, col_num - 1 - r] = v return rotated_array
def is_palindrome_permutation(s): """ Determine if s is a palindrome permutation Complexity: Time : O(n) Space : O(n) """ if not s: raise ArgError() if len(s) == 1: return True odd_cnt = 0 m = collections.defaultdict(int) for c in s: m[c] += 1 if m[c] % 2 == 0: odd_cnt -= 1 else: odd_cnt += 1 return odd_cnt <= 1
def is_palindrome(s): """ Determine if s is a palindrome Complexity: Time : O(n) Space : O(n) """ if not s: raise ArgError() if len(s) == 1: return True start, end = 0, len(s) - 1 while start < end: if s[start] == s[end]: start += 1 end -= 1 else: return False return True
def is_unique_char(s, *, case_sensitive=False): """ Determine if str has all unique characters Complexity: Time : O(n) Space : O(C) """ if not s: raise ArgError() if not case_sensitive: s = s.lower() m = dict() for c in s: if not m.get(c, None): m[c] = True continue else: # duplicated return False return True