#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. 算法第四版 在Eclipse中调用Algs4库

    首先下载Eclipse,我选择的是Eclipse IDE for Java Developers64位版本,下载下来之后解压缩到喜欢的位置然后双击Eclipse.exe启动 然后开始新建项目,File ...

  2. (第九周)视频发布及git统计报告

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 代码地址:HTTPS: https://git.coding.net/li_yuhuan/FoodChain. ...

  3. 3. 星际争霸之php设计模式--简单工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  4. 在centos中编译putty时提示找不到gtk库的解决办法

    明明已经安过gtk了, 后来在csdn上发现了前人的解决办法 yum install libgnomeui-devel 现在编译出来的putty就可以有图形界面了

  5. javascript中字符串格式json如何转化成json对象

    什么是JSON JSON(JavaScript Object Notation)是一种优美的JavaScript对象创建方法.JSON也是一种轻量级数据交换格式.JSON非常易于人阅读与编写,同时利于 ...

  6. 在TVideoGrabber中如何在预览时设置相机属性

    在使用TVideoGrabber进行预览时,如何设置相机的属性呢?比如曝光.对比度.亮度等. 下面来看一下,如何通过编程来调整这些设置: ——通过指定VideoDevice属性(在VideoDevic ...

  7. 加载执行预编译的Sql :prepareStatement

    1.获得连接:Connection con = null; con = DBUtil.getConnection(); 2.写sql语句:String sql=""; 3.用连接加 ...

  8. ASP.NET MVC下的四种验证编程方式[续篇]【转】

    在<ASP.NET MVC下的四种验证编程方式> 一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式(“手工验证”.“标注ValidationAttribute特性”.“ ...

  9. 使用SQLServer Profiler侦测死锁(转)

    准备工作: 为了侦测死锁,我们需要先模拟死锁.本例将使用两个不同的会话创建两个事务. 步骤: 1. 打开SQLServer Profiler 2. 选择[新建跟踪],连到实例. 3. 然后选择[空白] ...

  10. javaWeb 使用jsp开发 html过滤标签

    1.jsp调用代码 <t:htmlFilter> <a href="${pageContext.request.contextPath }/index.jsp"& ...