LeetCode编程练习

来源:互联网 发布:云计算工程师薪资待遇 编辑:程序博客网 时间:2024/04/27 19:55

题目:

      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加一。

原创粉丝点击