主元素
来源:互联网 发布:知乎日报rss 编辑:程序博客网 时间:2024/05/18 03:09
/*
问题描述:给定一个整形数组,找出主元素,它在数组中出现次数严格大于数组个数的二分之一
来源:LintCode
作者:syt
问题描述:给定一个整形数组,找出主元素,它在数组中出现次数严格大于数组个数的二分之一
来源:LintCode
作者:syt
日期:2017-7-24
思路:先排序,设置两个下标用于统计每个数字的个数,最后比较是否为主元素
*/
#include <iostream>#include <vector>#include <algorithm>using namespace std;/*** @param nums: A list of integers* @return: The majority number*/int majorityNumber(vector<int> nums) {// write your code hereint count = 1;sort(nums.begin(), nums.end());int i = 0, j = 1;for (; j < nums.size(); j++){if (nums[i] == nums[j]){count++;}else{if (count > nums.size() / 2){break;}i = j;count = 1;}}return nums[i];}
阅读全文
0 0
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素
- 主元素、主元素II、主元素III
- python_lintcode_47主元素_47主元素 II_48主元素 III
- 主元素II和主元素III
- 寻找多数元素/主元素问题
- 寻找多数元素(主元素)
- 找主元素
- linux下文件的查找
- javascript
- Point
- HBase应用Java API的HBaseAdmin来创建和编辑模式
- AsynchronousCall class
- 主元素
- searchview在toolbar中使用基础
- 事件、订阅与触发(一)
- C语言基础(三)函数
- 多继承的用法
- ACache的git地址以及简介
- C++之若所有参数皆需类型转换,请为此采用non-member函数(24)---《Effective C++》
- Android Notification创建、类型、取消、自定义、点击介绍
- BottomNavigationView 的使用