数组中出现次数超过一半的数字
来源:互联网 发布:什么是网络综艺节目 编辑:程序博客网 时间:2024/06/03 20:25
#include<iostream>#include <vector>using namespace std;int partition(vector<int>arr, int begin, int end){ int pivot = arr[begin]; while (begin<end) { while (begin<end&&arr[begin]<=pivot) begin++; while (begin<end&&arr[end]>=pivot) end--; if (begin<end) swap(arr[begin], arr[end]); } swap(pivot, arr[begin]); return begin;}int findOverHalf(vector<int>arr){ int begin = 0, end = arr.size(); int mid = arr.size() >> 1; int idx = partition(arr, begin, end); while (idx != mid) { if (idx>mid) { end = idx - 1; idx = partition(arr, begin, end); } else if (idx<mid) { begin = idx + 1; idx = partition(arr, begin, end); } } return arr[idx];}int main(){ int n; while (cin >> n) { vector<int>arr; int tmp; for (int i = 0; i<n; ++i){ cin >> tmp; arr.push_back(tmp); } cout << findOverHalf(arr) << endl; } return 0;}
阅读全文
0 0
- 数组中超过出现次数超过一半的数字
- 74.数组中超过出现次数超过一半的数字
- 74 数组中超过出现次数超过一半的数字
- 【数组4】数组中出现次数超过一半的数字
- 数组4:数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 找出数组中出现次数超过长度一半的数字
- 2-数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字[算法]
- 数组中出现次数超过一半的数字
- 找出数组中出现次数超过一半的数字--百度
- 数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- 寻找数组中出现次数超过一半的数字
- 面试训练数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字
- [C++ Primer Plus]学习笔记--每次读取一行字符串输入
- js获取当前时间的年月日时分秒以及时间的格式化
- spring学习之spring简介
- 简单调试前端 html js css woff ok
- C++ 面试整理之一
- 数组中出现次数超过一半的数字
- 牛客网算法学习笔记-智力题
- show slave status \G 分析
- 在两个模态框中保存数据并实现文件上传问题
- [LeetCode198 House Robber]数组中的动态规划求最值问题
- ThreadLocal理解
- Spring+Log4J.xml在web中的使用方法
- redis安装
- git更新和删除文件代码