这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做。

169.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.

这道题是难度为Easy的题目。看到这个题目我首先想到的(除去暴力算法)就是通过排序将相同的元素放在一起。由于这个题目的主元素出现次数一定大于n/2次,所以我想到的办法是排序后取最中间的元素(这个元素一定是主元素)。

想到这一点,程序就相当简单。复杂度为O(nlogn)。

//排序取中间数
#include<vector>
#include<algorithm>
using namespace std; class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(), nums.end());
return nums.at(nums.size() / 2);
}
};

当然,这周学的是分治法。我用上面的方法通过了这道题之后再尝试着用分治法的思路去做这道题。长时间思考后无果,看了LeetCode里面的Solution,解法如下。注释是我自己加的。

//Didive-and-Conquer Solution
class Solution {
public:
int majorityElement(vector<int>& nums) {
return majority(nums, 0, nums.size() - 1);
}
private:
// left为最左端元素,right为最右端元素,mid为中间元素
// 返回条件为最左端元素等于最右端元素
// 将问题分为两半递归下去
//
int majority(vector<int>& nums, int left, int right) {
if (left == right) return nums[left];
int mid = left + ((right - left) >> 1);
int lm = majority(nums, left, mid);
int rm = majority(nums, mid + 1, right);
if (lm == rm) return lm;
// 关键 —— 合并结果
return count(nums.begin() + left, nums.begin() + right + 1, lm) > count(nums.begin() + left, nums.begin() + right + 1, rm) ? lm : rm;
}
};

那除了分治法以外,我还看到了一些十分有意思的解法——

一个是随机算法,随机生成一个index,然后遍历这个数组,统计这个index下的元素在数组里的出现次数,如果超过n/2就确认是主元素。这是一个不稳定的O(n)的算法。

class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
srand(unsigned(time(NULL)));
while (true) {
int idx = rand() % n;
int candidate = nums[idx];
int counts = 0;
for (int i = 0; i < n; i++)
if (nums[i] == candidate)
counts++;
if (counts > n / 2) return candidate;
}
}
};

我看到的另一个有趣的解法是用栈的方法来遍历数组:当栈为空时,压入一个元素;当栈不为空时,当前元素跟栈内元素进行比较,如果相同则压入栈内,如果不同则将栈内元素弹出(当前元素不压栈)。当遍历完整个数组后,栈内剩下的元素一定会是主元素。

public class Solution {
public int majorityElement(int[] num) { int major=num[0], count = 1;
for(int i=1; i<num.length;i++){
if(count==0){
count++;
major=num[i];
}else if(major==num[i]){
count++;
}else count--; }
return major;
}
}

Week1 - 169.Majority Element的更多相关文章

  1. 169. Majority Element(C++)

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

  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 Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

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

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

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

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

  6. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

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

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

  8. (Array)169. Majority Element

    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 ...

随机推荐

  1. 移动端布局的思考和rem的设置

    如下方法如有不正确的地方,欢迎指正 前提: 设置稿750px 目标:40px = 1rem js设置方法:(小于等于750屏幕等比缩放) ;(function (doc, win, undefined ...

  2. String.IsNullOrEmpty官方示例

    // This example demonstrates the String.IsNullOrEmpty() method using System; class Sample { public s ...

  3. Centos6.6安装apache2.4

    安装apr请参考:   http://www.cnblogs.com/yuzhaokai0523/p/4382974.html 1安装httpd-2.4.10.tar.gz wget http://w ...

  4. java.sql.SQLException: Unknown system variable 'query_cache_size'

    改为 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java< ...

  5. element隐藏组件滚动条scrollbar使用

    可使用 组件 <el-scrollbar></el-scrollbar> 设置 组件的样式 为 高度100% <el-scrollbar style="heig ...

  6. 【SaltStack官方版】—— states教程, part 4 - states 说明

    STATES TUTORIAL, PART 4 本教程建立在第1部分.第2部分.第3部分涵盖的主题上.建议您从此开始.这章教程我们将讨论更多 sls 文件的扩展模板和配置技巧. This part o ...

  7. 【NOIP2015模拟10.22】最小代价

    前言 本来在比赛上就想到最小生成树了,但不相信这道题那么简单,然后就没有然后了... 题目 给出一幅由n个点m条边构成的无向带权图. 其中有些点是黑点,其他点是白点. 现在每个白点都要与他距离最近的黑 ...

  8. LeetCode--006--Z 字形变换(python)

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: 之后,你的输出需要从左 ...

  9. Rosetta Stone 不在C盘安装步骤

    本文出自:http://www.cnblogs.com/2186009311CFF/p/7500637.html Rosetta Stone默认安装在C盘的,很不好,故找到次解决方案: 总体就是移动文 ...

  10. 面试题常考&必考之--js闭包特性和优缺点 (外加小例子)

    当内部函数被返回到外部并保存时,一定会产生闭包.闭包会产生原来的作用域链,不释放. 闭包,可以理解为,写一个函数,然后产生闭包的这种现象. 概念: 基础: 主要是:add    reduce  被返回 ...