首页 > 其他 > 详细

Majority Element

时间:2015-11-16 22:35:05      阅读:265      评论:0      收藏:0      [点我收藏+]

1. Title

Majority Element

2. Http address

https://leetcode.com/problems/majority-element/

3. The question

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.

4 My code(AC)

 1         // Accepted
 2        public static int majorityElementTwo(int[] nums) {
 3         
 4            Arrays.sort(nums);
 5            int len = nums.length;
 6            int count,bf;
 7            bf = nums[0];
 8            count=1;
 9            for(int i = 1 ; i < len; i++)
10            {
11                if( count > len /2)
12                {
13                    return bf;
14                }
15                
16                if( nums[i] == bf)
17                {
18                    count++;
19                }else{
20                    bf = nums[i];
21                    count = 1;
22                }
23                
24            }
25            return nums[len-1];
26        }
27        

 

Majority Element

原文:http://www.cnblogs.com/ordili/p/4970040.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!