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. 逆袭之旅DAY24.XIA.数组练习

    2018-07-20 08:40:19 1. public void stringSort(){ String[] s = new String[]{"George"," ...

  2. Win10系列:JavaScript访问文件和文件夹

    在实际开发中经常会遇到访问文件的情况,因此学习与文件有关的操作对程序开发很有帮助,关于文件操作的一些基本技术,在前面章节中有专门基于C#语言的详细讲解,本节主要介绍如何使用HTML5和JavaScri ...

  3. mac mysql 操作

    参考 http://www.cnblogs.com/chenmo-xpw/p/6102933.html http://www.cnblogs.com/uoar/p/6492521.html 1.启动M ...

  4. js 中class选择器,addClass,removeClass,hasClass,toggleClass,getByClass

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  5. js 敏感词过滤

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. Java正则表达式校验

    package com.study.string; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 正则表达 ...

  7. [POJ2761]Feed the dogs

    Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...

  8. linux-xshell同时向多台服务器一起发命令

    概述:有时候我们要往多台linux服务器上面步东西,一台一台布能烦死我们.如果能同时向多台服务器发命令岂不美哉. 开工: 首先打开exshell,查看->撰写栏  打开 然后瓷砖排序,看起来方便 ...

  9. day23 模块02

    核能来袭--模块 2 1.nametuple() 2.os模块 3.sys模块(重点) 4.序列化 (四个函数) 5.pickle(重点) 6.json(重点中的重点) 1.nametuple() 命 ...

  10. 【转载】linux Jumpserver跳板机堡垒机部署安装使用教程

    原文地址:https://idc.wanyunshuju.com/li/554.html