【随想录训练营】Day06 ||

题目列表:

242. 有效的字母异位词 - 力扣(LeetCode)

349. 两个数组的交集 - 力扣(LeetCode)

202. 快乐数 - 力扣(LeetCode)

1. 两数之和 - 力扣(LeetCode)

242. 有效的字母异位词

思路:

  1. 通过哈希表来映射字母的关系,通过记录字母出现的次数来比对

代码:

解法 1:

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        record = [0] * 26
        # ord()函数表示返回的是字符的ASCII值
        for i in s:
            record[ord(i) - ord('a')] += 1

        for i in t:
            record[ord(i) - ord('a')] -= 1

        for i in range(len(record)):
            if record[i] != 0:
                return False
        
        return True

解法 2:

介绍了一个defaultdict的一种解体思路

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import defaultdict
        # 注:defaultdict(),括号里面需要加入int,因为表示在访问不存在的key返回的数据类型,int表示如果访问不存在的key则返回0	
        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

解法 3:

介绍Counter的解题思路

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import Counter
        s_dict = Counter(s)
        t_dict = Counter(t)

        return s_dict==t_dict

349. 两个数组的交集

思路:

  1. 通过字典和集合来统计交集
  2. 通过数组来做哈希表,力扣改了 题目描述和后台测试数据,添加了数据范围,
  • 0 <= nums1[i], nums2[i] <= 1000
  1. 方法三:使用集合的方法

代码:

  1. 方式一:通过字典和集合头来统计交集,集合是无需不重复的
class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        # 字典统计nums1出现的次数
        table = {}
        for num in nums1:
            table[num] = table.get(num,0) + 1

        # 集合,无序,不重复,来统计不重复的结果
        result = set()
        for num in nums2:
            # 如果num在table字典中
            if num in table:
                # 将交集放到集合中
                result.add(num)
                # 删掉字典中已经统计的元素,确保不重复统计
                del table[num]
        # 将集合转化为列表返回结果
        return list(result)
  1. 方式二:通过数组来做哈希表
class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:

        # 通过数组来做哈希表
        count1 = [0] * 1001
        count2 = [0] * 1001

        for i in range(len(nums1)):
            count1[nums1[i]] += 1

        for j in range(len(nums2)):
            count2[nums2[j]] += 1
        
        result = []
        for k in range(1001):
            if count1[k] * count2[k] > 0:
                # 相当于num[x] = k,重复的元素是num里的某个元素
                result.append(k)
        
        return result
  1. 方法三:通过集合的方法
class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        # 利用了集合的无序不重复的特性,再通过 & 的方式取出交集
        return list(set(nums1) & set(nums2))

202. 快乐数

思路:

思路一:通过set集合方法

  1. 通过set的不重复的特点,来记录中间过程,如果出现了重复值就说明不是快乐数,因为陷入了无限的循环中
  2. divmod(n, 10) 表示不断的将每位取出来,得到的结果是商和余数,通过余数的平方来不断记录,知道n=0循环停止

代码:

class Solution:
    def isHappy(self, n: int) -> bool:
        # 用于记录出现的重复数
        record = set()

        while True:
            n = self.get_sum(n)
            if n == 1:
                return True
            
            if n in record:
                return False
            else:
                record.add(n)

            
    def get_sum(self,n: int) -> int:
        new_num = 0
        while n:
            n,r = divmod(n, 10)
            new_num += r ** 2
        return new_num

通过字符串拼接的方式,一个一个的取出元素来解决

class Solution:
    def isHappy(self, n: int) -> bool:
        record = set()

        while n not in record:
            record.add(n)
            new_num = 0
            n_str = str(n)
            for i in n_str:
                new_num += int(i)**2
            if new_num == 1: return True
            else: n = new_num
        return False

1. 两数之和

思路:

  1. 通过map来记录查询过的值和对应的index,通过target - nums[index]的结果来查找,如果查到即返回

代码:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        record = dict()

        for index,value in enumerate(nums):
            if target - nums[index] in record:
                return [record[target - nums[index]], index]
            record[nums[index]] = index
        
        return []

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值