leetcode:Majority Number

1、Given an array of integers, the majority number is the number that occursmore than half of the size of the array. Find it.

Given [1, 1, 1, 1, 2, 2, 2], return 1

2、得到当前数组中个数最多的一个数

3、代码

  

public class MajorityNumber {
    public static  int majorityNumber(ArrayList<Integer> nums) {
        int count = 0, candidate = -1;
        for (int i = 0; i < nums.size(); i++) {
            /**
             * 1、得到初始化的值
             * 2、将值的个数进行判断
             * 3、若相同,得到的是第一个值
             */
            if (count == 0) {
                //得到第一个值
                candidate = nums.get(i);
                //当前个数为1
                count = 1;
            } else if (candidate == nums.get(i)) {
                count++;
            } else {
                count--;
            }
        }
        return candidate;
    }
}
时间: 2024-10-03 22:54:10

leetcode:Majority Number的相关文章

[LintCode] 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. Notice You may assume that the array is non-empty and the majority number always exist in the array. Have you met this questio

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

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

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] 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? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n

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

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r