Leecode刷题之旅-C语言/python-169求众数
/*
* @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求众数的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- centos7主机名的修改
在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵活的(pretty).“静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始 ...
- Python学习---重点模块之json
注意:JSON不能转换类,不能转换函数 重点方法示例 json.dumps():实现文件写入,字符串转换[写入文件当然是JSON字符串楼] 实际上,json.dumps()只是帮我们做了一个字符串的转 ...
- echarts tooltip太多会超出显示范围-解决
以下两种解决方式,能够应对大部分出现此类问题项目 tooltip.position: 提示框浮层的位置,默认不设置时位置会跟随鼠标的位置. 可选:string, Array, Function 一.解 ...
- C/C++内存泄露检测
以下测试基于的gcc版本: gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4Copyright (C) 2013 Free Software Foundation, In ...
- jQuery解决高度统一问题
<div class="itemdl over"> <dl class="fl"> <dt><img src=&quo ...
- 高CPU业务
高CPU业务 Gearman是当年LiveJournal用来做图片resize的,大家也明白图片resize是一个高CPU的操作,如果让web网站去做这个高CPU的功能,有可能会拖垮你的 web应用, ...
- Vue-Quill-Editor插件插入图片的改进
最近在做一个Vue-Clie小项目,使用到了Vue-Quill-Editor这个基于Vue的富文本编辑器插件.这个插件跟Vue契合良好,使用起来比其他的诸如百度UEditor要方便很多,但是存在一个小 ...
- 【2^k进制数】
发现自己推得组合数好像不太一样 先把这个复杂的柿子写一遍 \[\sum_{i=2}^{\left \lfloor\frac{n}{k}\right \rfloor}C_{2^k-1}^{i}+\sum ...
- PHP------知识复习
PHP概述 (1)PHP(Hypertext Perprocessor)超级文本预处理器 (2) PHP是一种在服务器端执行的嵌入HTML文档的脚本语言 (3) 是一种网站开发语言(B/S结构) ( ...
- [19/04/08-星期一] 多线程_线程的优先级(Priority) 和 守护线程(Daemon)
一.概念 1. 处于就绪状态的线程,会进入“就绪队列”等待JVM来挑选. 2. 线程的优先级用数字表示,范围从1到10,一个线程的缺省优先级是5. 3. 使用下列方法获得或设置线程对象的优先级. in ...