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 ...
随机推荐
- appiu 笔记
1.要在手机上输入字符, 要屏蔽手机键盘 于是可以想办法屏蔽软键盘,只需在desired_caps{}设置里面加两个参数 unicodeKeyboard是使用unicode编码方式发送字符串reset ...
- POJ 3268 Silver Cow Party 最短路径+矩阵转换
Silver Cow Party Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- [UE4]C++的const类成员函数
我们知道,在C++中,若一个变量声明为const类型,则试图修改该变量的值的操作都被视编译错误.例如: const char blank = ‘’; blank = ‘\n’; // 错误 要声明一个 ...
- 小项目,吃货联盟,java初级小项目,源代码
1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...
- 伯克利推出「看视频学动作」的AI智能体
伯克利曾经提出 DeepMimic框架,让智能体模仿参考动作片段来学习高难度技能.但这些参考片段都是经过动作捕捉合成的高度结构化数据,数据本身的获取需要很高的成本.而近日,他们又更进一步,提出了可以直 ...
- 了解ES6
内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 6.函数相关 内容参考:<ES6 标准入门> ES6标准阅读链 ...
- Kafka集群扩展以及重新分布分区
我们往已经部署好的Kafka集群里面添加机器是最正常不过的需求,而且添加起来非常地方便,我们需要做的事是从已经部署好的Kafka节点中复制相应的配置文件,然后把里面的broker id修改成全局唯一的 ...
- Activity服务类-1 DynamicBpmnService服务类
这个服务是5.19版本后新增的一个服务,和RepositoryService的作用相似,都是与流程定义有关,但是却完全不同.从名字上来看是动态的BPMN服务,看里面的方法都是改变流程的相关属性.这个方 ...
- UI5-文档-1-前言
主要是将SAP UI5官网文档做下了解,相关内容请查阅:https://sapui5.hana.ondemand.com/#/topic 设置您的开发环境并阅读我们的教程.它们使用交互式格式中的实际示 ...
- Java多线程之使用ATM与柜台对同一账户取钱
钱数要设置成静态的变量,两种取钱方式操作的是同一个银行账户! 废话不多说,直接上代码.注释写的都很详细!!! package com.thread.multi2; public class Bank ...