一:Majority Element

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.

class Solution {
public:
int majorityElement(vector<int>& nums) {
int numsSize = nums.size();
int times = ;
int res = ;
for(int i=;i<nums.size();i++){
if(i==){
res = nums[i];
times = ;
}else{
if(nums[i]!=res){
times--;
if(times==){
res = nums[i];
times = ;
}
}else{
times++;
}
}
}
return res;
}
};

二:Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

出现 ⌊ n/3 ⌋的数,在数组中至多只有两个,可以画图理解。

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int numsSize = nums.size();
int resval1 = ,resval2=;
int times1 = ,times2=;
for(int i=;i<numsSize;i++){
if(i==){
resval1 = nums[i];
times1++;
}else{
if(nums[i]==resval1){
times1++;
}else if(times2==){
times2=;
resval2 = nums[i];
}else if(nums[i]==resval2){
times2++;
}else{
times1--;
times2--;
if(times1==){
times1=;
resval1=nums[i];
}
}
}
}
int cnt1=,cnt2=;
for(int i=;i<numsSize;i++){
if(nums[i]==resval1)
cnt1++;
if(nums[i]==resval2)
cnt2++;
}
if(cnt1>(numsSize/))
res.push_back(resval1);
if(cnt2!=numsSize && cnt2>(numsSize/))
res.push_back(resval2);
return res;
}
};

Majority Element,Majority Element II的更多相关文章

  1. Is it possible to implement a Firebug-like “inspect element” DOM element highlighter with client-side JavaScript?

    Is it possible to implement a Firebug-like "inspect element" DOM element highlighter with ...

  2. 关于报错stale element reference: element is not attached to the page document处理

    1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...

  3. stale element reference: element is not attached to the page document 异常

    在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element referenc ...

  4. 报错stale element reference: element is not attached to the page document结局方案

    今天在调试脚本时,遇到如下报错: org.openqa.selenium.StaleElementReferenceException: stale element reference: elemen ...

  5. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...

  6. Leetcode # 169, 229 Majority Element I and II

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

  7. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  8. CSS选择器笔记,element element和element > element 的区别

    看官方解释 element element  例子: div p 官方解释:div内部所有的p元素 就是说 只要p在div内部.如果 p在span内部,span在div内部,p也算在div内部 < ...

  9. Raphael.js API 之Element.remove(),Element.removeData(),paper.text(),Element.node(),Element.onDragOver

    /*API-38*/ Element.remove() 删除某个元素对象,无返回值 /*API-39*/ Element.removeData([key]); 删除某个key的value值.假设没有特 ...

随机推荐

  1. SVG 和字符图标

    制作网站往往需要使用一些图标来提高用户体验,如果我们的是一些扁平化设计的图标,我们可以选择 SVG 或 图标字体来提高用户体验. 下面对这两种技术进行比较. 开发难度: 现在的在线工具非常强大,比如  ...

  2. sql server数据库区分大小写设置

    数据库表中字段alter Table TableName 区分大小写 ALTER Column ColumnName VARCHAR(50) COLLATE Chinese_PRC_CS_AS不区分大 ...

  3. python 3.5 之 单双三引号

    1. 单引号和双引号用法都是一样的,但是如果字符串里有相同的字符时要使用\进行转义 举例:1) print 'hello'2) print "hello"1和2,结果都是hello ...

  4. Z - 不容易系列之(3)―― LELE的RPG难题

    Description          人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即"可乐"),经过多方打探,某资深C ...

  5. C++输入和输出

    本文转载:blog.csdn.net/zhanghaotian2011/article/details/8868577博客 输入和输出并不是C++语言中的正式组成成分。C和C++本身都没有为输入和输出 ...

  6. CDZSC_2015寒假新人(1)——基础 e

    Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever ...

  7. android ellipsize 属性详解

    TextView中内容过长时添加省略号的属性,即ellipsize 用法如下: 在XML文件中设置: android:ellipsize = "end" //省略号在结尾 andr ...

  8. JSON入门之二:org.json的基本用法

    java中用于解释json的主流工具有org.json.json-lib与gson,本文介绍org.json的应用. 官方文档: http://www.json.org/java/ http://de ...

  9. 理解javascript 回调函数

    ##回调函数定义 百度百科:回调函数 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不 ...

  10. thinkphp框架开启页面gzip压缩

    Thinkphp下开启gzip压缩很简单,不管你是哪个版本,只要在你的入口文件index.PHP中加入以下两行,如果你的服务器支持,那么就OK了. define ( "GZIP_ENABLE ...