mycode  54.93%

class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic = {}
count = len(nums) // 2
for item in nums:
dic[item] = dic.get(item,0) + 1
if dic[item] > count :
return item

参考:

思路 : 题目中说明了majority的特点,所以排序之后从下标就能看出来啦

class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return nums[len(nums)//2]

leetcode-mid-others-169. Majority Element¶的更多相关文章

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

  2. 【leetcode❤python】169. Majority Element

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

  3. [LeetCode&Python] Problem 169. Majority Element

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

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

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

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

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

  6. 23. leetcode 169. Majority Element

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

  7. 169. Majority Element - LeetCode

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

  8. Week1 - 169.Majority Element

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

  9. 169. Majority Element(C++)

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

  10. LeetCode 169. Majority Element (众数)

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

随机推荐

  1. python redis之连接池的原理

    python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...

  2. 1.Java 字符分割

    使用方法 性能比较 使用方法 或|,点.,加+,乘*,在字符串中出现时,如果这个字符串需要被split,则split时候,需要在前面加两个反斜杠. 与&,在split时候,不需要转义. 一.j ...

  3. 23_1spring基础

    1.spring是什么 2.spring的优势 3.spring体系结构 4.耦合 程序间的依赖关系:类之间的依赖和方法之间的依赖. 解构:降低程序间的依赖关系. 实际开发中应该做到:编译期不依赖,实 ...

  4. 关于FileChannel的获取方式之open方法详解

    FileChannel.open(Path path, OpenOption... options); 例子使用JDK1.8 FileChannel open方法源码: public static F ...

  5. Generative Adversarial Nets(GAN Tensorflow)

    Generative Adversarial Nets(简称GAN)是一种非常流行的神经网络. 它最初是由Ian Goodfellow等人在NIPS 2014论文中介绍的. 这篇论文引发了很多关于神经 ...

  6. [POJ1664]放苹果(动态规划)

    [POJ1664]放苹果 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第 ...

  7. web性能优化--服务器端(二)

    静态资源用CDN部署 添加Expires或者cache-control报文头 Gzip压缩传输文件 配置Etags 使用Get ajax请求 避免空图片src 尽早flush response 减少c ...

  8. java打分系统

    package com.ioTest; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fil ...

  9. Gym-100923L-Por Costel and the Semipalindromes(进制转换,数学)

    链接: https://vjudge.net/problem/Gym-100923L 题意: Por Costel the pig, our programmer in-training, has r ...

  10. python接口自动化五(参数关联)

    前言 我们用自动化发帖之后,要想接着对这篇帖子操作,那就需要用参数关联了,发帖之后会有一个帖子的id,获取到这个id,继续操作传这个帖子id就可以了 (博客园的登录机制已经变了,不能用账号和密码登录了 ...