【LeetCode从零单刷】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.

解答:

寻找主元素。主元素有几个性质:

  1. 主元素,一定是排序之后序列的中位数;
  2. 对于一个序列而言,删除两个不同的数之和,序列的主元素不变。

排序的话,用快排之后,找中间数。这里是第二种方法:

但怎么知道哪个是主元素么?我们可以假设当前元素是主元素

这样,还需要另一个变量来记录主元素出现次数,当次数为 0 是下一个元素假设为主元素。

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

版权声明:本文为博主原创文章,转载请联系我的新浪微博 @iamironyoung

时间: 2024-08-07 16:59:44

【LeetCode从零单刷】Majority Element的相关文章

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

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. Credits: Special thanks t

LeetCode Javascript实现 169. Majority Element

169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { var hash = {}; var y=-1,z; //注意这里的方括号,利用变量访问对象属性时要用方括号 for(var i=0;i<=nums.length-1;i++){ if(hash[nums[i]]){ hash[nums[i]]++; }else{ hash[

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. 大意: 给出一个尺寸为n的数组,找到主要的元素.

[leetcode]Divide and Conquer-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. Credits:Special thanks to @t

【LeetCode从零单刷】Kth Smallest Element in a BST

题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you

【LeetCode从零单刷】Maximum Depth of Binary Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given a binary tree, find its maximum depth.  The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解答: 树的深度优先遍历,求得树高度即可.然后需要用到递归的思想,假设子树的高

【LeetCode从零单刷】Same Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given two binary trees, write a function to check if they are equal or not.  Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解答: 验证两棵树是否相等.使

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more