设为首页 加入收藏

TOP

Majority Number
2015-07-22 20:10:13 来源: 作者: 【 】 浏览:11
Tags: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

.

?

解法

    int majorityNumber(vector
   
     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; }
   

算法解释

一个思路,同时删除两个不同的数,那么结果不会变

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++哈希表 下一篇[数位dp] hihoCoder 1033 交错和

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: