一天一道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. 动态规划--Kin

    动态规划: 1.最大子序列和 2.LIS最长递增子序列 3.LCS最长公共子序列 4.矩阵连乘 5.数字金字塔 1.最大子序列和 #include<iostream> using name ...

  2. Linux上rpm实战搭建FTP服务器

    1.检测是否已安装FTP服务 # rpm -qa|grep vsftpd 2.未安装ftp服务的前提进行使用rpm安装 # yum install vsftpd -y Loaded plugins: ...

  3. 2018年4月更新70多个公司dnc招聘职位

    2018年4月更新70多个公司dnc招聘职位 请在本页回复,补充dnc招聘信息.公司案例 dnc简介 dnc = .NET Core.dotnet Core简写 dnc是微软新一代主力编程平台,开源. ...

  4. tensorflow共享变量 the difference between tf.Variable() and get_variable()

    一般这样用tf.get_variable(): v = tf.get_variable(name, shape, dtype, initializer) 下面内容来源于 http://blog.csd ...

  5. Python学习---字符串处理

    This world is but a canvas to our imagination. 世界只是我们的想象的画布. ----Apri 22 ''' 题目内容: "Pig Latin&q ...

  6. 网络流24题第一题(luogu2796飞行员配对方案)

    飞行员配对方案 二分图裸题,可以拿最大流怼. 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 ...

  7. Linux: Check version info

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/version Linux version 2.6.1 ...

  8. iOS关于时间的处理

    转自:iOS关于时间的处理 做App避免不了要和时间打交道,关于时间的处理,里面有不少门道,远不是一行API调用,获取当前系统时间这么简单.我们需要了解与时间相关的各种API之间的差别,再因场景而异去 ...

  9. 安卓高级 WebView的使用到 js交互

    我们先来学习 怎么使用再到用js和安卓源生方法交互 WebView简单使用 此部分转载并做了补充 原博客 原因:比较简单不是很想在写,我只要写js交互部分 WebView可以使得网页轻松的内嵌到app ...

  10. JBOSS EAP实战(1)

    JBOSS的诞生 1998年,在硅谷SUN公司的SAP实验室,一个年轻人正坐在电脑前面思考,然后写着什么东西.不,他没有在写程序,他在写辞呈.他正在做出人生的一个重大决定:他要辞掉在SUN的这份工作, ...