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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
分析
给定一个整数序列,求出其中出现次数大于floor(n/2)的元素,并返回。
这是一个很简单的题目,首先将序列排序,得到一个有序排列,然后依次遍历,统计重复元素的出现次数,返回次数大于一半的元素即可。
AC代码
class Solution {
public:
int majorityElement(vector<int>& nums) {
//题目假设输入数组非空,且出现次数大于[n/2]的元素存在
int len = nums.size() , count = len/2;
sort(nums.begin(), nums.end());
if (len == 1)
return nums[0];
int tmp = 1;
for (int i = 0; i < len-1; i++)
{
if (nums[i + 1] == nums[i])
{
++tmp;
if (tmp > count)
return nums[i];
}
else{
tmp = 1;
}//if
}//for
return -1;
}
};
LeetCode(169)Majority Element的更多相关文章
- LeetCode(27)Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- 新概念英语(1-69)The car race
新概念英语(1-69)The car race Which car was the winner in 1995 ? There is car race near our town every ye ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
随机推荐
- 「软件」仿站小工具v9.0
仿站小工具是通过网址下载静态网页的工具.从输入的网址下载html代码,提取出JS.Css.Image.Picture.Flash等静态文件网址,再从下载完好的Css代码中提取出Image静态文件网址, ...
- 无向图的边双连通分量(EBC)
嗯,首先边双连通分量(双连通分量之一)是:在一个无向图中,去掉任意的一条边都不会改变此图的连通性,即不存在桥(连通两个边双连通分量的边),称作边双连通分量.一个无向图的每一个极大边双连通子图称作此无向 ...
- AtCoder Grand Contest 017 A
Problem Statement There are N bags of biscuits. The i-th bag contains Ai biscuits. Takaki will selec ...
- 创建表的规范 nvarchar2,varchar2
1,这个真没见过什么最佳实践,都是变长的,这些都是研发根据业务需求自己设定啊. 如果需要多语种支持就用NVARCHAR2(或者汉语),如果只是单语种(英语)就varchar2 . 2. Oracle中 ...
- pyinstaller打包遇到的错误处理
在python环境中运行中没问题,但打包时遇到以下问题 1.有时打包出现 UnicodeDecodeError错误, 可以改变cmd的编码(暂且这么叫),在cmd 中输入 chcp 65001,再次打 ...
- (027)[技术资料]业余制作Windows图标
这几天一直在纠结一件事,想给软件制作一个简单的图标,以前(2014-10-4 11:00)制作的是下面这个,多重ICO,最大尺寸256,无压缩(windows允许图标尺寸在大于256时按PNG方式进行 ...
- Dev GridView 属性说明
说明 Options OptionsBehavior 视图的行为选项 AllowIncrementalSearch 允许用户通过输入想得到的列值来定位行 AllowPartialRedrawOnS ...
- 通俗易懂的Nhibernate教程(2) ---- 配置之Nhibernate配置
在上一个教程中,我们讲了Nhibernate的基本使用!So,让我们回顾下Nhibernate使用基本的步骤吧 1.NHibernate配置 ----- 这一步我们告诉了Nhibernate:数据库 ...
- [ Luogu 3709 ] 大爷的字符串题
\(\\\) Description 原题题面太过混乱出题人语文凉凉 给出一个长为 \(n\) 的数列 \(A\) ,多次询问: 对于一个区间 \([L_i,R_i]\),把区间内的所有数最少划分成多 ...
- git设置log的别名 for hist
hist -- alias for 'log --color --graph --date=short --pretty=format:'%Cred%h%Creset -%C(yellow)%d%C ...