LeetCode Day06 | 242. 有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和

文章介绍了使用Python实现几种常见的算法问题,包括判断两个字符串是否为字母异位词(使用哈希表或排序),找到两个整数数组的交集(利用哈希集合),以及判断一个数是否为快乐数(哈希集合或快慢指针法)。此外,还提到了两数之和问题的哈希表解决方案。
摘要由CSDN通过智能技术生成

242. 有效的字母异位词

难度:☆1

a. 哈希表-数组

用一个长度为 26 的数组作为哈希表,记录字符串 s 中每个字母出现的次数,从 a 到 z。再遍历字符串 t,将出现的每个字母对应的计数减 1,判断最终数组元素是否全部归零。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        alphabet_list = [0] * 26
        for char in s:
            alphabet_list[ord(char) - ord('a')] += 1
        for char in t:
            alphabet_list[ord(char) - ord('a')] -= 1
        return True if alphabet_list == [0] * 26 else False

b. 哈希表-defaultdict

用 defaultdict 作为哈希表。Python Doc: defaultdict 返回一个新的类似字典的对象。 [defaultdict](https://docs.python.org/zh-cn/3/library/collections.html?highlight=collections#collections.defaultdict) 是内置 [dict](https://docs.python.org/zh-cn/3/library/stdtypes.html#dict) 类的子类。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        s_dict = defaultdict(int)
        t_dict = defaultdict(int)
        for i in s:
            s_dict[i] += 1        
        for j in t:
            t_dict[j] += 1
        return s_dict == t_dict

c. 排序

暴力解法。先对 s 和 t 分别排序,再比较是否相同。不推荐,因为失去了算法考察的意义。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return sorted(s) == sorted(t)

349. 两个数组的交集

难度:☆1

a. 哈希集合1

Python 用 set() 实现,去重之后取交集,再以列表输出。

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        set1 = set()
        set2 = set()
        result = list()
        for num1 in nums1:
            set1.add(num1)
        for num2 in nums2:
            set2.add(num2)
        for element in set1:
            if element in set2:
                result.append(element)
        return result

b. Python 一行输出

或者用语法特性,一行输出。

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))  # 两个数组先变成集合,求交集后还原为数组

c. 哈希集合2

用一个 set 解决:把第一个数组放进去,再遍历第二个数组检查 set 和结果数组是否出现过,放入结果数组。

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        set1 = set()
        result = list()
        for num1 in nums1:
            set1.add(num1)
        for num2 in nums2:
            if num2 in set1 and num2 not in result:
                result.append(num2)
        return result

也可以先排序,再对两个数组进行双指针遍历,找出相同元素放入answer数组(列表),注意条件判断。

Python Doc: set() 的各种操作,子集、真子集、超集、真超集、交集、并集、集合求差。

202. 快乐数

难度:☆3

a. 哈希集合

最坏的情况下,算法可能会在 243 以下的所有数字上循环,然后回到它已经到过的一个循环或者回到 1(证明见官方题解)。做数位分离,求平方和。每次生成链中的下一个数字时,都检查它是否已经在哈希集合中。如果它不在哈希集合中,添加它。如果它在,说明正处于一个循环中,返回 false。单独写一个 get_next(n) 函数用来计算平方和更加方便。分析时间复杂度比较困难,见题解。

class Solution:
    def isHappy(self, n: int) -> bool:
        seen = set()
        while (n != 1) and (n not in seen):
            seen.add(n)            
            total_sum = 0
            while n > 0:
                digit = n % 10
                n //= 10
                total_sum += digit ** 2
            n = total_sum
        return n == 1

b. 快慢指针法

还有快慢指针法,经常用于检测一个链表是否有环。

class Solution:
    def isHappy(self, n: int) -> bool:
        def get_next(number):
            total_sum = 0
            while number > 0:
                number, digit = divmod(number, 10)
                total_sum += digit ** 2
            return total_sum

        slow_runner = n
        fast_runner = get_next(n)
        while fast_runner != 1 and slow_runner != fast_runner:
            slow_runner = get_next(slow_runner)
            fast_runner = get_next(get_next(fast_runner))
        return fast_runner == 1

1. 两数之和

难度:☆2

哈希表。创建一个哈希表,对于每一个 x,查询哈希表中是否存在 target - x,没有则将 x 插入到哈希表中,即可保证不会让 x 和自己匹配。用字典 dict(),选用 enumerate。

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        record = {}
        for i in range(len(nums)):
            rest = target - nums[i]
            if record.get(rest) is not None: 
                return [record[rest], i]
            record[nums[i]] = i
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        record = {}
        for idx, val in enumerate(nums):
            if target - val not in record.keys():
                record[val] = idx
            else:
                return [record[target-val], idx]

也可以创建一个哈希表,对于每一个 target - x,查询哈希表中是否存在 x,没有则将 target - x 插入到哈希表中。

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashTable = dict()
        index = 0
        while index < len(nums):
            if nums[index] not in hashTable:
                hashTable[target-nums[index]] = index
                index += 1
            else:
                return [hashTable[nums[index]], index]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值