LeetCode--169. Majority Element (Divide-and-Conquer)
来源:互联网 发布:硕士出国读博 知乎 编辑:程序博客网 时间:2024/06/07 01:48
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.
采用分治算法 (Divide-and-Conquer algorithms)class Solution {public: int majorityElement(vector<int>& nums) { return Maj(nums, 0, nums.size() - 1); }private: int Maj(vector<int>& nums, int left, int right) { if (left == right) { return nums[left]; } int mid = left + ((right - left) >> 1); int leftmaj = Maj(nums, left, mid); int rightmaj = Maj(nums, mid + 1, right); if (leftmaj == rightmaj) { return leftmaj; } return count(nums.begin() + left, nums.begin() + right + 1, leftmaj) > count(nums.begin() + left, nums.begin() + right + 1, rightmaj) ? leftmaj : rightmaj; }};
阅读全文
0 0
- LeetCode--169. Majority Element (Divide-and-Conquer)
- Divide and Conquer -- Leetcode problem169: Majority Element
- LeetCode 169.Majority Element--Divide and Conquer(分治法)
- 169. Majority Element (divide and conquer)
- Divide and Conquer:169. Majority Element
- [week 14][Leetcode][Divide and Conquer] Majority Element
- [leetcode] Divide and Conquer
- leetcode-Divide and Conquer
- LeetCode--Divide and Conquer
- LeetCode Devide & Conquer Problem || Majority Element
- leetCode刷题归纳-Divide and Conquer(215. Kth Largest Element in an Array)
- LeetCode OJ|Divide and Conquer|Kth Largest Element in an Array
- [week 16][Leetcode][Divide and Conquer] Kth Largest Element in an Array
- LeetCode 215. Kth Largest Element in an Array--Divide and Conquer(分治法)
- Divide and Conquer -- Leetcode problem215: Kth Largest Element in an Array
- leetcode Decode Ways Divide and Conquer
- LeetCode之Divide and Conquer题目汇总
- Divide and Conquer -- Leetcode problem53. Maximum Subarray
- 指针变量的数据类型
- 按钮选择,点击换颜色
- 快速排序的底层递归优化和针对基本有序序列的优化
- 备份数据测试有必要吗? | A.12.3.1信息备份
- jqueryde的用户名+密码验证
- LeetCode--169. Majority Element (Divide-and-Conquer)
- MySQL
- 表格用户名密码判断自动切换光标
- 练习3
- ubuntu 安装记录 2017年 09月 10日 星期日
- 天天爱跑步
- 用slice实现分页效果
- python 微积分计算-未懂
- 2017 ACM/ICPC Asia Regional Shenyang Online 1005 & hdu6198:number number number