题目链接

https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&tqId=11181&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

题目描述

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

解题思路

  • 思路一:

    排序再计数,时间复杂度O(nlogn)。
  • 思路二:

    思路:利用快排的partion函数。

    算法描述:记数组长度为n,数组第n/2大的数即中位数即是要找的数。利用快排思想,每一轮partition后,定位一个数(位置也表示第几小),将该数的位置与中位比较,若该数的位置大于中位,则应partion该数左侧的这些比该数小的数。反之partion右侧。直到partion的这个数位置是中位。

    时间复杂度O(n),但会改变输入
  • 思路三:

    思路:由题,所求数比其他所有数出现次数还要多。

    算法描述:遍历数组,记两个变量,一个是临时元素变量,一个是次数,当便利到的的元素与临时元素变量相同则次数++,相异则次数--;若次数已为0,则临时元素变量换为下一个元素,次数设为1;这样最终的临时元素所存元素即为所求值。

    时间复杂度O(n)

关于特例:若输入数组中没有出现次数超过一半的数,则返回0.对于思路二的解法,这个要在找到第n/2大数后判断一下。

相关知识

我认为,关于快排,精髓在于partion函数:即每次定位一个数,即是最终排序的位置,且该数左侧的数都比它小,右侧的数都比它大。

思路二代码 O(n)

class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if (numbers.empty()) {
return 0;
}
int index = partition(numbers, 0, numbers.size() - 1);
int mid = numbers.size() >> 1;
while (index != mid) {
if (index > mid) {
index = partition(numbers, 0, index - 1);
}
else if (index < mid) {
index = partition(numbers, index + 1, numbers.size());
}
} if (!moreThanHalfCheck(numbers, numbers[index])) {
return 0;
}
else {
return numbers[index];
} }
private:
int partition(vector<int>& num, int start, int end) {
if (num.empty() || start > end || start < 0) {
throw "invaild!";
}
int index = randomInRange(start, end);
swap(num[start], num[index]); int l = start;
int r = end;
int tempElm = num[l];
while (l<r)
{
while (num[r] >= tempElm && l < r) {
--r;
}
if (l != r) {
num[l] = num[r];
++l;
}
while (num[l] <= tempElm && l < r) {
++l;
}
if (l != r) {
num[r] = num[l];
--r;
}
}
num[l] = tempElm;
return l;
} int randomInRange(int start, int end) {
if (start > end) {
throw"invaild!";
}
srand((unsigned)time(0));
int index = start + rand() % (end - start + 1);
return index;
} void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
} bool moreThanHalfCheck(vector<int> numbers, int number) {
int numberCnt = 0;
for (int i = 0;i < numbers.size();++i) {
if (numbers[i] == number) {
++numberCnt;
}
}
if (numberCnt > numbers.size() / 2) {
return true;
}
else {
return false;
}
}
};

思路三代码

class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if (numbers.empty()) {
return 0;
}
int num;
int numCnt = 0;
for (int i = 0;i < numbers.size();++i) {
if (numCnt == 0) {
num = numbers[i];
++numCnt;
}
else {
if (numbers[i] == num) {
++numCnt;
}
else {
--numCnt;
}
}
}
if (!moreThanHalfCheck(numbers,num)) {
return 0;
}
else {
return num;
}
}
private:
bool moreThanHalfCheck(vector<int> numbers, int number) {
int numberCnt = 0;
for (int i = 0;i < numbers.size();++i) {
if (numbers[i] == number) {
++numberCnt;
}
}
if (numberCnt > numbers.size() / 2) {
return true;
}
else {
return false;
}
}
};

[剑指Offer]39-数组中出现次数超过一半的数字(快排延申,找第k大数同理)的更多相关文章

  1. 剑指 Offer 39. 数组中出现次数超过一半的数字 + 摩尔投票法

    剑指 Offer 39. 数组中出现次数超过一半的数字 Offer_39 题目描述 方法一:使用map存储数字出现的次数 public class Offer_39 { public int majo ...

  2. 剑指 Offer 39. 数组中出现次数超过一半的数字

    剑指 Offer 39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [ ...

  3. 力扣 - 剑指 Offer 39. 数组中出现次数超过一半的数字

    题目 剑指 Offer 39. 数组中出现次数超过一半的数字 思路1(排序) 因为题目说一定会存在超过数组长度一半的一个数字,所以我们将数组排序后,位于length/2位置的一定是众数 代码 clas ...

  4. 【Java】 剑指offer(39) 数组中出现次数超过一半的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如 ...

  5. 每日一题 - 剑指 Offer 39. 数组中出现次数超过一半的数字

    题目信息 时间: 2019-06-29 题目链接:Leetcode tag: 数组 哈希表 难易程度:简单 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 假设数组是非空的 ...

  6. 剑指Offer:数组中出现次数超过一半的数字【39】

    剑指Offer:数组中出现次数超过一半的数字[39] 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于这 ...

  7. 【剑指Offer】数组中出现次数超过一半的数字 解题报告(Python)

    [剑指Offer]数组中出现次数超过一半的数字 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-inter ...

  8. Go语言实现:【剑指offer】数组中出现次数超过一半的数字

    该题目来源于牛客网<剑指offer>专题. 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组 ...

  9. 剑指OFFER之数组中出现次数超过一半的数字(九度OJ1370)

    题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2 ...

  10. 剑指Offer 28. 数组中出现次数超过一半的数字 (数组)

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

随机推荐

  1. 机器学习进阶-边缘检测-Canny边缘检测 1.cv2.Canny(进行Canny边缘检测)

    1. cv2.Canny(src, thresh1, thresh2) 进行canny边缘检测 参数说明: src表示输入的图片, thresh1表示最小阈值,thresh2表示最大阈值,用于进一步删 ...

  2. jinjia

    https://www.cnblogs.com/dachenzi/p/8242713.html

  3. maven创建项目,打包出可执行Jar

    官网参考 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 配置多种打包方式 这个例子也不错 https://bl ...

  4. python反汇编函数字节码

    使用dis模块 >>> def test(): ... print(1) ... a = 1 ... print(a) ... >>> from dis impor ...

  5. dpkg卸载

    from:https://jingyan.baidu.com/article/f54ae2fc2724a71e92b849c4.html 选择 dpkg -l来查看软件的状态. 选择 dpkg -P来 ...

  6. Linux学习笔记--vim

    Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. vim的三种模式 命令模式(Command mode),输入模式(Insert m ...

  7. VMware Harbor学习

    同时安装Clair和Notary# ./install.sh --with-notary --with-clair 与notary或者Clair一起安装时管理Harbor的生命周期当Harbour与N ...

  8. 使用大于16TB的ext4文件系统

    我们的电脑想要快速开机,需要具备三个条件:第一是主板支持UEFI,二是系统支持UEFI(Win8),最后就硬盘需要采用GPT分区. GPT分区全名为Globally Unique Identifier ...

  9. Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter

    在上一篇文章中Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream介绍了缓冲流中的字节流,而这一篇着重介绍缓冲流中字符流Buffered ...

  10. 正则表达式(Kotlin)

    课题 使用正则表达式匹配字符串 使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89" 返回匹配结果:'" ...