《剑指offer》39题—数组中出现次数超过一半的数字
题目描述
方法1:
1 class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int n = numbers.size();
if(n == )
return ;
int begin=, end=n;
int index = partition(numbers, , n);
while(index != n/){
if(index < n/)
begin = index + ;
else
end = index;
index = partition(numbers, begin, end);
}
int cnt = ;
for(int i=; i<n; ++i){
if(numbers[index] == numbers[i])
++cnt;
}
return cnt > numbers.size()/ ? numbers[index] : ;
}
int partition(vector<int> arr, int begin, int end){
int pivot = arr[begin];
while(begin < end){
while(begin < end && arr[--end] >= pivot);
arr[begin] = arr[end];
while(begin < end && arr[++begin] <= pivot);
arr[end] = arr[begin];
}
arr[begin] == pivot;
return begin;
}
};
方法2:
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if(numbers.size() == )
return ;
int majority = numbers[], cnt = ;
for(int i=; i<numbers.size(); ++i){
if(cnt == ){
majority = numbers[i];
cnt = ;
}
else
cnt = numbers[i]==majority ? cnt+ : cnt-;
}
cnt = ;
for(int i=; i<numbers.size(); ++i){
if(majority == numbers[i])
++cnt;
}
return cnt > numbers.size()/ ? majority : ;
}
};
《剑指offer》39题—数组中出现次数超过一半的数字的更多相关文章
- 【剑指offer】73.数组中出现次数超过一半的数字
73.数组中出现次数超过一半的数字 知识点:数组:哈希:占领地思想: 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4 ...
- 【Offer】[39] 【数组中出现次数超过一半的数字】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数 ...
- 剑指offer-面试题39-数组中出现次数超过一半的数字-抵消法
/* 题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输 ...
- 剑指offer系列54---数组中出现次数超过一半的数
[题目]数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. * 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}. * 由于数字2在数组中出现了5次,超过数组长度的一半,因 ...
- 剑指offer-面试题39-数组中出现次数超过一半的数字-快速排序
/* 题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输 ...
- 《剑指offer》第三十九题(数组中出现次数超过一半的数字)
// 面试题39:数组中出现次数超过一半的数字 // 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例 // 如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- 剑指Offer:数组中出现次数超过一半的数字【39】
剑指Offer:数组中出现次数超过一半的数字[39] 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于这 ...
- 剑指 Offer 39. 数组中出现次数超过一半的数字 + 摩尔投票法
剑指 Offer 39. 数组中出现次数超过一半的数字 Offer_39 题目描述 方法一:使用map存储数字出现的次数 public class Offer_39 { public int majo ...
随机推荐
- JS中map list 数组的迭代
后台传给前台一个map 前台如何迭代呢 $.post("getSys.jhtml", function(data){ var temp = ""; $.each ...
- E20190420-hm
impact n. 巨大影响; 强大作用; 撞击; 冲撞; 冲击力; v. (对某事物) 有影响,有作用; 冲击; 撞击; incident n. 发生的事情(尤指不寻常的或讨厌的); 严 ...
- Unity 5.6中的混合光照(上)
https://mp.weixin.qq.com/s/AbWM21sihHw5pFdMzENDPg 在Unity 5中,光照得到了很大的改进.现在,创建高度逼真的游戏已成为可能.但是,出于对性能的考虑 ...
- 洛谷P2680 运输计划(树上差分+二分)
传送门 考虑树上乱搞 首先这是满足二分性质的,如果在某个时间可以完成工作那么比他更长的时间肯定也能完成工作 然后考虑二分,设当前答案为$mid$,如果有一条链的长度大于$mid$,那么这条链上必须得删 ...
- SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...
- SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
一.Mybatis框架 1.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...
- 数组Array的相关操作。
一 数组的对象(元素): 1. 数字, 2 .字符串 3 变量 4. 函数 .... 二 数组的创建 1 var arrayObj = new Array(); var a =new Array(si ...
- 解决lnmp无法远程登录的bug
使用lnmp一键安装好linux环境后,不能远程连接到所在服务器的mysql,但是通过80端口可以登录phpmyadmin,发现原来是因为lnmp的大多数版本为了安全不禁止远程连接mysql,方法很简 ...
- 简单记录下SpringCloud的微服务架构和一些概念
一.微服务的注册与发现——Eureka 和许多分布式设计一样,分布式的应用一般都会有一个服务中心,用于记录各个机器的信息.微服务架构也一样,我们把一个大的应用解耦成这么多个那么多个服务,那么在想要调用 ...
- python 遇到的一些问题和解决方法
安装crypto python3里面这个改成了pycryptodome 1. pip3 install pycryptodome 或者 pip3 install -i https://pypi.do ...