Majority Number
题目描写叙述
Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it..
Example
Given [1, 1, 1, 1, 2, 2, 2], return 1
Challenge
O(n) time and O(1) extra space
.
链接地址
http://www.lintcode.com/en/problem/majority-number/
解法
    int majorityNumber(vector<int> nums) {
        // write your code here
        int ret, num = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (num == 0) {
                ret = nums[i];
                num++;
            } else {
                if (nums[i] == ret) {
                    num++;
                } else {
                    num--;
                }
            }
        }
        return ret;
    }
算法解释
一个思路。同一时候删除两个不同的数。那么结果不会变
Majority Number的更多相关文章
- [LintCode] Majority Number 求众数
		
Given an array of integers, the majority number is the number that occurs more than half of the size ...
 - Majority Number I & || && |||
		
Majority Number Given an array of integers, the majority number is the number that occurs more than ...
 - Lintcode: Majority Number III
		
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
 - Lintcode: Majority Number II
		
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
 - leetcode majority number
		
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
 - lintcode 中等题:majority number III主元素III
		
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
 - lintcode 中等题:Majority number II 主元素 II
		
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
 - LintCode Majority Number II / III
		
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
 - [LintCode] Majority Number 求大多数
		
Given an array of integers, the majority number is the number that occurs more than half of the size ...
 - Lintcode: Majority Number II  解题报告
		
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
 
随机推荐
- 流式处理框架storm浅析(上篇)
			
本文来自网易云社区 作者:汪建伟 前言 前一段时间参与哨兵流式监控功能设计,调研了两个可以做流式计算的框架:storm和spark streaming,我负责storm的调研工作.断断续续花了一周的时 ...
 - niubi-job:一个分布式的任务调度框架设计原理以及实现
			
niubi-job的框架设计是非常简单实用的一套设计,去掉了很多其它调度框架中,锦上添花但并非必须的组件,例如MQ消息通讯组件(kafka等).它的框架设计核心思想是,让每一个jar包可以相对之间独立 ...
 - ACM-ICPC北京赛区2017网络同步赛
			
E: Cats and Fish 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. T ...
 - Android中动态改变控件的大小的一种方法
			
在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现 一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...
 - CSS编码规范(转)
			
1 前言 CSS作为网页样式的描述语言,在百度一直有着广泛的应用.本文档的目标是使CSS代码风格保持一致,容易被理解和被维护. 虽然本文档是针对CSS设计的,但是在使用各种CSS的预编译器(如less ...
 - [JSOI2016] 最佳团队 (树形DP+01分数规划)
			
Description JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号. 每个候选人都由一位编号比他小的候选人Ri推荐.如果Ri=0则说明这个候选人是JYY ...
 - 3D标签
			
动态实现3D标签, 主要代码: // // XLMatrix.h // XLSphereView // // Created by 史晶晶 on 16/4/4. // Copyright © 2016 ...
 - PAT 甲级 1003. Emergency (25)
			
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
 - 从0到1:全面理解RPC远程调用
			
上一篇关于 WSGI 的硬核长文,不知道有多少同学,能够从头看到尾的,不管你们有没有看得很过瘾,反正我是写得很爽,总有一种将一样知识吃透了的错觉. 今天我又给自己挖坑了,打算将 rpc 远程调用的知识 ...
 - webRTC windows demo1(转)
			
// setup video engine char cCameraName[MAX_CAMERA_NAME_LENGTH]; memset(cCameraName, , MAX_CAMERA_NAM ...