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.

求大于数组一半的元素,思想主要是对于大于数组一般的元素,给起计数起计数总和一定可以大于其他元素总和,代码如下:

 class Solution {
public:
int majorityElement(vector<int>& nums) {
int count = ;
int res;
int sz = nums.size();
for(int i = ; i < sz; ++i){
if(count == ){
res = nums[i];
count++;
}else{
if(res == nums[i])
count++;
else
count--;
}
}
return res;
}
};

LeetCode OJ:Majority Element(主要元素)的更多相关文章

  1. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. [LeetCode] 229. Majority Element II 多数元素 II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  3. 【leetcode】Majority Element (easy)(*^__^*)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  7. LeetCode 169. Majority Element - majority vote algorithm (Java)

    1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...

  8. leetcode 【 Majority Element 】python 实现

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  9. 【leetcode】Majority Element

    题目概述: Given an array of size n, find the majority element. The majority element is the element that ...

  10. ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. easymake cmake xmake nmake ...

    最简单的Makefile,但是还是大程序少不了makefile工具 #CC=arm-linux-gnueabihf-CC=target:    $(CC)gcc -o algo_main algo_m ...

  2. 开博第一篇,学习markdown

    Markdown学习 其实之前自己也一直有记录,不过是Evernote记录,没有分享出来,最近看了一些牛人博客,觉得也应该分享出来.和别人多交流,多学习.所以花了几小时学了一下Markdown语法,现 ...

  3. Redis五大数据类型及操作

    目录: 一.redis的两种链接方式 二.redis的字符串操作(string) 三.redis的列表操作(list) 四.redis的散列表操作(类似于字典里面嵌套字典) 五.redis的集合操作( ...

  4. Visual Studio 2012的Windows Service服务安装方式

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...

  5. springmvc 资源国际化

    <!-- 关于国际化: 1. 在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理 2. 可以在 bean 中获取国际化资源文件 Locale 对应的消息 3. ...

  6. Xcode 解决日志打印不全问题

    Xcode 出了8.0后,代码运行日志过长时会出现打印不全的问题. 这可能是Xcode优化的一项,不过这也给开发带来的不必要的麻烦.下面的宏定义可以解决这一问题. #ifdef DEBUG #defi ...

  7. reading list

    Machine  Learning1. Deep Learningimagenet classification with deep convolutional neural networks. 20 ...

  8. $ 专治各种python字符编码问题疑难杂症

    标准动作 在脚本第一行指定编码格式: # coding:utf-8 将默认的ascii字符流处理方式变为utf-8: import sys sys.getdefaultencoding() 'asci ...

  9. netty5----心跳

    netty3心跳: package com.heart; import java.net.InetSocketAddress; import java.util.concurrent.Executor ...

  10. 关于Webpage Not Found问题解决~~~

    还是外文网站好,以下解决办法: IIS6.0 UI vs. IIS 7.x UI Series: More about Web Service Extensions This week in the ...