leetcode刷题笔记 task3 查找1

leetcode #35 搜索插入位置

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        if len(nums) == 0:
            return 0
        left = 0
        right = len(nums) - 1
        if target > nums[-1]:
            return len(nums)
        if target < nums[0]:
            return 0
        while left <= right:
            mid = (left + right)//2
            if nums[mid] == target:
                return mid
            elif nums[mid] > target:
                right = mid - 1
            else:
                left = mid + 1
        return left

leetcode #202快乐数

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

        def get_next(n):
            total_sum = 0
            while n > 0:
                n, digit = divmod(n, 10)
                total_sum += digit ** 2
            return total_sum

        seen = set()
        while n != 1 and n not in seen:
            seen.add(n)
            n = get_next(n)

        return n == 1

leetcode #205 同构字符串

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        if len(set(s)) != len(set(t)):
            return False
        dic_s = dict()
        lst_s = []
        dic_t = dict()
        lst_t = []
        new = 1
        for char in s:
            if char not in dic_s:
                dic_s.setdefault(char,new)
                lst_s.append(new)
                new += 1
            else:
                lst_s.append(dic_s[char])
        new = 1
        for char in t:
            if char not in dic_t:
                dic_t.setdefault(char,new)
                lst_t.append(new)
                new += 1
            else:
                lst_t.append(dic_t[char])
        
        if lst_s == lst_t:
            return True
        else:
            return False

leetcode #242 有效的字母异位词

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        dic_s = dict()
        dic_t = dict()
        for char in s:
            dic_s.setdefault(char,0)
            dic_s[char] += 1

        for char in t:
            dic_t.setdefault(char,0)
            dic_t[char] += 1

        if dic_s == dic_t:
            return True
        else:
            return False

leetcode #290 单词规律

class Solution:
    def wordPattern(self, pattern: str, str: str) -> bool:
        s_lst = str.split(' ')
        dic_p = dict()
        dic_s = dict()
        lst_s = []
        lst_p = []
        new = 1
        for char in pattern:
            if char not in dic_p:
                dic_p.setdefault(char, new)
                lst_p.append(new)
                new += 1
            else:
                lst_p.append(dic_p[char])
        new = 1
        for char in s_lst:
            if char not in dic_s:
                dic_s.setdefault(char, new)
                lst_s.append(new)
                new += 1
            else:
                lst_s.append(dic_s[char])

        if lst_p == lst_s:
            return True
        else:
            return False

leetcode #349 两个数组的交集

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        lst = []
        nums1 = set(nums1)
        nums2 = set(nums2)
        for val in nums1:
            if val in nums2:
                lst.append(val)

        return lst

leetcode #350 两个数组的交集2

class Solution:
    def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
        if len(nums1) > len(nums2):
            return self.intersect(nums2, nums1)
        
        m = collections.Counter()
        for num in nums1:
            m[num] += 1
        
        intersection = list()
        for num in nums2:
            if (count := m.get(num, 0)) > 0:
                intersection.append(num)
                m[num] -= 1
                if m[num] == 0:
                    m.pop(num)
        
        return intersection

leetcode #410 分割数组的最大值

class Solution:
    def splitArray(self, nums: List[int], m: int) -> int:
        def check(x: int) -> bool:
            total, cnt = 0, 1
            for num in nums:
                if total + num > x:
                    cnt += 1
                    total = num
                else:
                    total += num
            return cnt <= m


        left = max(nums)
        right = sum(nums)
        while left < right:
            mid = (left + right) // 2
            if check(mid):
                right = mid
            else:
                left = mid + 1

        return left

leetcode #451 根据字符出现次数排序

class Solution:
    def frequencySort(self, s: str) -> str:
        set_s = set(s)
        lst = []
        for char in set_s:
            count = 0
            for i in s:
                if char == i:
                    count += 1
            lst.append((count, char))
        lst = sorted(lst, key = lambda x:x[0], reverse = True)
        res = ''
        for count, char in lst:
            res += char*count
        return res

leetcode #540 有序数组中的单一元素

class Solution:
    def singleNonDuplicate(self, nums: List[int]) -> int:
        def rec(left, right):
            if left == right:
                return nums[left]
            mid = (left + right)//2
            if nums[mid] != nums[mid-1] and nums[mid] != nums[mid+1]:
                return nums[mid]
            if nums[mid] == nums[mid - 1]:
                if (mid-1-left) % 2 != 0:
                    return rec(left, mid-2)
                else:
                    return rec(mid+1, right)
            else:
                if (right-mid-1) % 2 != 0:
                    return rec(mid+2, right)
                else:
                    return rec(left, mid-1)
        
        return rec(0, len(nums)-1)

你可能感兴趣的:(leetcode刷题笔记 task3 查找1)