數組中出現次數超過一半的數字

    題目:數組中有一個數字出現的次數超過數字長度的一半,請找出這個數字。例如輸入一個長度爲9的數組{1, 2,3, 2, 2, 2, 5, 4, 2}.因爲數字2在數組中出現了5次,超過數組長度的一半,所以輸出2。java

    解題思路:數組中有一個數字出現的次數超過數組長度的一半,它出現的次數比其餘全部數字出現的次數的和還要多。咱們在遍歷數組的時候保存兩個值:一個是數組中的一個數字,一個是次數。當遍歷到下一個數字的時候,若是下一個數字和以前保存的數字相同,則次數加1;若是下一個數字和以前保存的數字不一樣,則次數減1。若是次數爲0,保存下一個數字,並把次數設置爲1。要找的數字確定是最後一次把次數設爲1時對應的數字。
python

    C#實現:
c#

private static bool CheckInvalidArray(int[] numbers, int length)
        {
            bool bInputInvalid = false;

            if (numbers == null || length <= 0)
                bInputInvalid = true;

            return bInputInvalid;
        }

        private static bool CheckMoreThanHalf(int[] numbers, int length, int number)
        {
            int times = 0;
            for (int i = 0; i < length; i++)
                if (numbers[i] == number)
                    times++;

            bool isMoreThanHalf = true;
            if (times * 2 <= length)
            {
                isMoreThanHalf = false;
            }

            return isMoreThanHalf;
        }

        public static int MoreThanHalfNum(int[] numbers, int length)
        {
            if (CheckInvalidArray(numbers, length))
                return 0;

            int result = numbers[0];
            int times = 1;
            for (int i = 1; i < length; i++)
            {
                if (times == 0)
                {
                    result = numbers[i];
                    times = 1;
                }
                else if (numbers[i] == result)
                    times++;
                else
                    times--;
            }

            if (!CheckMoreThanHalf(numbers, length, result))
                result = 0;

            return result;
        }

    Java實現:
數組

private static Boolean CheckInvalidArray(int[] numbers, int length) {
		Boolean bInputInvalid = false;

		if (numbers == null || length <= 0)
			bInputInvalid = true;

		return bInputInvalid;
	}

	private static Boolean CheckMoreThanHalf(int[] numbers, int length, int number) {
		int times = 0;
		for (int i = 0; i < length; i++)
			if (numbers[i] == number)
				times++;

		Boolean isMoreThanHalf = true;
		if (times * 2 <= length) {
			isMoreThanHalf = false;
		}

		return isMoreThanHalf;
	}

	public static int MoreThanHalfNum(int[] numbers, int length) {
		if (CheckInvalidArray(numbers, length))
			return 0;

		int result = numbers[0];
		int times = 1;
		for (int i = 1; i < length; i++) {
			if (times == 0) {
				result = numbers[i];
				times = 1;
			} else if (numbers[i] == result)
				times++;
			else
				times--;
		}

		if (!CheckMoreThanHalf(numbers, length, result))
			result = 0;

		return result;
	}

    Python實現:
ide

def check_invalid_array(numbers, length):
    b_input_invalid = False

    if numbers == None or length <= 0:
        b_input_invalid = True

    return b_input_invalid

def check_more_than_half(numbers, length, number):
    times = 0
    for item in numbers:
        if item == number:
            times += 1

    is_more_than_half = True
    if times *2 <= length:
        is_more_than_half = False

    return is_more_than_half

def more_than_half_num(numbers, length):
    if check_invalid_array(numbers, length):
        return 0

    result = numbers[0]
    times = 1
    for item in numbers:
        if times == 0:
            result = item
            times = 1
        elif item == result:
            times += 1
        else:
            times -= 1

    if not check_more_than_half(numbers, length, result):
        result = 0

    return result
相關文章
相關標籤/搜索