Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) {
  4. int ans = 0, cnt = 0;
  5.  
  6. for(const auto &n : nums) {
  7. if(cnt == 0) {
  8. ans = n;
  9. ++cnt;
  10. }
  11. else {
  12. (n == ans) ? ++cnt : --cnt;
  13. }
  14. }
  15.  
  16. return ans;
  17. }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement