Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
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;
}
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;
}
提交网址: 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)的更多相关文章
- 剑指Offer:面试题29——数组中出现次数超过一半的数字(java实现)
PS:在前几天的面试中,被问到了这个题.然而当时只能用最低效的方法来解. 问题描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2, ...
- 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字
剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...
- 【剑指Offer】28、数组中出现次数超过一半的数字
题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如:输入如下所示的一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过 ...
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- 剑指offer(28)数组中出现次数超过一半的数
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 【Offer】[39] 【数组中出现次数超过一半的数字】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数 ...
- 《剑指offer》面试题39. 数组中出现次数超过一半的数字
问题描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, ...
- 剑指offer 面试题56. 数组中只出现一次的两个数字
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 方法1:用set记录出现过的数字 class Solution { public: void F ...
- 【剑指offer】找出数组中任意一个重复的数字,C++实现
原创博文,转载请注明出处! # 题目 # 思路 对于长度为n的数组,范围为0~n-1的数字而言,如果不粗在重复数字,则排序后数组元素和数组角标相同.如果存在重复数字,则在排序的过程中会出现不同下标对应 ...
随机推荐
- something for learning
[Unity Shaders]法线纹理(Normal Mapping)的实现细节 https://blog.csdn.net/candycat1992/article/details/41605257
- WPF使用总结
ListboxItemContainer样式 一般items控件的项模板 很容易 设置DataTemplate就可以了,比如listbox .但是在选中和失去焦点的时候 却是Windows自带的那种 ...
- 平时作业七 Java
以下是几本计算机书籍的基本信息编号 书名 价格 出版社1 JAVA基础 32 清华大学出版社2 JAVA WEB开发 40 电子工业出版社3 面向对象程序设计 28 清华大学出版社4 Struts开发 ...
- BZOJ3497 : Pa2009 Circular Game
令先手为$A$,后手为$B$,将相邻同色棋子合并成块,首先特判一些情况: 如果所有格子都是满的,那么显然$A$必败. 否则如果所有块都只有一个棋子,那么显然平局. 枚举$A$的第一步操作,如果可以使得 ...
- BZOJ2681 : 玩游戏2
首先若存在多个连通块,那么答案显然是$+\infty$. 否则以$m$为根,每棵子树的根节点都最多只能放一个金币,且这些子树之间互不干扰. 对于一棵父亲为$m$的子树,最优方案下一定可以将子树剖分成若 ...
- Google FireBase - fcm 推送 (Cloud Messaging)
要将 Firebase 添加到您的应用,您需要有一个 Firebase 项目以及适用于您的应用的 Firebase 配置文件. 如果您还没有 Firebase 项目,请在 Firebase 控制台中创 ...
- mpvue-docs基于vue来开发微信小程序
http://mpvue.com/和https://tencent.github.io/wepy/
- ionic-基于angularjs实现的多级城市选择组件
大家都知道在移动端的选择地区组件,大部分都是模拟IOS选择器做的城市三级联动,但是在IOS上比较好,在Android上因为有的不支持ion-scroll.所以就会出现滚动不会自动回滚到某一个的正中间. ...
- 初识Jmeter
初识Jmeter 测试计划是根节点,其下可以有多个Thread Group,起始可配setUp Thread Group和tearDown Group.在每个Group下可创建其它节点,模拟各类实际行 ...
- CPU运行原理
问题: CPU位宽表示什么意思? 下面这个是 https://www.bilibili.com/video/av9667986?from=search&seid=336127932106862 ...