169 Majority Element [LeetCode Java实现]

题目链接: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.
 *
 */
public class MajorityElement {

//	40 / 40 test cases passed.
//	Status: Accepted
//	Runtime: 253 ms
//	Submitted: 1 minute ago

	//时间复杂度为 O(n), 空间复杂度为 O(1)

	//因为最多的那个元素占一半及以上, 故能够和别的元素相互抵消,最后剩下的未抵消掉的元素为所求
	//将数组分成三块:num[0...i]为 归并的 还未抵消的数组。 num[i+1...j-1]空暇区, num[j...num.length-1]为等待抵消的数组
	//抵消规则:若num[i] = num[j] 则把num[j]归入num[0...i+1]数组中
	//		 若num[i] != num[j] 则把num[i] 抵消掉 然后 i--

    static int majorityElement(int[] num) {

    	int i = -1;

    	for (int j = 0; j < num.length; j++) {

    		if(i == -1) num[++i] = num[j];
        	else {
            	if(num[j] == num[i]) num[ ++i] = num[j];
    			else i --;
			}

		}
    	return num[0];
    }
	public static void main(String[] args) {

		System.out.println(majorityElement(new int[]{4}));
		System.out.println(majorityElement(new int[]{4, 4, 5}));
		System.out.println(majorityElement(new int[]{4, 3, 3}));
		System.out.println(majorityElement(new int[]{4, 3, 4}));
	}

}
时间: 2024-11-05 11:23:18

169 Majority Element [LeetCode Java实现]的相关文章

169. Majority Element - LeetCode

Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排序,次数超过n/2的元素必然在中间. Java实现: public int majorityElement(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int num : nums) { In

Leetcode#169. Majority Element(求众数)

题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ? n/2 ? 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2 思路 思路一: 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字. 思路二: 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数.即nums[n/2]为众数.但是在计算比

[Lintcode]46. Majority Element/[Leetcode]169. Majority Element

46. Majority Element/[169. Majority Element(https://leetcode.com/problems/majority-element/) 本题难度: Easy Topic: Greedy Description Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find

LeetCode Javascript实现 169. Majority Element

169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { var hash = {}; var y=-1,z; //注意这里的方括号,利用变量访问对象属性时要用方括号 for(var i=0;i<=nums.length-1;i++){ if(hash[nums[i]]){ hash[nums[i]]++; }else{ hash[

leetCode 169. Majority Element 数组

169. 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. 思路1: 使用m

169. Majority Element &amp;&amp; 229. Majority Element II

169. 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. Hide T

169. Majority Element(C++)

169. 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. 题目大意:

刷题169. Majority Element

一.题目说明 题目169. Majority Element,给定n个数的数组,返回出现次数超过半数的元素. 二.我的解答 这个题目用一个map,遍历一遍数组,计数每个元素出现的次数. class Solution{ public: int majorityElement(vector<int>& nums){ unordered_map<int,int> ump; int maxNum = 0; int maxCount = 0; for(int i=0;i<num

Java for LeetCode 169 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. 解题思路: 编程之美P130(寻找发帖水王)原题,如果删