/*
* @lc app=leetcode.cn id=169 lang=c
*
* [169] 求众数
*
* https://leetcode-cn.com/problems/majority-element/description/
*
* algorithms
* Easy (58.05%)
* Total Accepted: 27.2K
* Total Submissions: 46.7K
* Testcase Example: '[3,2,3]'
*
* 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
*
* 你可以假设数组是非空的,并且给定的数组总是存在众数。
*
* 示例 1:
*
* 输入: [3,2,3]
* 输出: 3
*
* 示例 2:
*
* 输入: [2,2,1,1,1,2,2]
* 输出: 2
*
*
*/
int majorityElement(int* nums, int numsSize) {
int count=,result=nums[];
int i;
for(i=;i<numsSize;i++){
nums[i]==result?count++:count--;
if(!count){
result=nums[i];
count++;
}
}
return result;
}

这里用的是投票法,如果下一个数还和这个数相等的话,count+1,否则count-1

当count等于0的时候结果被赋值为当前的数,count加一。

---------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=169 lang=python3
#
# [169] 求众数
#
# https://leetcode-cn.com/problems/majority-element/description/
#
# algorithms
# Easy (58.05%)
# Total Accepted: 27.2K
# Total Submissions: 46.7K
# Testcase Example: '[3,2,3]'
#
# 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
#
# 你可以假设数组是非空的,并且给定的数组总是存在众数。
#
# 示例 1:
#
# 输入: [3,2,3]
# 输出: 3
#
# 示例 2:
#
# 输入: [2,2,1,1,1,2,2]
# 输出: 2
#
#
#
class Solution(object):
def majorityElement(self, nums):
res=set(nums)
n=len(nums)/2
for item in res:
if(nums.count(item)>n):
return item

这里应该是用了歪门邪道了。。。判断概率是否大于二分之一。

Leecode刷题之旅-C语言/python-169求众数的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. 五、mariadb遇到的坑——Linux学习笔记

    C#连接MySQL异常:The host localhost does not support SSL connections. 解决方案: 连接字符串添加如下语句. SslMode = none; ...

  2. 安装nginx及依赖包

    #!/bin/bash #auto zhangjia #date 20171007#安装SSL##################################################### ...

  3. photoshopcs6破解补丁用来干嘛的

    photoshopcs6破解补丁为 Adobe CS6 系列软件通用破解补丁,亲测可用,终于能用了不再出现那个烦人的购买页面了,cs6破解补丁解压后得到32和64两个文件夹,根据自己的系统类型选择,6 ...

  4. mysql主从同步与防火墙端口的设定

    一.背景: 之前做主从同步时,把从库防火墙关了,现在开启防火墙后,从库不同步了 二.解决思路: 1.首先查看了mysql占用的端口,然后开启,tcp/udp都开了,结果还是不行 firewall-cm ...

  5. January 26 2017 Week 4 Thursday

    Wasting time is robbing yourself. 浪费时间就是掠夺自己. Wasting time is not only robbing yourself, moreover, i ...

  6. python实现读取类别频数数据画水平条形图

    1.数据分组-->频数分布表 环境配置: import pandas as pd import numpy as np import matplotlib.pyplot as plt 按照你设定 ...

  7. 关于LDA的文章

    转:http://www.zhizhihu.com/html/y2011/3228.html l  Theory n  Introduction u  Unsupervised learning by ...

  8. Java基础之二维数组的回顾

    class ArrayWork { /* * 二维数组的复习! * * 2014年4月2日 21:45:50 * * * **/ public static void main(String[] ar ...

  9. 将POST请求转换为DELETE、PUT等请求的方法

    一.在WEB工程的web.xml文件中配置HiddenHttpMethodFilter 二.form 表单中添加一个隐藏域 name="_method" value="D ...

  10. Executor线程池框架

    Executor线程池框架 new Thread()的缺点 每次new Thread()耗费性能 调用new Thread()创建的线程缺乏管理,被称为野线程,而且可以无限制创建,之间相互竞争,会导致 ...