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.

Hide Tags: Divide and Conquer Array Bit Manipulation

解题思路:

(1)使用HashMap,Map的特点:不允许重复元素,因此在存储前需要判断是否存在

(2)判断HashMap中存在nums[i],如果存在,使用hm.get(nums[i])获取value,即通过key来获得value值,即count(出现的次数)

(3)如果count大于数组长度的一般,即返回该元素

(4)如果count不满足条件,向HashMap存储元素以及出现的次数。

代码如下:

	public static int majorityElement(int[] nums)
	{
		/*
		 * Map的特点:不允许重复元素,因此在存储前需要判断是否存在
		 */
		Map<Integer, Integer> hm=new HashMap<Integer, Integer>();
		for (int i = 0; i < nums.length; i++)
		{
			int count=0;
			//判断HashMap中存在nums[i],如果存在,使用hm.get(nums[i])获取value
			//即通过key来获得value值,即count(出现的次数)
			if (hm.containsKey(nums[i]))
			{
				count=hm.get(nums[i])+1;
			}
			else
			{
				count=1;
			}
			//如果count大于数组长度的一般,即返回该元素
			if (count>nums.length/2)
			{
				return nums[i];
			}
			//向HashMap存储元素以及出现的次数
			hm.put(nums[i], count);
		}
		return 0;

	}
时间: 2024-10-27 04:06:08

leetcode——169 Majority Element(数组中出现次数过半的元素)的相关文章

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

找出数组中出现次数超过一半的元素

题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int half_number(int a[], int n) { if( a == NULL || n <= 0 ) return -1; int i, candidate; int times = 0; for( i=0; i<n; i++ ) { if( times == 0 ) { candidate

C语言--查询数组中出现次数最多的元素

查询数组中出现次数最多的元素 #include <stdio.h> #include <malloc.h> #include <stdlib.h> int max_count_num(int * arr, int len); int main() { int arr[5] = {1, 1, 1, 3, 1 }; max_count_num(arr, 5); return 0; } int max_count_num(int * arr, int len) { int i

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]为众数.但是在计算比

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. 也就是找数组中出现次数大于一半的数字,题目保证这

[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 169. Majority Element 求出现次数最多的数 --------- java

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 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. 思路: Find k different element

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]