Leetcode面试经典150_Q169多数元素

题目:

给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊n/2⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

解题思路:

1. 注意“大于 ⌊n/2⌋”,因此在将数据排序之后一定可以在⌊n/2⌋的下标位置找到该数字;

2. 哈希映射存储每个元素及其出现的次数;

3. 由于列表中有众数,随机挑选下标并验证;

4. 分治“如果数 a 是数组 nums 的众数,如果我们将 nums 分成两部分,那么 a 必定是至少一部分的众数”

5. Boyer-Moore 投票:维护一个候选众数 candidate 和它出现的次数 count。初始时 candidate 可以为任意值,count 为 0;遍历数组 nums 中的所有元素,对于每个元素 x,在判断 x 之前,如果 count 的值为 0,我们先将 x 的值赋予 candidate,随后我们判断 x;如果 x 与 candidate 相等,那么计数器 count 的值增加 1x 与 candidate 不等,那么计数器 count 的值减少 1;在遍历完成后,candidate 即为整个数组的众数


Python 解法:

class Solution: # 分治
    def majorityElement(self, nums: List[int]) -> int:
        def majority_element_rec(lo, hi) -> int:
            # base case; the only element in an array of size 1 is the majority
            # element.
            if lo == hi:
                return nums[lo]

            # recurse on left and right halves of this slice.
            mid = (hi - lo) // 2 + lo
            left = majority_element_rec(lo, mid)
            right = majority_element_rec(mid + 1, hi)

            # if the two halves agree on the majority element, return it.
            if left == right:
                return left

            # otherwise, count each element and return the "winner".
            left_count = sum(1 for i in range(lo, hi + 1) if nums[i] == left)
            right_count = sum(1 for i in range(lo, hi + 1) if nums[i] == right)

            return left if left_count > right_count else right

        return majority_element_rec(0, len(nums) - 1)


class Solution: # 投票
    def majorityElement(self, nums: List[int]) -> int:
        count = 0
        candidate = None

        for num in nums:
            if count == 0:
                candidate = num
            count += (1 if num == candidate else -1)

        return candidate

相关推荐

  1. Leetcode面试经典150_Q169多数元素

    2024-04-10 00:16:02       16 阅读
  2. Leetcode面试经典150题》169. 多数元素

    2024-04-10 00:16:02       29 阅读
  3. Leetcode面试经典150_Q189轮转数组

    2024-04-10 00:16:02       15 阅读
  4. [经典面试题]169. 多数元素

    2024-04-10 00:16:02       34 阅读
  5. [leetcode] 169. 多数元素

    2024-04-10 00:16:02       34 阅读
  6. leetcode 169.多数元素

    2024-04-10 00:16:02       51 阅读
  7. LeetCode 169. 多数元素

    2024-04-10 00:16:02       10 阅读
  8. leetcode-169-多数元素

    2024-04-10 00:16:02       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 00:16:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 00:16:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 00:16:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 00:16:02       20 阅读

热门阅读

  1. vivado 设计调试

    2024-04-10 00:16:02       17 阅读
  2. linux下python服务定时(自)启动

    2024-04-10 00:16:02       13 阅读
  3. C++基础——结构体与类

    2024-04-10 00:16:02       17 阅读
  4. GIT泄露

    GIT泄露

    2024-04-10 00:16:02      15 阅读
  5. 广义表的学习

    2024-04-10 00:16:02       13 阅读
  6. M2 Pro安装 huggingface transformer

    2024-04-10 00:16:02       16 阅读
  7. Leetcode面试经典150_Q189轮转数组

    2024-04-10 00:16:02       15 阅读
  8. LeetCode|501. Find Mode in Binary Search Tree

    2024-04-10 00:16:02       9 阅读
  9. 爬虫之数据神器10---Peewee实现ORM的核心原理

    2024-04-10 00:16:02       13 阅读
  10. Day32 线程安全二

    2024-04-10 00:16:02       14 阅读
  11. Day31 线程安全一

    2024-04-10 00:16:02       13 阅读