Java [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,就成对删除即count--,最终剩下的一定就是所求的。时间复杂度:O(n)。
代码如下:
public class Solution {
public int majorityElement(int[] nums) {
int major = 0;
int count = 0;
for(int i = 0; i < nums.length; i++){
if(count == 0){
major = nums[i];
count++;
}else{
if(nums[i] == major)
count++;
else
count--;
if(count >= nums.length / 2 + 1)
return major;
}
}
return major;
}
}
Java [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 ...
- 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 ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- Java for 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 ...
- 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 ...
随机推荐
- 【BZOJ】【2844】albus就是要第一个出场
高斯消元解XOR方程组 srO ZYF Orz 膜拜ZYF…… http://www.cnblogs.com/zyfzyf/p/4232100.html /******************** ...
- 把eclipse"中文版"变成"英文版"
在Eclipse.exe当前路径下,直接新建快捷方式,右键属性,添加如下参数即可 eclipse.exe -nl en
- DIY Ruby CPU 分析——Part I
[编者按]原文作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是DIY Ruby CPU Profiling 的第 ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- hdu 1288 Hat's Tea
这个要慢慢理解…… ;}
- VisualSvn+TortoiseSVN的安装说明
一.VisualSvn安装及简单使用 下载VisualSvn:VisualSVN-Server-2.5.5.msi 系统:Win7系统 安装步骤: 1.下载VisualSVN-Server-2.5.5 ...
- Thread的第五天学习
1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如:卖票系统就可以这么做! package com.thread.demo; publi ...
- http怎样保持有状态?
HTTP协议的特点 HTTP协议是无状态的协议,发送的请求不记录用户的状态,不记录用户的信息.就相当于它被访问了2次,不知道是哪两人访问的,或者是一个人访问两次. 正是因为HTTP协议的这一特点,用户 ...
- 什么叫非阻塞io
而一个NIO的实现会有所不同,下面是一个简单的例子: ByteBuffer buffer = ByteBuffer.allocate(48); int bytesRead = inChannel.re ...
- 电容值E系列标称方法
本节首先介绍常用的E系列标称方法,然后介绍电阻.电容器.电感器.二极管的分类.性能和识别方法,以及简单的实用电路. 一.E系列标称方法 厂家生产的电阻器,并不是包含任何阻值,就像人民币,只有1.2.5 ...