LeetCode Majority Element (c++)
来源:互联网 发布:多级审批数据库设计 编辑:程序博客网 时间:2024/05/19 19:41
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.
class Solution {public: int majorityElement(vector<int> &num) { // int n = num.size(); 注释的这三行也是一种solution,不过时间要翻翻,原因主要在于sort()方法 // sort(num.begin(),num.end()); // return num[n/2]; int nTimes = 0; int candidate = 0; for(int i = 0; i < num.size(); i ++) { if(nTimes == 0) { candidate = num[i]; nTimes = 1; } else { if(candidate == num[i]) nTimes ++; else nTimes --; } } return candidate; }};
0 0
- LeetCode Majority Element (c++)
- [C++]LeetCode: 50 Majority Element
- [leetcode]Majority Element C语言
- [leetcode]C语言:169 Majority Element
- [C语言][LeetCode][169]Majority Element
- [leetcode-229]Majority Element II(c++)
- [leetcode-229]Majority Element II(c)
- [LeetCode练习题-C语言]169. Majority Element
- LeetCode 169. Majority Element 题解(C++)
- 【LeetCode-229】Majority Element II(C++)
- 【leetcode】Majority Element II,Majority element algorithm
- Leetcode: Majority Element &Majority Element II
- Leetcode Majority Element
- leetcode 169-Majority Element
- 【leetcode 哈希表】Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- leetcode: Majority Element
- 1017. Queueing at Bank (25)
- 李锐_hbase上搭建广告实时数据处理平台
- 执行sql语句中 为什么应该尽量减少“SELECT *”这类语句的使用
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
- 如何设置 PHP.ini 让你的PHP更安全
- LeetCode Majority Element (c++)
- x86架构中的保护模式
- Ajax实现文件下载
- [IOS]icon与launch的命名和使用
- Val编程-val系列编程思想
- oracle 删除数据
- java 实现多个文件打包成zip的功能
- 程序员自我培训指南
- Socket bind failed: [730048]