Majority Element(ARRAY-BINARY SEARCH)
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)的更多相关文章
- Implement the hash table using array / binary search tree
今天在复习Arrays and String 时看到一个很有趣的问题.希望跟大家分享一下. Implement the hash table using array / binary search t ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- 169. Majority Element (Array)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
- Binary Search - Jump on the Stones
Binary Search algorithm. Wikipedia definition: In computer science, binary search, also known as hal ...
- [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 ...
- 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 ...
- 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 ...
- [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. 这道 ...
- (Array)169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 有意思的bug
1. Xss攻击型的bug Xss攻击即跨站脚步攻击,通过插入恶意脚本 ,实现对用户浏览器的控制. Bug现象:新增物品时,物品名称输入一段JavaScript代码,在提交时此代码被执行.如:输入&l ...
- httpclient 用户名密码认证实例
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.com ...
- python selenium-5根据unittest组织测试用例
driver:浏览器driver存放地址 testcase:测试用例目录 report:测试结果保存目录 runtest.py:执行文件 test_search1.py搜索selenium,test_ ...
- IP地址与子网掩码
IP地址 众所周知,为了确保通信时能相互识别,在internet上的每台主机都必须有一个唯一的标识,即主机的IP地址.IP协议就是根据IP地址来实现信息传递的. IP地址由32位(4字节)二进制数组成 ...
- mac osx下虚拟主机配置
1.打开“终端(terminal)”,输入 sudo apachectl -v,此指令显示apache版本 2.开启apache,输入 sudo apachectl start ...
- 第3章 文件I/O(3)_内核数据结构、原子操作
3. 文件I/O的内核数据结构 (1) 内核数据结构表 数据结构 主要成员 文件描述符表 ①文件描述符标志 ②文件表项指针 文件表项 ①文件状态标志(读.写.追加.同步和非阻塞等状态标志) ②当前文件 ...
- 使用alter database datafile 'XXX' offline drop 是否能够恢复(非归档模式下)
今天在群里面听到一位网友在说使用了alter database datafile 'XXX' offline drop命令是否能够恢复数据,在非归档模式下,下面是用一个实验来验证一下 ######## ...
- ajax json用法 上传文件 登录
1. json json 是一种数据结构 跨平台跨语言 1. python中json数据的转换 1.数据类型 字符串 数字 布尔值 列表 字典 None 2. 序列化 ...
- File处理
package com.cfets.ts.u.shchgateway.util; import java.io.BufferedInputStream; import java.io.Buffered ...
- 不规则ROI的提取
在网上看到基于opencv3.0之前的API实现不规则ROI的提取,我自己试了一下发现opencv3.0不行,第一想法是我写的有问题,最后发现是API的改版.原理很简单. 目标:提取黑线作为ROI 原 ...