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.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2
 
from collections import Counter
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a=len(nums)//2
c=Counter(nums)
ans=0
for i in c:
if c[i]>a:
return i

  

[LeetCode&Python] Problem 169. Majority Element的更多相关文章

  1. 【leetcode❤python】169. Majority Element

    #Method 1import math class Solution(object):    def majorityElement(self, nums):        numsDic={}   ...

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

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

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

  4. [LeetCode&Python] Problem 27. Remove Element

    Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...

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

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

  6. 169. Majority Element - LeetCode

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

  7. 23. leetcode 169. Majority Element

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

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

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

  9. Week1 - 169.Majority Element

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

随机推荐

  1. Linux stress CPU的测试方法

    一.stress工具安装:1.获取stress源码安装包(stress-1.0.4.tar.gz)3.解压并安装 [root@localhost /]#cd /tmp/ [root@localhost ...

  2. SpringBoot主程序类,主入口类

    主程序类,主入口类 /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootApplication publi ...

  3. suffix word ality ally an ancy ance an aneity out ~1

    1● ality 状态,性质   2● ally al+ly ~地   3● an ~地方 ,~人       1★ ance=ancy 性质 ,状态   2★ant ~人,~剂,~的   3★ an ...

  4. 分析图第二讲导出图片和后期PS5.12

    导出渲染的白模加上EXTRATEX.再导出一张“消隐”样式的模型图片.就是线稿图. 再导出一张着色显示图,并去掉边线.,用于后期PS选择范围用. 把这几张图全都导入ps. 渲染图的阴影面是灰色的,示例 ...

  5. LY.JAVA面向对象编程思想概述

    面向对象 2018年7月5日  逆袭之旅DAY09 2018年7月5日  逆袭之旅DAY09 2018-07-07

  6. Resharper插件的使用

    一.Resharper设置 1.1 智能提示 安装完毕后,IDE 的智能提示(Intellisense)便会默认使用 Resharper 的提示,不知道为什么,我一直不太喜欢它的提示.改过来,是在Op ...

  7. bootstrap table 分页显示问题

    1.bootstrap-table客户端分页 客户端分页的数据源可以是后台服务器端传递过来(一次性获取,即获取所有你需要的数据),点击页码不再请求后台,利用页面缓存分页;cache : true, / ...

  8. Centos7 LNMP 一键安装

    首页: https://lnmp.org/ 安装包生成页: https://lnmp.org/auto.html

  9. java中String的认识

    String不是Java的基本数据类型.String类是final类,故不可继承. String 和 StringBuffer之间的区别非常大,Java平台提供了两个类,两者都是包含多个字符的的字符数 ...

  10. SharePoint REST API - 基本操作(二)

    博客地址:http://blog.csdn.net/FoxDave 上一节讲了SharePoint REST API的一些基本操作,本节将继续介绍一些关于SharePoint REST API的内容. ...