Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

时间:2022-09-21 10:22:25
剑指offer 面试题29:数组中出现次数超过一半的数字

提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&tqId=11181                                                

参与人数:3512  时间限制:1秒  空间限制:32768K

本题知识点:数组

题目描述

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

分析:

当测试用例出现不到一半的情况,应该输出0。

方法1:快排,如果出现次数超过一半数目的数存在,则该值一定与中位数相等,将其返回;否则返回0. 快排复杂度为n*log n.

另外此题据说有O(n)复杂度的算法...

AC代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
class Gift {
public:
int getValue(vector<int> gifts, int n) {
if(gifts.size()==0 || n<=0) return 0;
// if(gifts.size()!=n) return 0; // 此语句有无,在牛客网oj上均能通过,按道理应加上的...
sort(gifts.begin(),gifts.end());
int countMid=0, res;
for(int i=0; i<gifts.size();i++)
{
if(gifts[i] >= gifts[n/2]) countMid++;
}
if(countMid>n/2) res=gifts[n/2];
else res=0;
return res;
}
};
// 以下为测试
int main()
{
Gift sol;
int n1=5;
vector<int> gifts1={1,2,3,2,2}; int res1=sol.getValue(gifts1, n1);
printf("%d\n",res1); return 0;
}

腾讯 2015秋招 编程题4:微信红包中个数超过总数一半的红包金额

题目描述
春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。

给定一个红包的金额数组gifts及它的大小n,请返回所求红包的金额。

测试样例:
[1,2,3,2,2],5


返回:2


AC代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
class Gift {
public:
int getValue(vector<int> gifts, int n) {
if(gifts.size()==0 || n<=0) return 0;
// if(gifts.size()!=n) return 0; // 此语句有无,在牛客网oj上均能通过,按道理应加上的...
sort(gifts.begin(),gifts.end());
int countMid=0, res;
for(int i=0; i<gifts.size();i++)
{
if(gifts[i] >= gifts[n/2]) countMid++;
}
if(countMid>n/2) res=gifts[n/2];
else res=0;
return res;
}
};
// 以下为测试
int main()
{
Gift sol;
int n1=5;
vector<int> gifts1={1,2,3,2,2}; int res1=sol.getValue(gifts1, n1);
printf("%d\n",res1); return 0;
}

这道题最优的解法类似于 《编程之美》中"寻找水王"的问题,时间复杂度为O(n),空间复杂度O(1)。


169. Majority Element(求众数)

提交网址: https://leetcode.com/problems/majority-element/

Total Accepted: 113359 Total Submissions: 273577 Difficulty: Easy

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

AC代码:

class Solution {
public:
int majorityElement(vector<int>& numbers) {
int len=numbers.size();
if(len==0) return 0;
sort(numbers.begin(),numbers.end());
int countMid=0, res;
for(int i=0; i<numbers.size();i++)
{
if(numbers[i] == numbers[len/2]) countMid++;
}
if(countMid>len/2) res=numbers[len/2];
else res=0;
return res;
}
};

Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)的更多相关文章

  1. 剑指Offer&colon;面试题29——数组中出现次数超过一半的数字&lpar;java实现&rpar;

    PS:在前几天的面试中,被问到了这个题.然而当时只能用最低效的方法来解. 问题描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2, ...

  2. 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字

    剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...

  3. 【剑指Offer】28、数组中出现次数超过一半的数字

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

  4. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  5. 剑指offer(28)数组中出现次数超过一半的数

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

  6. 【Offer】&lbrack;39&rsqb; 【数组中出现次数超过一半的数字】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数 ...

  7. 剑指offer 面试题56&period; 数组中只出现一次的两个数字

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 方法1:用set记录出现过的数字 class Solution { public: void F ...

  8. 【剑指offer】找出数组中任意一个重复的数字,C&plus;&plus;实现

    原创博文,转载请注明出处! # 题目 # 思路 对于长度为n的数组,范围为0~n-1的数字而言,如果不粗在重复数字,则排序后数组元素和数组角标相同.如果存在重复数字,则在排序的过程中会出现不同下标对应 ...

  9. 面试题五 数组中出现次数超过一半的数字 时间为O&lpar;n&rpar;

    也就是说 该数字出现的次数比其他所有数字出现次数的和还要多. 因此可以保存两个值,一个数字,一个次数. 遍历时 1.如果数字相同,count++ 2.如果count == 0 count = 1 nu ...

随机推荐

  1. 网上找的Gif图片解析类

    这个是搜到的大部分的答案 下面贴出来代码 public class MyGifView extends View { private long movieStart; private Movie mo ...

  2. 如何用火车头采集当前页面url网址

    首先创建一个标签为本文网址,勾选后面的“从网址中采集”. 选择下面的“正则提取”,点击通配符“(?<content>?)”,这样在窗口中就显示为(?<content>[\s\S ...

  3. WPF DataGrid 增加&quot&semi;更新&quot&semi;模板列&comma;根据行Row的选择而显示&quot&semi;更新&quot&semi;按钮

    SelectionMode="Single" <DataGridTemplateColumn Header=""> <DataGridTemp ...

  4. mysql函数二

    四.条件推断函数 1.if(expr,v1,v2)函数:成立返回结果v1,否则结果v2 例:select id,if(grade>=60,'pass','fail') from t; 2.IFN ...

  5. Perl 之 use&lpar;&rpar;&comma; require&lpar;&rpar;&comma; do&lpar;&rpar;&comma; &percnt;INC and &commat;INC

    来源: http://www.cnblogs.com/itech/archive/2013/03/12/2956185.html 转自:http://perl.apache.org/docs/gene ...

  6. centos yum安装ffmpeg

    ffmpeg是一个重要的应用软件,用于执行与视频文件转换成不同的视频流格式的视频站点,能够安装在linux系统上来使用 (一)安装编译环境  #yum install -y automake auto ...

  7. Python--day05(数字、字符串、列表)

    1.数字类型 1.  整型  int   long(py2) 2.  小数 float 3.  布尔 bool 4.  复数 complex 2.  字符串类型 只能存一个值,是有序的不可变类型 2. ...

  8. Java Web乱码原因与解决

    Java Web乱码原因与解决 一.了解编码常识: 1.ASCII 码 众所周知,这是最简单的编码.它总共可以表示128个字符,0~31是控制字符如换行.回车.删 除等,32~126是打印字符,可以通 ...

  9. docker-安装技巧

    使用官方脚本安装 curl -fsSL "https://get.docker.com/" | sh 使用yum 安装是可以查看版本 yum list docker-ce.x86_ ...

  10. Debian &amp&semi; CentOS建立本地iso源

    在宿舍搞开发的时候经常遇到有些工具需要安装,没有网络,这时候只能靠mount本地的iso镜像来搞,结果像Debian有3张安装光盘,CentOS有2张光盘,有时候安装包不在第一张光盘里,而在第二张光盘 ...