Leetcode_169_Majority Element
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42247887
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.
思路:
(1)题意为给定一个整形数组,寻找出现次数大于数组长度一半的数字。
(2)这道题很简单。如果你对Arrays类比较熟悉的话,可以先运用其中的sort()方法对数组排序,排完序后再从左到右遍历遍历数组,并设置count记录遍历数字出现的次数,如果大于数组长度一半则返回。
(3)这道题如果不用上述方法,可以只遍历一次就得到结果,这里不再啰嗦了,详见下方代码。
(4)希望本文对你有所帮助。
算法代码实现如下:
public static int majorityElement(int[] num) {
if(num==null ||num.length==0) return-1;
if(num!=null &&num.length==1) return num[0];
Arrays.sort(num);
int count = 1;
int flag = 0;
for (int i = flag+1; i < num.length; i++) {
if(num[flag]==num[i]){
count++;
if(count>num.length/2){
return num[flag];
}
}else{
flag=i;
count=1;
}
}
return -1;
}
只遍历一次算法的代码实现如下:
public static int majorityElement(int[] num) {
if (num == null || num.length == 0)
return -1;
int count = 0;
int index = 0;
for (int i = 0; i < num.length; ++i) {
if (index == 0)
count = num[i];
if (count == num[i])
++index;
else
--index;
}
return count;
}
Leetcode_169_Majority Element的更多相关文章
- Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .
例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...
- 【解决方案】cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-r
[JAVA错误] cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One o ...
- WebComponent魔法堂:深究Custom Element 之 从过去看现在
前言 说起Custom Element那必然会想起那个相似而又以失败告终的HTML Component.HTML Component是在IE5开始引入的新技术,用于对原生元素作功能"增强& ...
- WebComponent魔法堂:深究Custom Element 之 标准构建
前言 通过<WebComponent魔法堂:深究Custom Element 之 面向痛点编程>,我们明白到其实Custom Element并不是什么新东西,我们甚至可以在IE5.5上定 ...
- WebComponent魔法堂:深究Custom Element 之 面向痛点编程
前言 最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:"资料99%是英语 ...
- 深入理解DOM节点类型第五篇——元素节点Element
× 目录 [1]特征 [2]子节点 [3]特性操作[4]attributes 前面的话 元素节点Element非常常用,是DOM文档树的主要节点:元素节点是html标签元素的DOM化结果.元素节点主要 ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...
- MongoDB查询转对象是出错Element '_id' does not match any field or property of class
MongoDB查询转对象是出错Element '_id' does not match any field or property of class 解决方法: 1.在实体类加:[BsonIgno ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- SpringMVC格式转化错误之HTTP Status [400] – [Bad Request]
SpringMVC中,如果直接为Date类型的属性赋值,服务器有可能会报HTTP Status [400] – [Bad Request] Type Status Report Description ...
- Dockerfile怎么创建镜像
编写完成 Dockerfile 之后,可以通过 docker build 命令来创建镜像. 基本的格式为 docker build [选项] 路径,该命令将读取指定路径下(包括子目录)的 Docker ...
- 微信小程序基础之开源项目库汇总
awesome-github-wechat-weapp 是由OpenDigg整理并维护的微信小程序开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. (链接:ht ...
- nginx 日志分析工具goaccess
参考:https://www.goaccess.io/download 安装 $ wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz $ tar -xz ...
- SpringBatch前言
批处理应用程序就是对程序进行批量处理. 特点:批量应用程序处理大量数据而无需人类干预. 用途:可以选择使用批处理程序来计算每月的财务报表.计算统计和索引文件的数据等等. 应用:当银行系统中有很多业务都 ...
- RunLoop总结:RunLoop的应用场景(四)
今天要介绍的RunLoop使用场景很有意思,在做长期项目,需要跟踪解决用户问题非常有用. 使用RunLoop 监测主线程的卡顿,并将卡顿时的线程堆栈信息保存下来,下次上传到服务器. 参考资料 关于今天 ...
- Playground中格式注释语法
类似于Ruby的ruby document,Xcode的Playground自身也提供一些嵌入文档中的格式注释的语法. 我们先定义一个简单的类: class A{ } 按住opt点击class A,你 ...
- Scala:类,对象和特征(接口)
http://blog.csdn.net/pipisorry/article/details/52902609 Scala类和对象 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象 ...
- Spring整合DWR comet 实现无刷新 多人聊天室
用dwr的comet(推)来实现简单的无刷新多人聊天室,comet是长连接的一种.通常我们要实现无刷新,一般会使用到Ajax.Ajax 应用程序可以使用两种基本的方法解决这一问题:一种方法是浏览器每隔 ...
- Spark技术内幕: Shuffle详解(三)
前两篇文章写了Shuffle Read的一些实现细节.但是要想彻底理清楚这里边的实现逻辑,还是需要更多篇幅的:本篇开始,将按照Job的执行顺序,来讲解Shuffle.即,结果数据(ShuffleMap ...