Time:2019/4/4
Title: Majority Element 1
Difficulty: easy
Author: 小鹿javascript
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.java
You may assume that the array is non-empty and the majority element always exist in the array.git
Example 1:github
Input: [3,2,3] Output: 3
Example 2:算法
Input: [2,2,1,1,1,2,2] Output: 2
题目的要求是让咱们求数组中超过一半数据以上相同的元素且老是存在的。有这样一个思路要和你们分享:假若有这样一种数据,数组中的所存在的这个超过 n/2 以上的数据(众数)确定比与此众数不相同的其余元素个数要多(n/2 以上)。咱们能够这样统计,用一个变量来计数,另外一个变量记录计数的该元素,遍历整个数组,若是遇到相同的 count 加 +1,不一样的就 -1 ,最后所存储的就是众数,由于其余数据的个数比众数个数要少嘛,这就是所谓的摩尔投票算法。编程
/** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { //用来计数相同的数据 let count = 0; //存储当前的元素 let majority = 0; //遍历整个数组 for(let i = 0;i < nums.length; ++i){ //若是 count 为 0 if(count === 0){ //将当前数据为众数 majority = nums[i]; count++; }else if(majority === nums[i]){ //若是遍历的当前数据与存储的当前数据相同,计数+1 count++; }else{ //若不相同,计数 - 1 count--; } } //假设相同的众数呢? if(count === 0){ return -1; }else{ return majority; } };
欢迎一块儿加入到 LeetCode 开源 Github 仓库,能够向 me 提交您其余语言的代码。在仓库上坚持和小伙伴们一块儿打卡,共同完善咱们的开源小仓库!
Github:https://github.com/luxiangqia...
数组