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的数字而言,如果不粗在重复数字,则排序后数组元素和数组角标相同.如果存在重复数字,则在排序的过程中会出现不同下标对应 ...
随机推荐
- Android Studio升级到3.4遇到的问题总结
1.gradle需要升级. 1).project的build.gradle文件写下如下代码: buildscript { repositories { google() jcenter() } dep ...
- 2019工作计划idea
2019.2.24 工作需求: 汇总 2008-2018年 销售订单数据; 分类历史订单数据, 并可能采取方法进行预测(预测只是一种行为不代表结果) 目前已知条件: 订单生产周期; 45天(标准天数) ...
- Little Sub and Mr.Potato's Math Problem-构造
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5864 思路 : 判断小于它的合法的,再看大于它的合法的,特判10000. ...
- PBRT笔记(13)——光线传播1:表面反射
采样反射函数 BxDF::Sample_f()方法根据与相应的散射函数相似的分布来选择方向.在8.2节中,该方法用于寻找来自完美镜面的反射和透射光线;在这里讲介绍实现其他类型的采样技术. BxDF:: ...
- pip install –r ./requirements.txt 报错 改成 pip install -r requirements.txt 成功
Invalid requirement: '–r'Traceback (most recent call last): File "/home/dev/.pyenv/versions/3.6 ...
- java 虚拟机学习--未完
1.学习了解GC垃圾回收 参考:https://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak2/ 2.类加载机制 http://blog.cs ...
- 181102 Python环境搭建(安装Sublime Text3)
利用Pycharm来编写.执行python代码是一个不错的选择,Pycharm的安装的确也很方便.但是偶然看到别人用Sublime Text来编写.执行代码,觉得很酷.所以自己动手搭建环境. 1. 下 ...
- org.hibernate.hql.internal.ast.QuerySyntaxException: XXX is not mapped
异常情况: 最近在把一个项目拆分多个 module 的时候数据库查询遇到这个异常:org.hibernate.hql.internal.ast.QuerySyntaxException: Identi ...
- dedecms 后台可以上传mp4,但无法选择
原文链接 找到 /include/dialog/select_media.php 找到rmvb,在其后面加 “|mp4” 即可. 1
- Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决
servlet获值乱码问题解决 解决办法一(最简单有效) request.setCharacterEncoding("utf-8"); 解决办法二 因为乱码问题的产生是因为默认格式 ...