题目

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;
}
};

GitHub测试程序源码

LeetCode(169)Majority Element的更多相关文章

  1. LeetCode(27)Remove Element

    题目 Given an array and a value, remove all instances of that value in place and return the new length ...

  2. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  3. 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 ...

  4. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  5. 新概念英语(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 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. 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 ...

随机推荐

  1. 我人生中的第一场Java面试

    1.说起我的第一次Java面试,我不禁回想起我大学时参加校园招聘的那段日子,那时候我还是本科生,由于不是科班出身,只学过一点点Java皮毛,所以那时候对于找Java工作并没有什么概念,只是以为上过Ja ...

  2. Beta版本冲刺第二天!

    该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址:https://edu.cnblogs.com/c ...

  3. Jenkins自动化部署——持续交付

    感谢之前带领过我的leader,让我能够知道什么是好的开发方法. 在很早之前就接触过敏捷开发.什么是敏捷开发,简单来说就是让软件可靠地,快速地发布出来的一种开发方法和技巧. 而敏捷开发中有许多的实践, ...

  4. [问题][已解决] 并发场景下 "mysql: too many connections" 原因

    问题出现是这样的,用node写爬虫, 之前每条数据都是await插入,并且是阻塞的,后来改成了非阻塞,可以并行插入操作,结果一直找不到原因. 后来在日志中找到了 too many connection ...

  5. C#实现较为实用的SQLhelper

    第一次写博客,想不到写什么好b( ̄▽ ̄)d ,考虑的半天决定从sqlhelper开始,sqlhelper对程序员来说就像helloworld一样,很简单却又很重要,helloworld代表着程序员萌新 ...

  6. idea web项目热部署

    之前用idea写web项目的时候,一直都是改一点东西就要重启一下,很烦.今天终于忍受不了百度了一下idea怎么热部署web项目. 在此记录下. 第一步 编辑tomcat配置 第二步 选择打包的项目,并 ...

  7. 雪碧图(background-position)、overflow、title中的小图标、光标、rgb 和opacity 与rgba

    一.background-position     雪碧图 我们的html和css中有三个属性可以向服务器发送请求:src   url    href 1.我们为什么使用雪碧图? 因为我们使用雪碧图之 ...

  8. 从 fn_dbLog 解析操作日志(补充update)

    过去经常听到SQL server 日志,可是在提供的界面上看到的Log不是我们想要的,我们想窥探具体的数据操作日志.专业恢复追踪数据库操作日志的软件:ApexSQLLog,偶然发现SQL Server ...

  9. ThreadPoolExecutor 线程池

    TestThreadPoolExecutorMain package core.test.threadpool; import java.util.concurrent.ArrayBlockingQu ...

  10. IOS文件下载

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, ...