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

排序,选择第n/2个数,调用STL的sort,所以时间复杂度是O(nlogn)

class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nuns.end());
return nums[nums.size()/2];
}
};

  

思路2

设置一个计数,当count为0时设置majority的值,如果下一个值与majority相同则count+1,如果不同,则-1,当count==0时,对majority重新赋值,因为majority的个数肯定大于n/2所以最后>0的count的肯定是majority,这样,就只需要遍历一遍就可以求出majority,时间复杂度为O(n)

class Solution {
public:
int majorityElement(vector<int>& nums) {
int Majority = nums[0];
int count = 1;
for( int i = 1; i < nums.size(); i++ ){
if( count == 0 ){
count++;
Majority = nums[i];
}
else if( Majority == nums[i] ){
count++;
}
else if ( Majority != nums[i] ){
count--;
}
}
return Majority;
}
};

  

LeetCode【169. Majority Element】的更多相关文章

  1. LeetCode OJ 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. LeetCode Problem 169: Majority Element查找多数元素

    描述:Given an array of size n, find the majority element. The majority element is the element that app ...

  3. leetcode 【 Find Peak Element 】python 实现

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  6. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  7. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  8. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  9. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

随机推荐

  1. day 2

    三目运算符 A?B:C 等价于 if(A) B; else C; 实例: int i: i=(3>2?5:1)  //如果3>2为真,i的值为5,否则为1 printf(“%d”,i): ...

  2. Android Studio 简单设置

    转自:http://ask.android-studio.org/?/article/14 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings - ...

  3. Beginning Windows Azure Development Guide

    目  录 一 初始化Windows Azure 二 云应用程序的编写. 2.1云应用程序的创建. 2.2一个简单的云应用程序. 2.3 托管云程序. 三 云程序的数据库操作. 3.1通过Cloud平台 ...

  4. JavaScript学习笔记及知识点整理_2

    1.一般而言,在Javascript中,this指向函数执行时的当前对象.举例如下: var someone = { name: "Bob", showName: function ...

  5. [css]【转载张鑫旭】我是如何对网站CSS进行架构的

    一.写在前面的 都是自己积累形成的一些东西,可能带有明显的个人印记.不是专业内容,不是权威指南,只是展示一点自己的观点,借此希望能与各位优秀的同行交流看法,见解.以得到进步与提高. 二.我所知的一些过 ...

  6. linux笔记:shell基础-概述和脚本执行方式

    什么是shell: linux使用的默认shell是Bash: shell脚本的后缀名为.sh,shell脚本的第一行#!/bin/bash 不是注释,而是标识这是一个shell脚本,因为linux并 ...

  7. Echarts的基本用法

    首先需要到导入echatrs.js文件 <script src="dist/echarts.js"></script> 路径配置 require.confi ...

  8. git删除远程仓库的某次错误提交

    改日写git的各种本地恢复 今天不小心把一个分支合并到master上了,上网查了一下回复的方法,简单来说就是: 在本地 把远程的master分支删除再把reset后的分支内容给push上去 新建old ...

  9. CSS选择器和jQuery选择器的区别与联系之一

    到底什么是选择器?我们通过常接触的CSS选择器和jQuery选择器理解一下,我们知道CSS是用于分离网页的结构和表现的,也就是说对于一个网页,HTML定义网页的结构,CSS描述网页的样子,一个很经典的 ...

  10. 了解Sql Server的执行计划

    前一篇总结了Sql Server Profiler,它主要用来监控数据库,并跟踪生成的sql语句.但是只拿到生成的sql语句没有什么用,我们可以利用这些sql语句,然后结合执行计划来分析sql语句的性 ...