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

242. Valid Anagram

使用List模拟哈希表

在字符数量有限且较小的情况下可以使用List模拟哈希表,本题用一个长度为26的List,基于ASCII表,任意小写字母 - ‘a’ 是一个相对数字。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        hashSet = [0]*26
        # Create 
        for char in s:
            hashSet[ord(char) - ord('a')] += 1
        
        for char in t:
            hashSet[ord(char) - ord('a')] -= 1

        # Evaluate
        for i in range(len(hashSet)):
            if hashSet[i] != 0:
                return False

        return True

使用Python字典

简单粗暴,将每一个字母作为字典的键。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        hashSet = dict()
        # Create 
        for char in s:
            if char not in hashSet:
                hashSet.setdefault(char, 1)
            else:
                hashSet[char] += 1
        
        for char in t:
            if char not in hashSet:
                return False
            else:
                hashSet[char] -= 1
                if hashSet[char] == 0:
                    del hashSet[char]


        # Evaluate
        if hashSet == {}:
            return True
        else:
            return False

使用Python Counter

Counter可以直接对字符串中每个字符计数,并且Counter是一个无序容器,可以直接判断两个Counter是否相等。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import Counter
        counter_s = Counter(s)
        counter_t = Counter(t)
        if counter_s == counter_t:
            return True
        return False

349. Intersection of Two Arrays

Python中的&可以求两个set的交集

Python Set Intersection using Bitwise “&” Operator
The Python “&” Operator also returns the intersection of elements of two or more sets.
The only difference between & operator and intersection() method is that the & operator operates only on set objects while the intersection method can operate on any iterable object like lists, sets, etc.

  • set1 & set2 求set1和set2的交集
  • set1 | set2 求set1和set2的并集
  • set1 - set2 求set1和set2的差集
class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1) & set(nums2))

202. Happy Number

因为题目中说如果不是快乐数,每次的sum就会陷入循环,使用用一个set来保存每次的sum

class Solution:
    def isHappy(self, n: int) -> bool:
        
        def getSum(num):
            sum_ = 0
            while num != 0:
                single_n = num % 10
                num = num // 10
                sum_ += single_n * single_n
            return sum_

        num_set = set()
        while True:
            sum_ = getSum(n)
            if sum_ == 1:
                return True
            if sum_ in num_set:
                return False
            num_set.add(sum_)
            n = sum_
            
                

1. Two Sum

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashSet = dict()
        for i in range(len(nums)):
            if nums[i] not in hashSet:
                hashSet.setdefault(nums[i], i)
            if target-nums[i] in hashSet and hashSet[target-nums[i]] != i:
                return [i, hashSet[target-nums[i]]]

你可能感兴趣的:(算法,leetcode,算法,职场和发展)