LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
分析:
遍历数组,每当发现一对儿不相同的element时就成对儿删除,最后剩下的一定是个数过半儿的element。
public class Solution {
public int majorityElement(int[] nums) {
int count = 0;
int majElem = 0;
for(int i=0; i<nums.length; i++) {
if(count == 0) {
majElem = nums[i];
count++;
} else {
if(majElem == nums[i]) {
count++;
} else {
count--;
}
}
}
return majElem;
}
}
LeetCode 169. Majority Element的更多相关文章
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- C++ 开篇
C++ 程序员历练之路 1.C++ primer 2.C++程序设计语言 C++之父的作品 3.C++标准库 STL 4.TCP/IP协议详解 共3卷 5.Oracle数据库和MySQl数据库的学习 ...
- AT指令(转)
资料来自网络 附录AT指令简编一. 一般命令1.AT+CGMI 给出模块厂商的标识.2.AT+CGMM 获得模块标识.这个命令用来得到支持的频带(GSM 900,DCS 1800 或PCS 1900) ...
- OS实验报告--FCFS算法
实验二.作业调度模拟实验 专业:商业软件工程 姓名:王泽锴 学号:201406114113 一.实验目的 (1)加深对作业调度算法的理解: (2)进行程序设计的训练. 二.实验内容和要求 (1)实验 ...
- 零基础如何自学MySQL数据库?
作者:姜健链接:https://www.zhihu.com/question/34840297/answer/67536521来源:知乎著作权归作者所有,转载请联系作者获得授权. 本人是个活生生的例子 ...
- UIView详解
MVC架构模式 MVC(Model-View-Controller)是实现数据和显示数据的视图分离的架构模式(有一定规模的应用都应该实现数据和显示的分离).其中,M代表模型,就是程序中使用的数据和 ...
- [课程设计]Scrum 1.4 多鱼点餐系统开发进度
Scrum 1.4 多鱼点餐系统开发进度 (点餐页面框架布置) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐系 ...
- linux开机启动mongodb
方式一(不推荐) ubuntu编辑/etc/rc.local /home/wyt/bin/mongodb-linux-x86_64-ubuntu1404-3.2.8/bin/mongod --dbpa ...
- C#连接操作mysql实例
第三方组件:Mysql.Data.dll说明:去官方网站下载Mysql.Data.dll,然后在项目中添加该组件的引用,在代码页里输入using Mysql.Data.MysqlClient,我们就可 ...
- linux下shell编写九九乘法表
主要语法:类似 1x2 echo $((1*2)) for 变量 in 值1 值2 值3 ;do linux命令或者语句done
- 关于使用微信登录第三方APP的实现(Android版)
使用微信登录APP,免去注册过程,现在已经有很多的类似应用了.集成该功能过程不复杂,但还是有一些地方需要注意的. 开始之前,需要做下面的准备工作. 1.到微信开放平台注册你的APP,并申请开通微信登录 ...