国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

169. Majority Element

2019-11-08 02:38:01
字體:
供稿:網(wǎng)友

題目

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.

Subscribe to see which companies asked this question.

Show Tags Show Similar Problems


思路

思路是vector減去兩個(gè)不同的數(shù)字后那個(gè)超過一半的數(shù)字還是不變的 ,通過不斷去除兩個(gè)不同的數(shù)來縮小范圍


代碼

class Solution {public: int majorityElement(vector<int>& nums) { int maxTimeNum; size_t maxNumSum = 0; size_t length = nums.size(); for(int i=0;i<length;i++) { //當(dāng)前無最大次數(shù)數(shù)字記錄 if(maxNumSum == 0) { maxTimeNum = nums[i]; maxNumSum++; continue; } //當(dāng)前數(shù)字與最大數(shù)字相同 if(maxTimeNum == nums[i]) { maxNumSum++; continue; } //不同則減去一個(gè)計(jì)數(shù) maxNumSum--; } return maxTimeNum; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玛曲县| 涪陵区| 密山市| 沙湾县| 邵阳县| 集安市| 关岭| 南皮县| 长寿区| 芦山县| 宁河县| 云霄县| 神木县| 奇台县| 精河县| 翁源县| 开远市| 邵阳县| 舒兰市| 隆林| 株洲市| 柯坪县| 罗山县| 阳信县| 视频| 沂水县| 江永县| 乌什县| 玛多县| 客服| 林甸县| 元朗区| 彭山县| 灵川县| 突泉县| 普兰县| 怀化市| 桃江县| 类乌齐县| 克拉玛依市| 蕲春县|