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 ...
随机推荐
- 使用GDI技术创建ASP.NET验证码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- SQL-SERVER学习(一) 数据表的基本操作
1.创建一个数据库,数据库的名字是UserInfo create database UserInfo; 2.创建一个数据表,数据表的名字是UserInfocreate table UserInfo( ...
- 最新-Linux常用命令大全-随时更新
一.系统管理与设置 1.信息显示命令 # man & info //帮助手册 # man 命令 //显示相应命令的帮助内容 # arch //显示当前系统体系结构 # cal //显 ...
- 如何在SAP C4C里使用ABSL消费第三方Restful API
首先我们得有一个可以正常工作的Restful API: 然后在Cloud for Customer的Cloud Application Studio里创建Restful API的模型,把第一步可以正常 ...
- 容器,Docker, Kubernetes和Kyma,以及Kyma对SAP的意义
大家好,今天非常高兴能给大家做一个关于Kyma的技术分享.这个session的audience主要是针对使用咱们成都研究院使用Java和nodejs等技术栈做微服务开发的同事们.对于在ABAP net ...
- Python解析配置文件模块:ConfigPhaser
算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...
- mac git 命令自动补全
步骤如下: 1.下载Git-completion.bash 或者直接使用SourceTree去clone到本地. 下载地址:https://github.com/markgandolfo/git-ba ...
- Windows 使用iCloud日历
作者:Lumos Night链接:https://www.zhihu.com/question/34287617/answer/97299386来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...
- Spring data JPA先排序再分页。
//工具类,增删改查等等package com.yunqing.service.impl; import java.util.Map; import org.springframework.beans ...
- CentOS gitlab 安装配置
CentOS gitlab 安装配置 2018-11-02 11:23:09 Visit 5 在/etc/yum.repos.d 目录下创建文件gitlab-ce.repo,使用国内的安装源 b ...