国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 汉沽区| 芜湖市| 新安县| 湾仔区| 德令哈市| 甘南县| 隆化县| 永登县| 闽侯县| 卓尼县| 二连浩特市| 大丰市| 宁德市| 焦作市| 衡阳市| 华容县| 库伦旗| 泰州市| 修文县| 玉林市| 十堰市| 浮山县| 石阡县| 元氏县| 田林县| 延吉市| 普洱| 海安县| 乐平市| 崇左市| 扶沟县| 泸水县| 乐至县| 永登县| 溆浦县| 彭水| 个旧市| 亳州市| 黄陵县| 永德县| 二手房|