#Method 1
import math
class Solution(object):
    def majorityElement(self, nums):
        numsDic={}
        for num in nums:
            numsDic[num]=numsDic[nums]+1 if num in numsDic else 0
            if numsDic[num]>len(nums)/2:
                return num

#Method 2
#这种方法的思想是把 majority element 看成是 1,而把其他的元素看成是 -1。
#算法首先取第一个元素 x 作为 majority element,并计 mark = 1;
#而后遍历所有的元素,如果元素和 x 相等, 则 mark ++;
#否则如果不等, 则 mark--, 如果 mark == 0, 则重置 mark = 1, 并且更新 x 为当前元素。
#由于majority element 的数量大于一半,所以最后剩下的必然是majority element.

class Solution(object):
    def majorityElement(self, nums):
        mark=1
        x=nums[0]
        for i in range(1,len(nums)):
           
            if(mark==0):
                mark=1;x=nums[i]
            elif(nums[i]==x):
                mark+=1
            elif(nums[i]!=x):
                mark-=1
       
        return x

【leetcode❤python】169. Majority Element的更多相关文章

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

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

  2. 【leetcode❤python】27. Remove Element

    #-*- coding: UTF-8 -*- class Solution(object):    def removeElement(self, nums, val):        "& ...

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

  4. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  5. 【一天一道LeetCode】#169. Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】169 - Majority Element

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

  7. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  8. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

  9. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

随机推荐

  1. sql 数据库查看主外键关联

    SELECT 主键列ID=b.rkey ,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid) ,外键表ID=b. ...

  2. ORACLE CUP相关

    遭遇cpu过多占用,表现为%usr很高,top 或者topas中cpu占用最多的进程为oracle server process. 则根据pid可以找出该pid对应的sql_text select s ...

  3. CSSの神小结-简单备忘一下(亲测可用)

    css 选择器优先级,标签>id>class 权重 id>class>标签 只记录能想到的以免遗忘: 1.字体css可继承 2.表格:表格细线的合并,表格单元格合并,单元格内容 ...

  4. DMA-330(二)

    DMA内部的block diagram: DMAC包含一个instruction processing block,来process program code,control DMA transfer ...

  5. css 标签 清除浮动

    .clearfloat:after{content: "";clear:both;display: block;}

  6. [OrangePi] Installation on internal EMMC

    Install the image on SD Card as described above Boot your Orange PI board from SD Card Run: sudo ins ...

  7. 《聚焦3D地形编程》学习点

    痞子龙的译本虽然称不上好,但却保留了原汁原味,看这本书时最好结合原文与痞子龙的译文.另外,如果有过地形生成的经验再看这本书时有些帮助,这本书介绍的专业的室外地形开发,很全面的介绍. 仅是个人总结,可能 ...

  8. 解决 “invalid resource directory name”, resource “crunch”

        try this: from the menu click Project->Clean... a popup window will appear. select the check ...

  9. python 提取图片转为16 24BPP 的方法

    python 中处理图片用的是 pil ,在 linux  和 win 上都可以使用. centOS 5.x 上安装的方法是 yum install python-imaging 24BPP: imp ...

  10. Failed to read auto-increment value from storage engine, Error Number: 1467

    重设auto_increment:ALTER TABLE tableName auto_increment=number