leetcode majority elements

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

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

解法:

As we sweep we maintain a pair consisting of a current candidate and a counter. Initially, the current candidate is unknown and the counter is 0.

When we move the pointer forward over an element e:

  • If the counter is 0, we set the current candidate to e and we set the counter to 1.
  • If the counter is not 0, we increment or decrement the counter according to whether e is the current candidate.

When we are done, the current candidate is the majority element, if there is a majority.

public class Solution {
    public int majorityElement(int[] num) {
    	int sum=0;
    	int result=0;
    	for(int i=0;i<num.length;i++){
    		if(sum==0){
    			result=num[i];
    			sum++;
    		}
    		else if(result==num[i]){
    			sum++;
    			if(sum>num.length/2){
    				return result;
    			}
    		}
    		else sum--;
    	}
    	return result;
    }
}

  

时间: 2024-10-23 15:32:00

leetcode majority elements的相关文章

Leetcode: Remove Elements

一次性通过的,比较顺利,从读题到编写到检查到通过,14分50秒,我在不断进步中,相信经过一段时间联系,这种题可以一眼就写出来,不超过5分钟. 这道题应该说方法跟 Remove Duplicates from sorted Array挺类似的 My Solution: 1 public class Solution { 2 public int removeElement(int[] A, int elem) { 3 int count = 0; 4 for(int i=0; i<A.length

LeetCode &quot;Majority Element II&quot;

Similar as "Majority Element". There are at most 2 such elements, for > floor(n/3), and every non-hit element will decrease count of EACH hit element. https://leetcode.com/discuss/42769/o-n-time-and-in-o-1-space-c-solution

[LeetCode]Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: Special thanks to @

LeetCode——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相

leetcode:Majority Number

1.Given an array of integers, the majority number is the number that occursmore than half of the size of the array. Find it. Given [1, 1, 1, 1, 2, 2, 2], return 1 2.得到当前数组中个数最多的一个数 3.代码 public class MajorityNumber { public static int majorityNumber(A

leetcode Majority Element python

Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. python code

LeetCode:Majority Element Ⅱ

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 解法一: 1 class Solution { 2 public: 3 vector<int> majorityElement(vector<int>& nums) { 4 unor

LeetCode—Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. 其实就是找到序列中出现次数最多的元素,而且已经提出假设这

[LeetCode] Majority Element 求众数

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Credits:Special thanks to @t