首页 > 学院 > 开发设计 > 正文

Leetcode 169. Majority Element

2019-11-11 02:58:53
字体:
来源:转载
供稿:网友

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.

s思路: 1. 有一个很著名的算法专门干这个的。 2. 明天思考一下,这个方法为啥行?

class Solution {public: int majorityElement(vector<int>& nums) { // int count=0; int mj=0; for(int num:nums){ if(count==0) mj=num; if(num==mj) count++; else count--; } return mj; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表