题目地址:

https://oj.leetcode.com/problems/majority-element/

题目内容:

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.

方法:

要求有两个:

0、时间复杂度是O(N)

1、空间复杂度是O(1)  // 这个是当然的,否则来一个hashmap,谁不会 = _ =!

要点是维护这么一个性质:

首先,不妨假设目标元素出现了a次,而总共有b个元素,那么:

1>= b/a >= 1/2

我们可以设定两个指针,一个指向头部,一个指向尾部。要点在于维护这么一个性质:首尾指针之间的数组,目标元素数量占全部数量的1/2下取整之上。如果两个指针指向的元素不相等,说明此时至少有一个指针指向的元素不是所要求的元素,因此,我们可以抛弃这两个元素,这个时候目标元素在源数组中的比率不会降低。

证明就比较简单了。假设最坏的情况,有一个是目标元素,另一个不是,则有:

b/a < (b-1)/(a-2),当2b >= a时成立。而2b >= a是题目给出的条件。

那么如果两个元素相等呢?我们还能抛弃这两个元素吗?回答是不能。因为(b-1)/(a-1) < b/a,不能维持目标元素在原数组中比率不降低的性质。

那该怎么办?答案是维护一个指向当前元素【下一个不相等】的指针,如果相等,那么和下一个和自己不相等的元素交换位置即可。这个指向下一个不相等的指针只会往前走,因此,最多走n次。而我们总共维护4个指针,最多O(4n),还是O(n)。

下面是具体代码:

class Solution {
public:
int majorityElement(vector<int> &num) {
int start = ;
int fin = num.size() - ;
int startnxt = findNextStart(start,num,start); //找到下一个和start不等的指针。
int finnxt = findNextFin(fin,num,fin);
while (start < fin)
{
if (num[start] == num[fin])
{
if (startnxt >= finnxt) // 若当前首尾指针指向的数据相等,那么下轮操作后之间的数据就全部相等了,故可以直接返回结果。
return num[start];
swap(&num[start],&num[startnxt]);
}
start ++;
fin --;
startnxt = findNextStart(start,num,startnxt);
finnxt = findNextFin(fin,num,finnxt);
}
return num[start];
} int findNextStart(int start,vector<int> &num,int preptr)
{
int nowNumber = num[start];
int nxtPtr = preptr == start ? start + : preptr; // 若prestart正好为start,那么前进一格。否则在既有基础上出发。
while (nxtPtr < num.size() && num[start] == num[nxtPtr])
nxtPtr ++;
return nxtPtr;
} int findNextFin(int fin,vector<int> &num,int preptr)
{
int nowNumber = num[fin];
int nxtPtr = preptr == fin ? fin - : preptr;
while (nxtPtr >= && num[fin] == num[nxtPtr])
nxtPtr --;
return nxtPtr;
} void swap(int *a,int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
};

【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)的更多相关文章

  1. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  2. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  3. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  4. LeetCode 169. Majority Element解题方法

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

  5. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

  6. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  7. 【原创】leetCodeOj --- Find Peak Element 解题报告

    题目地址: https://oj.leetcode.com/problems/find-peak-element/ 题目内容: A peak element is an element that is ...

  8. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  9. 【原创】leetCodeOj --- Dungeon Game 解题报告

    原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) ...

随机推荐

  1. HNCU1099:堆积木

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1099 题目描述 小明的侄子要过生日了,小 ...

  2. codeforces 659C Tanya and Toys

    题目链接:http://codeforces.com/problemset/problem/659/C 题意: n是已经有的数字,m是可用的最大数字和 要求选自己没有的数字,且这些数字的数字和不能超过 ...

  3. [Android学习笔记]LinearLayout布局,剩余空间的使用

    转自:http://segmentfault.com/q/1010000000095725 如果使得一个View占用其父View的剩余空间? 答案是使用:android:layout_weight = ...

  4. curl订单具体解释

    为windows假设用户Cygwin模拟unix环境的话,不会有带curl命令,拥有设备,它建议使用Gow为了模拟,它已经自带curl工具,直接安装之后cmd使用环境curl命令可以,由于路径是自己主 ...

  5. poj 1220 NUMBER BASE CONVERSION(短除法进制转换)

    题目连接:1220 NUMBER BASE CONVERSION 题目大意:给出两个进制oldBase 和newBase, 以及以oldBase进制存在的数.要求将这个oldBase进制的数转换成ne ...

  6. 敏捷开发用户故事系列之十一:CSDN博客用户故事分析

    这是敏捷开发用户故事系列的第十一篇.(栏目目录) 经常有人问起有没有完整的用户故事案例.本人在网上找了一下,大约能找到两三篇,但多数只是为了描述用户故事的语法而已,都不涉及用户故事的颗粒度.大量故事的 ...

  7. document.write()相关

    原文地址:http://www.cnblogs.com/dh616854836/articles/2140349.html document.write()脚本向窗口(不管是本窗口或其他窗口)写完内容 ...

  8. 关于JVM的ClassLoader(转)

    众所周知,java是编译型的语言,写的是java文件,最后运行的是class文件,class文件是运行在JVM之中的,这时候就有一个问题,JVM如何装载class文件的?是通过ClassLoader来 ...

  9. Amazon S3数据一致性模型

    左右Amazon S3有两种类型的数据的一致性模型的: 最后,一致性和读一致性. 有下面几种行为: 1 写一个新的object,然后開始读它.直到全部的变化都传播完(副本),你才干读到它,否则就是ke ...

  10. leetcode 之 Permutation Sequence

    Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...