LeetCode 169
2016-05-10 19:08
225 查看
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.
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.
/************************************************************************* > File Name: LeetCode169.c > Author: Juntaran > Mail: Jacinthmail@gmail.com > Created Time: Tue 10 May 2016 02:40:25 PM CST ************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include<stdio.h> int majorityElement( int* nums, int numsSize ) { int ret = nums[0]; int count = 1; int i; for( i=1; i<numsSize; i++ ) { if( ret == nums[i] ) { count ++; } else { count --; } if( count == 0 ) { ret = nums[i]; count ++; } } return ret; } int main() { int nums[] = {3,2,3}; int numsSize = 3; int ret = majorityElement( nums, numsSize ); printf("%d\n", ret); return 0; }
相关文章推荐
- flask笔记:2:Hello World
- Table of Contents - Lombok
- nginx php-fpm 小VPS 优化
- 巧用Singleton(单例)
- 连接oracle数据库
- Linux操作系统几个常用知识点
- CodeForces 165C Another Problem on Strings(组合)
- OpenCv训练降低CPU占用率
- 嵌入式linux-dropbear连接时重复输入密码
- 欧拉函数的经典公式
- bufferevent简介
- HDOJ/HDU 2535 Vote(排序、)
- flask笔记:1:安装
- HDOJ/HDU 2535 Vote(排序、)
- iOS UIKit:TableView之表格创建(1)
- 关于VS2015找不到WIN32的解决办法
- UI、ID、UE和GUI,这些都是什么
- 1008 数组元素循环右移问题
- linux常用命令
- 订单