[Leetcode-java] Majority Element II

Source: Internet
Author: User

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

Test instructions: Finds numbers in the array with weights greater than ⌊n/3⌋ (becomes the majority).

Ideas:

In order to meet the time complexity and space complexity, can not use map to do, according to test instructions, the number is greater than ⌊n/3⌋ no more than two,

Remember variable n1, N2 is the number of candidates, C1, C2 for their corresponding occurrence

Iterates through an array, remembering that the current number is num

If NUM is the same as N1 or N2, add 1 to its corresponding occurrences

Otherwise, if N1 or N2 is empty, assign it to num and set the corresponding counter to 1

Otherwise, subtract 1 from the counter with a smaller number of occurrences of N1 and N2, and if the counter is reduced to 0, assign it to num and set the corresponding counter to 1

Finally, the number of times a candidate's number appears in the array is counted and returned if the requirement is met.

Code:

 Public classSolution { PublicList<integer> Majorityelement (int[] nums) {List<Integer> list =NewArraylist<integer>(); if(Nums = =NULL|| Nums.length = = 0)            returnlist; intn =nums.length; intCount = N/3; intNUM1 = Nums[0]; intnum2 = Nums[0]; intCount1 = 1; intCount2 = 0;  for(inti = 1; I < n; i++){            inttemp =Nums[i]; if(temp = =NUM1) {Count1++; }Else if(temp = =num2) {Count2++; }Else{                if(Count2 = = 0) {//At the beginningnum2 =temp; Count2= 1; Continue; }                if(Count1 <Count2) {Count1--; }ElseCount2--; if(Count1 = = 0) {NUM1=temp; Count1= 1; }                if(Count2 = = 0) {num2=temp; Count2= 1; } }} count1= 0; Count2= 0;  for(inti = 0; I < n; i++){            if(Nums[i] = =num1) Count1++; if(Nums[i] = =num2) Count2++; }                if(Count1 >count) List.add (NUM1); if(num2! = num1 && count2 >count) List.add (num2); returnlist; }}

Reference Link: Http://www.tuicool.com/articles/eA7nIzY

[Leetcode-java] Majority Element II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.