一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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 len = nums.size();

        sort(nums.begin(),nums.end());//调用STL的排序函数

        return nums[len/2];

    }

};

另一种优化的算法。O(n)复杂度。

定义两个整数j和num,j记录次数,num记录一个数

当遍历到nums数组的下一个数时,如果nums[j]==num,或者j==0,此时j++,并保存num的值为nums[j],

如果j!=0且nums[j]!=num,这时候j–,由于num这个数出现次数超过一半,那么最后一个将j变成1的数就是我们要找的数。

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int j = 0;//记录次数
        int ret = nums[0];//初始化为第一个数
        int size = nums.size();
        for(int i =0 ; i < size ; i++)
        {
            if(j==0||nums[i]== ret){
                ret=nums[i];
                j++;
            }
            else j--;
        }
        return ret;
    }
};

【一天一道LeetCode】#169. Majority Element的更多相关文章

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

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

  2. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  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 冰山查询

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

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

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

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

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

  9. LeetCode 169. Majority Element

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

  10. Java for LeetCode 169 Majority Element

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

随机推荐

  1. python常用命令(持续) | Commonly used Python command list (con't)

    ---------------------------------------------------------------------------------------------------- ...

  2. jquery 中toggle的2种用法

    一.在元素的click事件中绑定两个或两个以上的函数  toggle不像bind需要在后面添加"click"来绑定click触发事件,toggle本身就是click触发的(而且只能 ...

  3. python 中range numpy.arange 和 numpy.linspace 的区别

    1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...

  4. c++ primer第15章这几个例子中的构造函数形式不太理解

    //向基类构造函数传递实参p491 class Bulk_item : public Item_base{ public: Bulk_item(,double disc_rate = 0.0): It ...

  5. SpringMVC中url-pattern /和/*的区别

    http://blog.csdn.net/u010648555/article/details/51612030

  6. hibernate4整合spring3出现java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;

    解决办法 原先:<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annota ...

  7. 学习sharding-jdbc 分库分表扩展框架

    先丢代码地址 https://gitee.com/a247292980/sharding-jdbc 再丢pom.xml的dependency <properties> <projec ...

  8. 面向对象+canvas 倒计时

    效果参照网上的,用面向对象改写了一下,只写了自己需要的部分. 1.效果: 实现: //html <canvas id="canvas" width="800px&q ...

  9. WMI远程启动软件(某个应用程序)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...

  10. Ajax相关——get请求和post请求的区别

    一.完整的URL由以下几部分组成: scheme:通信协议,常用的有:http/ftp. host:主机,服务器(计算机)域名或IP地址 port:端口,整数,可选,省略时使用默认端口,http的默认 ...