Majority Number II

    • 题目描述
    • 链接地址
    • 解法
    • 算法解释

题目描述

Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.

Example

Given [1, 2, 1, 2, 1, 3, 3], return 1.

Note

There is only one majority number in the array.

Challenge

O(n) time and O(1) extra space.

.

链接地址

http://www.lintcode.com/en/problem/majority-number-ii/

解法

 int majorityNumber(vector<int> nums){
        // write your code here
         int num1 = 0, num2 = 0;
         int ret1 = 0, ret2 = 0;
         for (int i = 0; i < nums.size(); i++) {
             if (num1 != 0 && ret1 == nums[i]) {
                 num1++;
             } else if (num2 != 0 && ret2 == nums[i]) {
                 num2++;
             } else if (num1 != 0 && num2 != 0) {
                 num1--;
                 num2--;
             } else if (num1 == 0) {
                 num1 = 1;
                 ret1 = nums[i];
             } else {
                 num2 = 1;
                 ret2 = nums[i];
             }
         }
         int count1 = 0, count2 = 0;
         for (int i = 0; i < nums.size(); i++) {
             if (ret1 == nums[i]) {
                 count1++;
             } else if (ret2 == nums[i]) {
                 count2++;
             }
         }
         if (count1 > count2) {
             return ret1;
         } else {
             return ret2;
         }
    }

算法解释

正如算法Majority Number,同时删除两个元素,现在同时删除三个元素。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 16:06:26

Majority Number II的相关文章

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra

Majority Number I &amp; || &amp;&amp; |||

Majority Number Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example Given [1, 1, 1, 1, 2, 2, 2], return 1 分析: 既然这里只有一个majority number,那么它的个数减去其它number个数之和还是为正值. 1 public

169. Majority Element &amp;&amp; 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 T

[LeetCode][JavaScript]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. https://leetcode.com/problems/majority-element-ii/ 使用Moore voting algorithm,时间复杂度是

1245 - Harmonic Number (II)(规律题)

1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int n ) {     long long res = 0;     for( int i =

leetcode Ugly Number II

题目连接 https://leetcode.com/problems/ugly-number-ii/ Ugly Number II Description Write a program to find the $n_{th}$ ugly number. Ugly numbers are positive numbers whose prime factors only include $2, 3, 5.$ For example,$ 1, 2, 3, 4, 5, 6, 8, 9, 10, 12

Single Number,Single Number II

Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you imp

[LeetCode][JavaScript]Single Number II

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/

LintCode-Majority Number II

Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the array Example For [1, 2, 1, 2, 1, 3, 3] return 1 Challenge O(n) time and O(1) spa