1. 字母异位词分组
题目链接:https://leetcode.cn/problems/group-anagrams/
给定一个字符串数组,将 字母异位词 组合在一起。
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)
题目链接:https://leetcode.cn/problems/powx-n/
实现 pow(x, n) ,即计算 x 的整数 n 次幂函数。
class Solution:def myPow(self, x: float, n: int) -> float:if n == 0:return 1if n < 0:x = 1 / xn = -nres = 1while n:if n & 1:res *= xx *= xn >>= 1return res