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. Bootstrap:教程、简介、环境安装

    ylbtech-Bootstrap:教程.简介.环境安装 1. Bootstrap 教程返回顶部 1. Bootstrap 教程 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.B ...

  2. [转]Outlook HTML渲染

    转自:http://www.cnblogs.com/dolphinX/p/4081828.html 是不是很讨厌为Email代码兼容Outlook? 太遗憾了!虽然光都有尽头,但Outlook始终存在 ...

  3. 【转载】CSS + DIV 实现局部布局

    HTML CSS + DIV实现局部布局 1.本章教大家掌握2种布局方式: 1)顶部导航菜单布局,效果图: 2)购物版块布局,效果图: 2.技术目标:使用div + ul-li实现导航菜单布局    ...

  4. call和apply,bind的区别专讲

    可以干什么? 改变函数内的this指向: 什么时候使用? 构造函数使用this 为什么使用? 为了生成对象 类(函数名不可以带括号).call()      因为this指向对象,所以call的第一个 ...

  5. 第3章 文件I/O(4)_dup、dup2、fcntl和ioctl函数

    5. 其它I/O系统调用 (1)dup和dup2函数 头文件 #include<unistd.h> 函数 int dup(int oldfd); int dup2(int oldfd, i ...

  6. 【Python编程:从入门到实践】chapter5 if语句

    chapter5 if语句5.1 一个简单示例cars = ['audio','bmw','subaru','toyota'] for car in cars:if car == 'bmw':prin ...

  7. Android ffmpeg rtmp(source code)

    souce code: Android.mk 编译生成APK需要调用的so文件 LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODU ...

  8. web基础 (一) http协议

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

  9. 深入分析Java Web技术内幕 修订版 pdf

    百度网盘:http://pan.baidu.com/s/1slHCCw9

  10. windows环境下简单Jenkins持续集成搭建

    Jenkins是基于Java开发的持续集成工具,所以在安装Jenkins之前我们要确定电脑上已经安装了Java JDK并且环境变量配置正确,否则在启动使用java -jar Jenkins.war启动 ...