LeetCode229 majorityelementii Java

Source: Internet
Author: User

<p> title: </p><p><span style= "Color:rgb (51, 51, 51); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; font-size:14px; line-height:30px; " >given an integer array of Size </span><span style= "Box-sizing:border-box; Color:rgb (51, 51, 51); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; font-size:14px; line-height:30px; " >n</span><span style= "Color:rgb (51, 51, 51); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; font-size:14px; line-height:30px; " Elements, find all, this appear more Than </span><code style= "Box-sizing:border-box; Font-family:menlo, Monaco, Consolas, ' Courier New ', monospace; font-size:12.6000003814697px; PADDING:2PX 4px; Color:rgb (199, 37, 78); border-radius:4px; line-height:30px; Background-color:rgb (249, 242, 244); " ? N/3 </code><span style= "Color:rgb (51, 51, 51); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; font-size:14px; Line-height:30px; " > times. The algorithm should run in linear time and in O (1) space.</span></p><p><span style= "Color:rgb (51, 5 1, 51); font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; font-size:14px; line-height:30px; " > The number of occurrences is greater than one-third of the length of the array, so there are only 2 such elements, the topic requires linear time complexity and constant space complexity, so this problem can not be solved using a hash table. </span></p><p> through the first traversal to find the most occurrences of the two elements (using this principle), this time can not get the exact number of occurrences, so get two occurrences of the most elements, Need to traverse again to get the number of occurrences of the accuracy of the array length of One-third </p><p></p><p> code: </p>import java.util.ArrayList; Import Java.util.list;public class Leetcode229_majorityelementii {/** * @param args */public static void main (string[] Arg s) {//TODO auto-generated method stubint[] nums={1,2,3,4,5,6,1,1,1,2,2,2,1,2}; List<integer> result=majorityelement (Nums); for (int i=0;i<result.size (); i++) System.out.println ( Result.get (i));} public static list<integer> majorityelement (int[] nums) {list<integer> result=new Arraylist<integer > (); int Majornum1=0,majornum2=0,majorcount1=0,majorcount2=0;for (int i=0;i<nums.length;i++) {if (majorcount1==0) {majorNum1=nums[i]; Majorcount1=1;} else if (Majornum1==nums[i]) {majorcount1++;} else if (majorcount2==0) {majornum2=nums[i];majorcount2=1;} else if (Majornum2==nums[i]) {majorcount2++;} else {majorcount1--;majorcount2--;}} majorcount1=0;majorcount2=0;for (int i=0;i<nums.length;i++) {if (NUMS[I]==MAJORNUM1) majorcount1++;if (nums[i]== majorNum2) majorcount2++;} if (MAJORCOUNT1&GT;NUMS.LENGTH/3) Result.add (MAJORNUM1); if (majorcount2>nums.length/3&&majornum2!=            MAJORNUM1) Result.add (majorNum2); return result; }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

LeetCode229 majorityelementii Java

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.