《剑指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 ...
随机推荐
- linux 删除软链接
Linux下取消软连接,做个案例来说明: 1.先建立一个软连接 ln -s /soft/hexo/nodejs/bin/node /usr/local/bin/node ln -s /soft/hex ...
- ai技术体系
- solidity 学习笔记 2 (二维数组)
solidity 二维数组: pragma solidity ^0.4.23; contract twoArray{ uint[2][3] grade =[[20,30],[40,50],[45,60 ...
- Hyperledger Fabric 替换couchDB
fabric中默认数据存储的方式是levelDB,一个key/value存储的单机数据库.除此之外还提供了另外一种存储方式:couchDB.同样也是一个K/V 数据库,对fabric而言,相比于前者, ...
- 洛谷P3431 [POI2005]AUT-The Bus
P3431 [POI2005]AUT-The Bus 题目描述 The streets of Byte City form a regular, chessboardlike network - th ...
- 洛谷 P3372 【模板】线段树 1
P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...
- ajax异步传输数据,return返回值为空
今天在项目中遇到了一个问题,就是在定义了一个函数drawHtml(),本意是想在函数运行结束后,返回拼接的字符串,可是函数运行结束后始终返回的是undefined 有BIG的代码: function ...
- Java IO 输入和输出流
数据流是指一组有顺序的,有起点和终点的字节集合. 最初的版本中,java.io 包中的流只有普通的字节流,即以 byte 为基本处理单位的流.字节流用来读写 8 位的数据,由于不会对数据做任何转换,因 ...
- Glassfish DeploymentException: Error in linking security policy for
http://stackoverflow.com/questions/7322476/glassfish-deploymentexception-error-in-linking-security-p ...
- Ubuntu安装Python2+Python3
sudo apt-get install python2.7 python2.7-dev sudo apt-get install python3 命令: python 默认执行python2 pyt ...