找到以后要再扫一遍确认。

http://zhedahht.blog.163.com/blog/static/25411174201085114733349/

#include <iostream>
#include <memory.h>
#define LEN 100005
#define ulong unsigned long long
using namespace std; ulong A[LEN]; int main()
{
int n;
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
int count = 0;
ulong candidate = 0;
for (int i = 0; i < n; i++)
{
if (count == 0)
{
count++;
candidate = A[i];
}
else if (candidate == A[i])
{
count++;
}
else
{
count--;
}
}
count = 0;
for (int i = 0; i < n; i++)
{
if (A[i] == candidate) count++;
}
if (count * 2 > n)
{
cout << candidate << endl;
}
else
{
cout << -1 << endl;
}
}
return 0;
}

  

[jobdu]数组中出现次数超过一半的数字的更多相关文章

  1. 九度OJ 1370 数组中出现次数超过一半的数字

    题目地址:http://ac.jobdu.com/problem.php?pid=1370 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2 ...

  2. 数组中出现次数超过一半的数字 -java

    数组中出现次数超过一半的数字 -java 方法一: 数组排序,然后中间值肯定是要查找的值. 排序最小的时间复杂度(快速排序)O(NlogN),加上遍历. 方法二: 使用散列表的方式,也就是统计每个数组 ...

  3. 【C语言】统计数组中出现次数超过一半的数字

    //统计数组中出现次数超过一半的数字 #include <stdio.h> int Find(int *arr, int len) { int num = 0; //当前数字 int ti ...

  4. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  5. 《剑指offer》— JavaScript(28)数组中出现次数超过一半的数字

    数组中出现次数超过一半的数字 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超 ...

  6. 《剑指offer》第三十九题(数组中出现次数超过一半的数字)

    // 面试题39:数组中出现次数超过一半的数字 // 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例 // 如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, ...

  7. 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字

    剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...

  8. 剑指Offer:数组中出现次数超过一半的数字【39】

    剑指Offer:数组中出现次数超过一半的数字[39] 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于这 ...

  9. 编程算法 - 数组中出现次数超过一半的数字 代码(C)

    数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. ...

随机推荐

  1. JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue

    An enum type is annotated with XmlEnum. It has an optional element value of type java.lang.Class whi ...

  2. SSH免密码登录设置

    我们使用ssh-keygen在ServerA上生成private和public密钥,将生成的public密钥拷贝到远程机器ServerB上后,就可以使用ssh命令无需密码登录到另外一台机器Server ...

  3. ###Linux基础 - 3

    点击查看Evernote原文. #@author: gr #@date: 2014-10-15 #@email: forgerui@gmail.com 一.GCC编译程序 在Windows下使用Vis ...

  4. Markdown 生成目录

    <link rel="stylesheet" href="http://yandex.st/highlightjs/6.2/styles/googlecode.mi ...

  5. 2.1 JavaScript应用开发实践指南

    创建交互层 循环 示例代码如下: var people = family, peopleCount = items.length, i; if(peopleCount>0){ for(i=0; ...

  6. js个人笔记

    一.删除元素 <!DOCTYPE html> <html> <head> <title>删除元素</title> </head> ...

  7. [xUnit]尝试单元测试

    参考: http://xunit.github.io/ http://shouldly.readthedocs.org/en/latest/ 创建测试项目工程,类型选择类库,在NuGet中搜索xuni ...

  8. 如何在Markdown、HTML编辑器上输入一些特殊字符

    如何输入EntityCode 参考: 1.EntityCode 2.Common HTML entities used for typography 3.Latin Supplement-拉丁补充

  9. wait(...) notify() notifyAll()

    简介 wait.notify.notifyAll是Java中3个与线程有关的方法,它们都是Object类中的方法. 其中,wait方法有3个重载形式: 1.wait() 2.wait(long tim ...

  10. 第9条:覆盖equals时总要覆盖hashCode

    在每个覆盖equals方法的类中,也必须覆盖hashCode方法.否则,会违反Object.hashCode的通用约定,从而导致该类无法结合所有基于散列的集合一起正常工作,包括HashMap,Hash ...