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
posted @ 2024-09-08 01:59  WindMay  阅读(15)  评论(0)    收藏  举报