Study Plan For Algorithms - Part25
1. 字母异位词分组
给定一个字符串数组,将 字母异位词 组合在一起。
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hashmap = {}
for s in strs:
sorted_s = ''.join(sorted(s))
if sorted_s in hashmap:
hashmap[sorted_s].append(s)
else:
hashmap[sorted_s] = [s]
return list(hashmap.values())
2. Pow(x, n)
实现 pow(x, n) ,即计算 x 的整数 n 次幂函数。
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
x = 1 / x
n = -n
res = 1
while n:
if n & 1:
res *= x
x *= x
n >>= 1
return res
本文来自博客园,作者:WindMay,转载请注明原文链接:https://www.cnblogs.com/stephenxiong001/p/18402407

浙公网安备 33010602011771号