剑指offer:数组中出现次数超过一半的数字
来源:互联网 发布:装修报价软件排名 编辑:程序博客网 时间:2024/05/16 01:30
题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
class Solution {public: int MoreThanHalfNum_Solution(vector<int> numbers) { map<int, int> m; int i; for(i = 0; i < numbers.size(); i++) { m[numbers[i]]++; } map<int, int> :: iterator it; for(it = m.begin(); it != m.end(); it++) { if(it->second > numbers.size() / 2) { return it->first; break; } } return 0; }};
0 0
- 剑指offer--数组中出现次数超过一半的数字
- 《剑指offer》数组中出现次数超过一半的数字
- 剑指offer:数组中出现次数超过一半的数字
- 【剑指Offer】数组中出现次数超过一半的数字
- 剑指offer:数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字||剑指offer
- 剑指offer 数组中出现次数超过一半的数字
- 剑指offer:数组中出现次数超过一半的数字
- [剑指offer]数组中出现次数超过一半的数字
- 《剑指offer》-数组中出现次数超过一半的数字
- 剑指offer|数组中出现次数超过一半的数字
- 剑指offer-数组中出现次数超过一半的数字
- 《剑指offer》数组中出现次数超过一半的数字
- 剑指offer - 数组中出现次数超过一半的数字
- 剑指Offer:数组中出现次数超过一半的数字
- 剑指offer-数组中出现次数超过一半的数字
- 剑指offer:数组中出现次数超过一半的数字
- 剑指offer 数组中出现次数超过一半的数字
- android中的HandlerThread类的学习
- Wex5-Home页
- maven(一)
- android Resources Overview
- 【水】LightOJ Positive Negative Sign 1294
- 剑指offer:数组中出现次数超过一半的数字
- XMPP客户端库Smack 4.0.6版开发之四
- make工具与makefile文件编写实例
- leetcode-Nim Game
- XMPP客户端库Smack 4.0.6版开发之五
- 房产售卖信息查询器-React Native实战
- Android 上传文件,图片。以及服务器端接收相关
- Palindrome Number
- String 的toString()方法