0%

从两数之和开始的力扣通关之路

1. 两数之和

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hash_table = {}
        for i in range(len(nums)):
        # for i,num in enumerate(nums):
            diff = target - nums[i]
            if diff in hash_table:
                return [hash_table[diff],i]
            else:
                hash_table[nums[i]] = i
        return []

11. 盛水最多的容器

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_area = 0
        left = 0
        right = len(height) - 1
        while left < right:
            current_area = min(height[left], height[right]) * (right - left)
            max_area = max(max_area,current_area)
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        return max_area

最大肯定宽度得大,从两边开始往中间合拢。(直观想法)想想为什么从头往后拓宽不行,典型错例比如[1,2,1],初始化left=0,right=1,1 < 2,left也到2,相同然后left再+1到1,没有遍历出1和1相差2这种情况。所以两头才是所有的情况而从头会有遗漏。两边一边一个指针,谁小谁挪,每次记录一下最大面积。

错误代码be like:

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_area = 0
        left = 0
        right = 1
        while left < len(height) and right < len(height):
            current_area = min(height[left], height[right]) * abs(right - left)
            max_area = max(max_area,current_area)
            if height[left] <= height[right]:
                left += 1
            else:
                right += 1
        return max_area

15. 三数之和

49. 字母异位词分组

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        anagrams = {}
        for s in strs:
            sorted_s = ','.join(sorted(s))
            if sorted_s in anagrams:
                anagrams[sorted_s].append(s)
            else:
                anagrams[sorted_s] = [s]
        return list(anagrams.values())

128. 最长连续序列

思路一

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        if not nums:
            return 0
        consecutive_map = {}
        max_length = 0
        sorted_nums = sorted(nums)
        for n in nums:
            if n in consecutive_map:
                continue
            left_length = consecutive_map.get(n - 1, 0)
            right_length = consecutive_map.get(n + 1, 0)
            current_length = left_length + 1 + right_length
            consecutive_map[n] = current_length
            max_length = max(max_length,current_length)
            consecutive_map[n - left_length] = current_length
            consecutive_map[n + right_length] = current_length
        return max_length            

1,最大长度是1,左看看有看看,2,左边有1,最大长度是2,更新一下1的节点,以此类推。

思路二(官方题解

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        longest_streak = 0
        num_set = set(nums)

        for num in num_set:
            if num - 1 not in num_set:
                current_num = num
                current_streak = 1

                while current_num + 1 in num_set:
                    current_num += 1
                    current_streak += 1

                longest_streak = max(longest_streak, current_streak)

        return longest_streak

将List转换为Set,Set与List类似,但不存储相同的元素,且查找速度要更快一些。先找到连续序列的开头,然后再用循环往下找最长序列。

283. 移动零

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        left =  0 # left用来指示0的位置
        right = 0 # right用来遍历数组
        while right < len(nums):
            if nums[right] != 0:
                nums[left],nums[right] = nums[right],nums[left]
                left += 1
            right += 1

1,0,2,最开始left,right都指1,1不是0,1和自己换一下,然后left和right都+1,都指0,0等于0不执行if语句,只有right继续遍历+1,left指向0,标识0的位置。right指向2,执行if语句,将0和2换位,跳出while循环结束。

“富哥vivo50看看实力”