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. HUD 1175 连连看

    连连看 Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  2. centos 安装LAMP环境后装phpmyadmin

    首先在CentOS 上安装EPEL 要想安装EPEL,我们先要下载EPEL的rpm安装包. 1. 确认你的CentOS 的版本 首先通过以下命令确认你的CentOS 版本 $ cat /etc/Red ...

  3. TensorFlow函数:tf.FIFOQueue队列

    转载:https://blog.csdn.net/akadiao/article/details/78552037 tf.FIFOQueue tf.FIFOQueue继承基类QueueBase. Qu ...

  4. MongoDB对Javascript的支持

    在项目中MongoDB的Map-Reduce功能做了许多统计任务,在重构代码的时候修改了_id对象里面的属性字段名称,当用db.collection.update({$rename:{"_i ...

  5. Mybatis 接口绑定

    MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...

  6. 长沙雅礼中学集训-------------------day2

    怎么说呢,今天的题特别的神奇,因为emmmmmm,T1看不懂(一直到现在还没有理解明白期望概率什么的),T2题面看不懂+扩展欧几里得求逆元怎么求我忘了,T3哇,终于看懂一题了,然而写了个50分的程序但 ...

  7. 【Unix网络编程】chapter3 套接字编程简介

    chapter3套接字编程简介3.1 概述 地址转换函数在地址的文本表达和他们存放在套接字地址结构中的二进制值之间进行转换.多数现存的IPv4代码使用inet_addr和inet_ntoa这两个函数, ...

  8. php如何高效的读取大文件

    通常来说在php读取大文件的时候,我们采用的方法一般是一行行来讲取,而不是一次性把文件全部写入内存中,这样会导致php程序卡死,下面就给大家介绍这样一个例子. 需求:有一个800M的日志文件,大约有5 ...

  9. web基础 (一) http协议

    一.什么是web服务 浏览器与网页服务端发起的请求与回应(返回的是一堆字符串,浏览器去渲染生成页面!)都是 标准的CS模式 ---- bs模式:客户端用浏览器即可,服务端需要自己去写 http协议是建 ...

  10. php基础和数据库

    服务器和客户端 客户端 程序: 通过浏览器直接运行 服务器 程序: 通过安装某种服务器软件   程序才可以运行              apache   php文件              tom ...