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. Vue中 computed 和 methods的区别

    涉及到计算部分的时候,计算属性是基于它们的依赖进行缓存的,如果说值不变,那么它就不会去重新执行,只有当值发生了改变,它才会去重新执行一次,其它时候它都是缓存的.而方法则会反复计算处理.二者之间的差距就 ...

  2. python logs

    # -*- coding: utf-8 -*-import loggingimport sysimport osimport xlrdfrom UtilAzxu import Properties# ...

  3. PHP7 ci框架session存文件,登录的时候session不能读取

    config.php配置 $config['sess_driver'] = 'files';//以文件存储session $config['sess_cookie_name'] = 'ci_sessi ...

  4. CAD绘制室外平台步骤5.3

    1.在平面上用直线划出台阶范围. “工具”“曲线工具”“线变复线”选择这几条线,它们就变成了一条线. “三维建模”“造型对象”“平板”选择这条封闭的线,回车,选择相邻门窗柱子等,回车输入平台厚度如“- ...

  5. LY.JAVA.DAY12.Scanner

    2018-07-24 13:23:18 Scanner类 一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器 package cn.itcast_01; /* * Scanner:用于接收键 ...

  6. shell IF分支判断语句

    单分支IF条件语句 if [ 条件判断式 ] then  程序: fi //结束的时候if反过来写 fi ----------------------------- /** * if test -d ...

  7. mybatis-ResultMappingResolver类信息

    ResultMappingResolver类介绍 一个ResultMap元素对应一个ResultMap对象 resultMap元素中的idArg/result/association/collecti ...

  8. centos7 克隆 网卡无法启用

    1.克隆后查看网卡无法启用,报错信息如下: Apr :: agent systemd: network.service: control process exited, code=exited sta ...

  9. 解决NPM无法安装任何包的解决方案(npm ERR! code MODULE_NOT_FOUND)

    前言 今天突然发现npm无法使用了,执行任何命令都报如下错误: npm ERR! code MODULE_NOT_FOUND npm ERR! Cannot find module 'internal ...

  10. 分布式ID设计方案

    分布式ID的定义: 全局唯一 有序性 有意义 高可用 紧凑性 序列号的可预测性 方案1:使用数据库递增的顺序 最常见的方式.利用数据库,全数据库唯一. 优点: 1)简单,代码方便,性能可以接受. 2) ...