题目:

      Given an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋ times.

      You may assume that the array is non-empty and the majority element always exist in the array.

      给定一个大小为n的数组,找到多数元素。多数元素的元素⌊n / 2⌋多次出现。假定数组是非空的,并且数组中始终存在多数元素。


思路:

   遍历数组,如果下一个元素和当前元素相同时,b加一,否则就减一,若b为0时,则更改对比元素,并且重置b加一。

LeetCode编程练习 - Majority Element学习心得

相关文章:

  • 2021-10-03
  • 2021-04-06
  • 2021-12-11
  • 2021-06-26
  • 2021-04-24
  • 2021-06-02
  • 2021-09-27
  • 2021-10-13
猜你喜欢
  • 2021-11-16
  • 2021-09-26
  • 2021-05-13
  • 2021-06-05
  • 2021-11-12
  • 2021-06-25
  • 2021-06-04
相关资源
相似解决方案