169. Majority Element && 229. Majority Element II

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.

Hide Tags

Array Divide and Conquer Bit Manipulation

Hide Similar Problems

(M) Majority Element II

More readings: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm

public class Solution {
  public int majorityElement(int[] num) {
    int major = num[0];
    int count = 1;
    //The majority number has enough counts to cover all other numbers
    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;
  }
}

229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!

Hide Tags

Array

Hide Similar Problems

(E) Majority Element

public class Solution {
  public List<Integer> majorityElement(int[] nums) {
    List<Integer> results = new ArrayList<>();
    if (nums == null || nums.length == 0)
      return results;

    int n1 = nums[0];
    int n2 = nums[0];
    int count1 = 0;  //count keeps a "relative" count of current number
    int count2 = 0;
    int len = nums.length;

    for (int i = 0; i < len; ++i) {
      if (nums[i] == n1)
        ++count1;
      else if (nums[i] == n2)
        ++count2;
      else if (count1 == 0) {
        n1 = nums[i];
        count1 = 1;
      } else if (count2 == 0) {
        n2 = nums[i];
        count2 = 1;
      } else {
        //Zeros were check beforehand to make sure counts won‘t go negative
        --count1;
        --count2;
      }
    }

    count1 = 0;
    count2 = 0;
    for (int i = 0; i < len; i++) {
      if (nums[i] == n1)
        ++count1;
      else if (nums[i] == n2)
        ++count2;
    }
    if (count1 > len / 3)
      results.add(n1);
    if (count2 > len / 3)
      results.add(n2);
    return results;
  }
}
时间: 2024-12-28 04:06:20

169. Majority Element && 229. Majority Element II的相关文章

Leetcode # 169, 229 Majority Element I and II

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. 这一题可以用排序之后查看序列正中间那个元素的方法来解.但

LeetCode66/169/79 Plus One/Majority Element /Word Search

一: Plus One 题目: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 链接:https://leetcode.com/problems/plus-one/ 分析:就是简单的加法运算,注意进位

辨析element.offsetXxxx和element.style.xxxx

DOM操作时,经常使用element.style属性,没错,element.style是属性,和几个offsetXxxx属性一样,概念是一样的. 但是style有几个属性,这几个属性和offsetXxxx有很大关系.他们是可以相互转化的. 之所以说转化,是因为他们的值类型不同,element.offsetXxxx的值类型是Number,并且是整型,比如100.而element.style.xxx是带有单位的字符串,比如100px. 所以要相互转化,要使用parseInt对style的数据进行处理

元素的属性相关操作element.getAttribute(&#39;xxx&#39;),element.setAttribute(&#39;xxx&#39;,&#39;xxx&#39;),element.removeAttribute(&#39;xxx&#39;)

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <div id="demo" class="one" title="鼠标经过">&

【LeetCode】229. Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint

[LeetCode] 229. Majority Element II 多数元素 II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2] 169. Maj

LeetCode OJ 229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

leetcode 229. Majority Element II(多数投票算法)

就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int> majorityElement(vector<int>& nums) { int cnt1=0,cnt2=0,ans1=0,ans2=1; for(auto n:nums){ if(n==ans1){ cnt1++; } else if(n==ans2){ cnt2++; } el

229. Majority Element II java solutions

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! Subscr