leetcode_169题——Majority Element(采用的map来做的)

Majority Element

Total Accepted: 35645 Total Submissions: 103047My Submissions

Question Solution

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 @ts for adding this problem and creating all test cases.

Hide Tags

Divide and Conquer Array Bit Manipulation

Have you met this question in a real interview?

Yes

No

Discuss

这道题的要求是找到行向量中出现次数最多的数字,而在题目中好像希望可以用分治法来做,但是我采用的map来做的,因为这个记录起来很快,map查找的时间复杂度很小,<int,int> first中储存值。second储存值出现的次数

#include<iostream>
#include <vector>
#include <map>
#include <utility>
using namespace std;

int majorityElement(vector<int>& nums) {
	map<int,int> temp;
	int len=nums.size();
	int result;
	if(len==1)
	{
		result=nums[0];
		return result;
	}
	for(int i=0;i<len;i++)
	{
		if(temp.count(nums[i])==0)
		{
			temp.insert(make_pair(nums[i],1));
		}
		else
		{
			temp[nums[i]]++;
		}
	}
	pair<int,int>max_num(temp.begin()->first,temp.begin()->second);
	for(map<int,int>::iterator i=temp.begin();i!=temp.end();i++)
	{
		if(i->second>max_num.second)
		{
			max_num.first=i->first;
			max_num.second=i->second;
		}
	}
	result=max_num.first;
	return result;
}
int main()
{
	vector<int> vec;
	vec.push_back(2),vec.push_back(2);
	cout<<majorityElement(vec)<<endl;
}

  

时间: 2024-10-28 15:30:55

leetcode_169题——Majority Element(采用的map来做的)的相关文章

LeetCode 169 Majority Element(主要元素)(vector、map)

翻译 给定一个长度为n的数组,找出主要的元素. 所谓主要的元素是指的出现次数超过? n/2 ?次的元素. 你可以假定这个数组是非空的,并且"主要元素"一定是存在的. 原文 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

刷题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

【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. 题解:设置两个变量,一个是current,初始化为A[0

LeetCode66/169/79 Plus One/Majority Element /Word Search

一: Plus One 题目: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 链接:https://leetcode.com/problems/plus-one/ 分析:就是简单的加法运算,注意进位

【算法31】寻找数组的主元素(Majority Element)

题外话 最近有些网友来信问我博客怎么不更新了,是不是不刷题了,真是惭愧啊,题还是在刷的,不过刷题的频率没以前高了,看完<算法导论>后感觉网上很多讨论的题目其实在导论中都已经有非常好的算法以及数学证明,只是照搬的话好像意义也不是很大,希望找到些有代表性的题目在更新,另外希望能接着前面的<穷举递归和回溯算法终结篇>一系列如动态规划.贪心算法类的终结篇,在梳理自己知识结构的同时也能够帮助读者们更系统的学习算法思想.好了话不多说,进入正题. 问题描述 给定一个数组A[n], 定义数组的主元

【Divide and Conquer】169. Majority Element(easy)

#Week_1# #From LeetCode# Description: 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 t

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

[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 @

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. 哈希实现 通过map计数即可 1 class Solut