Majority Element,Majority Element II
一: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的更多相关文章
- 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 ...
- 关于报错stale element reference: element is not attached to the page document处理
1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...
- stale element reference: element is not attached to the page document 异常
在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element referenc ...
- 报错stale element reference: element is not attached to the page document结局方案
今天在调试脚本时,遇到如下报错: org.openqa.selenium.StaleElementReferenceException: stale element reference: elemen ...
- selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...
- 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 ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- CSS选择器笔记,element element和element > element 的区别
看官方解释 element element 例子: div p 官方解释:div内部所有的p元素 就是说 只要p在div内部.如果 p在span内部,span在div内部,p也算在div内部 < ...
- Raphael.js API 之Element.remove(),Element.removeData(),paper.text(),Element.node(),Element.onDragOver
/*API-38*/ Element.remove() 删除某个元素对象,无返回值 /*API-39*/ Element.removeData([key]); 删除某个key的value值.假设没有特 ...
随机推荐
- ORACLE OCP认证
基本情况介绍 Oracle产品非常多,这里说的是Oracle数据库认证体系. Oracle数据库认证体系包括3层,分别是OCA(助理),OCP(专家),OCM(大师) 一般情况下,需一级一级认证,也就 ...
- UIView 和 UIWindow 的学习内容
UIWindow是UIView的子类,一个程序只能有一个window主窗口. 在XCode7之后我们创建UIWindow的对象,代码如下: //创建一个窗口,使其铺满屏幕(设置大小) ...
- struts2 <s:iterator> 遍历方法
1.MapAction.java import java.util.ArrayList; import java.util.HashMap; import java.util.List; ...
- Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- C++程序设计实践指导1.6分数运算改写要求实现
改写要求:重载>>和<<实现分数类对象的直接输入输出,重载+完成多个分数对象加法 #include <cstdlib> #include <iostream& ...
- Router和History (路由控制)-backbone
Router和History (路由控制) Backbone.Router担任了一部分Controller(控制器)的工作,它一般运行在单页应用中,能将特定的URL或锚点规则绑定到一个指定的方法(后文 ...
- setTimeout setInterval 带参数的问题
转载http://www.jb51.net/article/36233.htm 在JS中无论是setTimeout还是setInterval,在使用函数名作为调用句柄时都不能带参数,而在许多场合必须要 ...
- QString转LPCWSTR
QFileInfo info("./records.db"); std::string str = info.absoluteFilePath().toStdString(); / ...
- javascript的stringFormat函数实现
写一个简单的stringFormat来给自己用 function stringFormat(format, args) { var formatData; if (arguments.length = ...
- SQL Server 性能篇- 碎片
本文分为两个问题: 第一,碎片是什么:第二,碎片怎么处理: 现在,来找解决这两个问题: 一.碎片是什么 说到碎片,就要提到索引了,索引用着挺爽的啊!是的,一旦索引建立,我们搜索数据的效率就提高了:然 ...