【代码随想录day5】有效的字母异位词;两个数组的交集;快乐数;两数之和

242. 有效的字母异位词

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        chars = set(list(s)+list(t))
        for i in chars:
            counts = s.count(i)
            countt = t.count(i)
            if counts!=countt:
                return False
        return True

349. 两个数组的交集

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))

202. 快乐数

class Solution:
    def isHappy(self, n: int) -> bool:
        def cal(num):#算82/68/100/1
            sums = 0
            while num:
                sums += (num % 10)**2
                num = num//10
            return sums
        record = set()#集合
        while True:#无限循环
            n = cal(n)
            if n == 1:
                return True
            if n in record:#出现过啦
                return False
            else:
                record.add(n)

1. 两数之和

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        d = {}
        j = 0
        for i,j in enumerate(nums):
            if target-j in d:
                return d[target-j],i
            else:
                d[j]=i

你可能感兴趣的:(代码随想录,leetcode,算法)