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值.假设没有特 ...
随机推荐
- 自动匹配HTTP请求中对应实体参数名的数据(性能不是最优)
/// <summary> /// 获取请求参数字段 /// </summary> /// <typeparam name="T"></t ...
- 被「李笑来老师」拉黑之「JavaScript微博自动转发的脚本」
故事的背景如下图,李笑来 老师于10月19日在 知乎Live 开设 一小时建立终生受用的阅读操作系统 的讲座,他老人家看到大家伙报名踊跃,便在微博上发起了一个 猜数量赢取iPhone7 的活动. 因为 ...
- 使用react-native做一个简单的应用-01项目介绍
学习react-native也有一个月的时间了.当学习了关于react-native的基础知识之后,打算自己去仿一个应用去练手.于是花了10天左右的时间,这个小应用的基本功能也实现的差不多了. 在展示 ...
- 11i - 12 How To Set Email Style Preference For All Users At Once?
(文档 ID 578574.1) In this Document Goal Solution Workflow Information Center, Diagnostics, & ...
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...
- Spring-----4、使用Spring容器
转载自:http://blog.csdn.net/hekewangzi/article/details/41349591
- 沼跃鱼早已看穿了一切 C/C++
沼跃鱼早已看穿了一切 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 593 Solved: 229[Submit][Status][Web Boa ...
- Simple Daemon Shell
PROPATH="/var/www/html/" PROGRAM="vertical" LOGNAME="/tmp/monitor.vertical. ...
- Intent七大属性之总结
参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...
- JavaScript的异步操作
http://sporto.github.io/blog/2012/12/09/callbacks-listeners-promises/