【力扣hot100】刷题笔记Day27

前言

  • 关于技巧的题目想不出来直接抄答案好了,都是没遇到的算法可能

136. 只出现一次的数字 - 力扣(LeetCode)

异或操作

复制代码
```python
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        # 异或运算:
        # 1. a ^ 0 = a
        # 2. a ^ a = 0
        # 3. 满足交换律和结合率
        res = 0
        for num in nums:
            res = res ^ num  # 把所有的数进行异或,两个的消成0,再和1个的异或得到1个的
        return res
```

169. 多数元素 - 力扣(LeetCode)

摩尔投票

复制代码
```python
class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        votes = 0
        for num in nums:
            # 如果票数为0,则众数在剩下的集合里,重新设置当前数为众数 
            if votes == 0: x = num   
            # 和众数相同的票数+1,和众数不同的票数-1       
            votes += 1 if num == x else -1 
        return x
```

75. 颜色分类 - 力扣(LeetCode)

三指针

复制代码
```python
class Solution:
    def sortColors(self, nums: List[int]) -> None:
        n = len(nums)
        if n < 2: return
        def swap(nums, i, j):
            nums[i], nums[j] = nums[j], nums[i]
        # 以下区间结合为全区间
        # all in [0, zero) = 0
        # all in [zero, i) = 1
        # all in [two, len - 1] = 2
        # 各指针初始化为空区间
        zero = 0
        i = 0
        two = n
        while i < two:  # 当 i == two 上面的三个子区间正好覆盖了全部数组
            if nums[i] == 0:        # [0, zero)取不到,先换再加
                swap(nums, i, zero)
                i += 1
                zero += 1
            elif nums[i] == 2:
                two -= 1            # [two, len - 1]取得到,先减再换
                swap(nums, i, two)
            else:
                i += 1
```

31. 下一个排列 - 力扣(LeetCode)

换数+改升序

复制代码
```python
class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        if len(nums) <= 1:
            return

        # 寻找查找第一个相邻升序的元素对(i,i+1),满足A[i]<A[i+1],[i+1,end)为降序
        i = len(nums) - 2
        while i >= 0 and nums[i] >= nums[i + 1]:
            i -= 1

        # 如果i不是 -1,说明找到了相邻升序的元素对(i,i+1)
        if i >= 0:
            # 在[i+1,end)中寻找第一个大于 nums[i] 的元素的位置k,且k必须在i之后
            k = len(nums) - 1
            while nums[i] >= nums[k] and k > i:
                k -= 1
            # 交换 nums[i] 和 nums[k]
            nums[i], nums[k] = nums[k], nums[i]

        # 反转从 i+1 到数组末尾的部分,以生成下一个排列
        nums[i+1:] = reversed(nums[i + 1:])
```

287. 寻找重复数 - 力扣(LeetCode)

原地哈希

复制代码
```python
class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        n = len(nums)
        i = 0
        while i < n:
            # 如果要放的地方已经被占了,说明找到重复数
            if nums[i] == nums[nums[i]]:
                return nums[i]  
            # 如果没有,就把nums[i]放到下表为nums[i]处,交换
            else:
                tmp = nums[i]
                nums[i], nums[tmp] = nums[tmp], nums[i]
            # 如果当前位置已放好,i往后移动
            if i == nums[i]:
                i += 1
        return -1
```

环形链表

复制代码
```python
class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        def next(i):
            return nums[i]
        slow = fast = 0
        # 第一次相遇
        while True:
            slow = next(slow)
            fast = next(next(fast))
            if slow == fast:
                break
        slow = 0
        # 第二次相遇
        while slow != fast:
            slow = next(slow)
            fast = next(fast)
        return slow
```

后言

  • Hot100终于刷完了!!(咆哮怒吼兴奋到处爬行),我要找实习!我要上岸!
相关推荐
NeDon几秒前
[OJ]数据结构:移除链表元素
c语言·数据结构·算法·链表
刃神太酷啦1 分钟前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
承渊政道2 分钟前
一文彻底搞清楚链表算法实战大揭秘和双向链表实现
c语言·数据结构·算法·leetcode·链表·visual studio
jianfeng_zhu6 分钟前
数据结构中括号匹配的问题
数据结构
sali-tec9 分钟前
C# 基于halcon的视觉工作流-章69 深度学习-异常值检测
开发语言·图像处理·算法·计算机视觉·c#
Lv117700824 分钟前
Visual Studio中的字典
ide·笔记·c#·visual studio
努力写代码的熊大29 分钟前
手撕AVL树:从理论到实践,掌握插入操作的完美平衡
算法
wbs_scy40 分钟前
C++:二叉搜索树(BST)完全指南(从概念原理、核心操作到底层实现)
数据结构·算法
东华万里41 分钟前
Release 版本禁用 assert:NDEBUG 的底层逻辑与效率优化
java·jvm·算法