leetCode 169 [PHP代码]

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.

寻找多数元素

就是某一个数组中的元素的个数是N ,这个N 大于 数组个数/2 取整的值就是多数元素。

下面是PHP的代码实现。

// array(1,2,3,2,5,69,8,2,5,2)
// array(1,2,5,8,5,5,5,98,65,5,5)
// array(1,2,6,8,6,6,6,98,65,6,6)
echo majority(array(2,2,3,2,2,68,8,2,5,2));

function majority($n) {
print_r($n);echo "<br />";
	$elem = 0;
	$count = 0;
	// echo count($n);echo "<br />";
	for($i = 0; $i<count($n); $i++) {
		if($count == 0) {
			$elem = $n[$i];
			// print_r($elem);echo "<br />";
			$count = 1;
		}else{
		// print_r($n[$i]);echo "<br />";
			if($n[$i] == $elem) {
				$count++;
			}else{
				$count--;
			}
		}
	}
	if($count>0)
<span style="white-space:pre">	</span>return $elem;
<span style="white-space:pre">	</span>else
<span style="white-space:pre">	</span>return "Don't have majority element<br />";
}

算法描述:

将计数器置为1,并把元素1保存下来,从元素2开始比对,逐个的比较元素。

如果被扫描的元素和保存下来的值(元素1)相等,计数器加一,不相等,减一。

所有的元素(从元素2到结尾)比较完成计数器大于0,则返回元素一,是多数元素。否则

把元素2存入临时变量,再比较元素3开始的整个数组。

这样依次比较完数组。

判断有无大数。

时间: 2024-11-06 07:30:44

leetCode 169 [PHP代码]的相关文章

[Lintcode]46. Majority Element/[Leetcode]169. Majority Element

46. Majority Element/[169. Majority Element(https://leetcode.com/problems/majority-element/) 本题难度: Easy Topic: Greedy Description Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find

[leetcode]multiply-strings java代码

题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 给两个用字符串表示的数字,求它们的乘积. 注意:数字非负,且可以无限大 ------------------------ 之前未考虑数字可以无限大的情况,采用的方法是 将字符串转

【原创】用Python爬取LeetCode的AC代码到Github

在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地没有代码,除非有题调试半天A不来,本地才有存代码 于是我就考虑,直接用Python把leetCode上的AC代码爬下来,然后扔到本地github文件夹里,然后一个同步大法 大概涉及的知识: 0.cookie 1.网站的结构分析 2.脚本登陆 3.脚本爬站 ----------------------

Leetcode#169. Majority Element(求众数)

题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ? n/2 ? 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2 思路 思路一: 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字. 思路二: 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数.即nums[n/2]为众数.但是在计算比

LeetCode 169. Majority Element解题方法

题目: 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. 也就是找数组中出现次数大于一半的数字,题目保证这

LeetCode 169 Majority Element(主要元素)(vector、map)

翻译 给定一个长度为n的数组,找出主要的元素. 所谓主要的元素是指的出现次数超过? n/2 ?次的元素. 你可以假定这个数组是非空的,并且"主要元素"一定是存在的. 原文 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

leetcode——169 Majority Element(数组中出现次数过半的元素)

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. Hide Tags: Divide and Conquer

LeetCode 169. Majority Element (众数)

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. 题目标签:Array 忘记说了,特地回来补充,今天看完<

leetcode 169 Majority Element 冰山查询

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. 思路: Find k different element