check the element in the array occurs more than half of the array length
Learn this from stackflow.
public class test {
public static void main(String[] args) throws IOException
{
int [] a={1,2,3,4,4,4,5,4,4};
int r=GetMajorElement(a);
System.out.println(r);
}
//check the element in the array occurs more than half of the array length
public static int GetMajorElement(int[] a)
{
int r=a[0];
int count=1;
for(int i=1;i<a.length;i++)
{
if(r==a[i])
count++;
else count--;
if(count==0)
{ r=a[i]; count++;}
}
return r>=a.length/2?r:-1;
}
}
check the element in the array occurs more than half of the array length的更多相关文章
- Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] }的差别
Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] } Array.new(size, default_object) creates an arr ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- Why does typeof array with objects return “Object” and not “Array”?
https://stackoverflow.com/questions/4775722/check-if-object-is-an-array One of the weird behaviour a ...
- LeetCode Array Easy 167. Two Sum II - Input array is sorted
Description Given an array of integers that is already sorted in ascending order, find two numbers s ...
- LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑
Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)
Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展(实例)
(1)clean var arr = [1,2,null,3,'']; alert(Ext.Array.clean(arr)); //clean的对象:(value === null) || (val ...
随机推荐
- 如何高效部署前端代码,如css,js...
看了网上一些文章,做了点总结,顺便再加点自己的东西,简单的说下. 1.利用浏览器的304缓存,但是304叫协商缓存,还是需要与服务器通信一次 2.强制使用浏览器使用本地缓存(cache-control ...
- js倒计时防页面刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php表单中如何获取单选按钮与复选按钮的值
php代码中获取表单中单选按钮的值:(单选按钮只能让我们选择一个,这里有一个"checked"属性,这是用来默认选取的,我们每次刷新我们的页面时就默认为这个值.) 例:<fo ...
- new 小记
new运算符 能根据需求来创建对象的实例 通过与构造函数和一系列初始化过程中使用的可选参数来创建对象的实例,对象创建完成后,新创建的对象继承自构造函数的原型 function Person(name) ...
- js的基本数据类型有哪些?
js的基本数据类型有哪些? ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined.Null.Boolean.Number和String.还有1中复杂的数据类型----O ...
- 移动端调试工具-Debuggap
随着移动互联网的迅速崛起,开发移动应用程序越来越多,但如果在移动端开发应用程序需要调试时,额- 仿佛又回到了IE时代,最方便也只能到处 alert 来调试.目前已经有一款产品可以做到这一点,比如pho ...
- spring和mybatis整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- javascript 自定义类型 属性,方法
<html> <head> <script type="text/javascript"> function member(name,gende ...
- Java基础之子类父类属性覆盖
当java的子类和父类具有相同名字的属性时,到底java是怎么处理的. 先看代码: package com.joyfulmath.study.field; public class Person { ...
- Xcode模拟器和真机生成的日志查看(转载)
在进行实际代码开发的过程中,我们会生成一些plist文件,但是如何在调试过程中查看这些plist文件是否被成功生成以及生成的内容是否正确? 如果查看模拟器生成的日志和真机生成的日志到底如何查看? DE ...