QUESTION

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.

FIRST TRY

每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。

class Solution {
public:
int majorityElement(vector<int> &num) {
int ret;
int nTimes = ;
for(int i = ; i < num.size(); i++){
if(nTimes == )
{
ret = num[i];
nTimes++;
}
else if(ret == num[i]) nTimes++;
else nTimes--; //2 different number, delete
}
return ret;
}
};

Result: Accepted

可扩展到⌊ n/k ⌋的情况,每k个不同的element进行成对删除。

Majority Element(ARRAY-BINARY SEARCH)的更多相关文章

  1. Implement the hash table using array / binary search tree

    今天在复习Arrays and String 时看到一个很有趣的问题.希望跟大家分享一下. Implement the hash table using array / binary search t ...

  2. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  3. 169. Majority Element (Array)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. [LeetCode] questions conclusion_ Binary Search

    Binary Search T(n) = T(n/2) + O(1)   =>    T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...

  5. Binary Search - Jump on the Stones

    Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as hal ...

  6. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  7. LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array

    原题链接在这里:https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/ 题目: G ...

  8. Binary search for the first element greater than target

    We all know how to search through an array for an element whose value equals the target value, but h ...

  9. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  10. (Array)169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. jenkins 执行python脚本 断言失败就可以构建失败

    可以配合try: 那个语句去搭配

  2. 杂项:GIT (分布式版本控制系统)

    ylbtech-杂项:GIT (分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git的读音为/gɪt/.Git是一个开源的分布式版本控制系统 ...

  3. 第15章 高并发服务器编程(2)_I/O多路复用

    3. I/O多路复用:select函数 3.1 I/O多路复用简介 (1)通信领域的时分多路复用 (2)I/O多路复用(I/O multiplexing) ①同一线程,通过“拨开关”方式,来同时处理多 ...

  4. spark1.0属性配置以及spark-submit简单使用

    在spark1.0中属性支持三种配置方式: 1.代码 在代码中构造SparkConf时指定master.appname或者key-value等 val conf = new SparkConf(); ...

  5. C# DbHelperSQL 类,从东软生成器提取而来

    DBHelerSQL,可以结合东软生成器写简单三层结构,也可以自己去调用执行相关SQL语句 直接上代码: using System; using System.Collections; using S ...

  6. tcpdump抓sql语句

    -A -n -i any |grep --color 'system_type' -n2 -- -E..,.@.@.f........ ...Ndh-....GP..:A.............. ...

  7. elk6.22

    启动错误: 参考网站:https://blog.csdn.net/feinifi/article/details/73633235?utm_source=itdadao&utm_medium= ...

  8. can't load package the specified module could not be found

    can't load package the specified module could not be found 用 Dependency Walker 2.2Dependency Walker ...

  9. WP8.1 发送邮件

    Method 1: Windows.System.Launcher.LaunchUriAsync(new Uri("abc@outlook.com?subject=hello world&a ...

  10. 原生js实现三级复选框

    工作中要做一个三级的复选框,用js实现了一下,从项目中把相关代码抽取出来了,有相关需求的可以参考一下.亲测可用. <!DOCTYPE html> <html> <head ...