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.

题目大意:

给定一个长度为n的数组,寻找其中的“众数”。众数是指出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的并且数组中的众数永远存在。

解题方法:

首先,众数一定大于一半,所以先对数组进行排序,中间元素就是众数。

注意事项:

C++代码:

1 class Solution {
2 public:
3     int majorityElement(vector<int>& nums) {
4         sort(nums.begin(),nums.end());
5         return nums[nums.size()/2];
6     }
7 };
时间: 2024-12-17 10:41:39

169. Majority Element(C++)的相关文章

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

【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 (众数)

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. 题目标签:Array 忘记说了,特地回来补充,今天看完<

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. Hide Tags: Divide and Conquer

169.Majority Element (法1排序法2多数投票)

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

leetcode169——Majority Element (C++)

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. 个人博客:http://www.cnblogs.com/

Majority Element (算法)

自己写了一种方法,看了别人的解析之后觉得自己的方法好low,不高效也不明智.所以决定记录下来. 题目要求 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 al

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