169. Majority Element(查找多数元素)

【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力) Question1Given 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 elemen 阅读详情

这题难度不大,直接用两层循环暴力求解可以AC:

class Solution {
public:
	int majorityElement(vector<int>& nums) {
		int res = -2147483647;
		int result;
		int length = nums.size();
		for (int i = 0; i < length; i++) {
			int count = 0, tmp = -2147483647, flag = nums[i];
			if (flag != -2147483647) {
				for (int j = 0; j < length; j++) {
					if (flag == nums[j] && nums[j] != -2147483647) {
						count++;
						tmp = nums[j];
						nums[j] = -2147483647;
					}
				}
				if (count > res) {
					res = count;
					result = tmp;
				}
			}
		}
		return result;
	}
};

当然这样暴力求解没有什么意思,于是我选择用了分治算法,主要思想就是找出数组中间值,将数组一分为二,找出新的两个数组中的多数元素X、Y,若X=Y则直接返回,否则对整个数组进行遍历,找出X、Y出现次数更多的元素。由于是分成两半进行递归且仍需要遍历数组,因此复杂度应为O(nlogn)。

int myMajor(vector<int> nums) {
	int len = nums.size();
	if (len == 1)
		return nums[0];
	vector<int> vec1(nums.begin(), nums.begin() + len / 2);
	int l = myMajor(vec1);
	vector<int> vec2(nums.begin() + len / 2, nums.begin() + len);
	int r = myMajor(vec2);
	if (l == r)
		return l;
	else {
		int count1 = 0, count2 = 0;
		for (int i = 0; i < len; i++) {
			if (nums[i] == l)
				count1++;
			if (nums[i] == r)
				count2++;
		}
		return (count1 > count2) ? l : r;
	}
}

class Solution {
public:
	int majorityElement(vector<int>& nums) {
		return myMajor(nums);
	}
};

但是我觉得每次递归都要新开vector来存储,过于浪费空间,于是打算直接用原始vector作为参数代入递归之中,做法同上。但是在做题过程中一直出错,经过长时间的调试才发现递归结束的vector并未发生改变,才知道原来是传入递归的参数是vector而不是vector&,因此只是传入了vector的拷贝,数据怎么修改都不会影响原vector。从此之后估计自己怎么都不会忘记深复制浅复制的区别了...

int myMajor(vector<int>& vec, int size, int beg) {
	if (size == 1)
		return vec[beg];
	int l = myMajor(vec, size / 2, beg);
	int r = myMajor(vec, size - size / 2, beg + size / 2);
	if (l == r)
		return l;
	else {
		int count1 = 0, count2 = 0;
		for (int i = beg; i < size; i++) {
			if (vec[i] == l)
				count1++;
			if (vec[i] == r)
				count2++;
		}
		return (count1 > count2) ? l : r;
	}
}

class Solution {
public:
	int majorityElement(vector<int>& nums) {
		int len = nums.size();
		return myMajor(nums, len, 0);
	}
};

做完分治之后,从网上又看了一下其他大神的做法,发现竟然有复杂度为O(n)的算法。先定义一个计数器count与一个默认多数元素,再对数组进行遍历,遇到相同的元素count++,否则count--,相当于此不相同的元素与默认多数元素相互“抵消”,直到count=0时更新默认多数元素。

class Solution {
public:
	int majorityElement(vector<int>& nums) {
		int elem = 0;
		int count = 0;
		int l = nums.size();
		for (int i = 0; i < l; i++) {
			if (count == 0) {
				elem = nums[i];
				count = 1;
			}
			else {
				if (elem == nums[i])
					count++;
				else
					count--;
			}
		}
		return elem;
	}
};

除此之外,C++还有一个nth_element函数能够直接把第n大元素放在第n个位置。由于题意多数元素必然多于n/2次,因此出现在第n/2位置的元素必然为多数元素。

class Solution {
public:
	int majorityElement(vector<int>& nums) {
		nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());
		return nums[nums.size() / 2];
	}
};


169. Majority Element [easy] (Python) 题目链接https://leetcode.com/problems/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 ass 阅读详情

相关推荐

LeetCode 题解 169. Majority Element(查找多数元素)

(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/70917827冷血之心的博客) 欢迎关注微信公众号(文强的技术小屋),学习更多技术知识,一起遨游知识海洋~ 开始在LeetCode上多学习别人的算法,加油~~~ 题目链接地址:https://leetcode.com/problems/majority-...

冷血之心的博客 2848

leetcode 169. Majority Element(大多数的元素)

给出一个数组,找出其中过半的元素。 思路: 因为这里的假设是一定存在过半的元素。 用Moore Voting法,需要 O(n) 的时间和 O(1) 的空间。 先假设count =1 , majority是第一个元素,从左到右遍历数组,当前数字 != majority时,count–。 直到count== 0 时, 更新mojority为当前数字,count重新置为1。如果当前数字==majority,那么count++ 因为题目中假设一定存在这样的元素,所以最后不需要再验证majority的个数是否>

蓝羽飞鸟的博客 477

UE5实战:如何用Mesh Shader优化你的游戏场景渲染(附性能对比)

本文深入探讨了在UE5中利用Mesh Shader优化游戏场景渲染的实战方法。通过对比传统渲染管线与GPU Driven Pipeline,详细阐述了Mesh Shader如何通过任务着色器和网格着色器两阶段并行处理,实现更精细的meshlet级剔除,从而显著降低Draw Call和GPU带宽,提升渲染性能。文章附有具体的性能对比数据与代码示例,为开发者优化高复杂度场景提供了有效路径。

sunny的博客 295

169. 多数元素(majority-element)

给定一个大小为n的数组nums,返回其中的多数元素。多数元素是指在数组中出现次数⌊ n/2 ⌋的元素。你可以假设数组是非空的,并且给定的数组总是存在多数元素。**进阶:**尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

mirana的专栏 428

2024.6.10丨基于DIAMOND与VFDB数据库的致病菌毒力因子高效注释指南

本文详细介绍了基于DIAMOND与VFDB数据库的致病菌毒力因子高效注释方法,涵盖工具安装、参数优化、数据库选择及实战案例解析。通过DIAMOND的快速比对和VFDB的全面毒力因子数据,显著提升微生物基因组研究效率,适用于临床检测、食品安全监测等多个场景。

weixin_33712987的博客 375

中国 it 集成商 主要有哪些 主要做什么工作

既要懂技术(云计算/AI),又要吃透行业(金融/政务)。:用国产化方案(麒麟OS+达梦数据库)替换Oracle/Windows。:如为银行搭建数据中心(采购华为服务器+思科交换机)。:数据中心监控、故障处理(如华胜天成服务国家电网)。:核心交易系统(如中科软为保险公司开发精算平台)。:智慧城市(如太极股份承建“一网通办”政务平台)。:HIS医院信息系统(如东华软件的电子病历系统)。:硬件透明化(如服务器价格战)压缩集成毛利。:工厂设备联网(新华三的工业互联网平台)。

hxsln11的博客 3031

169. Majority Element

输入:一个数组 输出:数组中的众数 规则:众数就是出现次数大于数组长度一半的元素。 分析:暴力 分析2:Boyer-Moore 投票算法 分析3:Hash 分析4:位运算 分析5:分治算法 ...

约定的博客 356

mmsegmentation教程1:自定义数据集、config文件修改、训练教程

mmseg使用教程

weixin_42748439的博客 2万+

Majority Element && Majority Element II

查找数组的多数元素

大道至简的专栏 4749

AD9176 双通道高速DAC调试记录

AD9176是一款高性能双通道16位数模转换器,支持高达12.6GSPS采样率,专为多频段无线应用设计。该器件集成8通道15.4Gbps JESD204B接口,支持复数/实数数据输入,每通道最大输入速率达3.08GSPS。关键特性包括:48位高精度NCO、灵活的插值滤波器(最高12倍)、优异的动态性能(SFDR达-83dBc@1.84GHz)以及多芯片同步功能。硬件调试需重点关注时钟配置(建议250M基准时钟)、JESD参数匹配及交流耦合设计。测试指标要求SFDR>55dBc@312MHz,实测值可达65d

universaldata的博客 1489

[A-21]ARMv8/v9-SMMU系统架构和功能概述

本文简述了SMMU的系统架构和核心功能

基层架构师的分享 1723

Majority Element问题

问题: 数组中有一个数字出现的次数超过了数组changs

a130737的专栏 3169

【华为OD】华为性格测试(综合测试)高分策略

1、性格测试非常重要,是一次否决制,18 个月有效期,所以态度一定要认真对待,不要随便乱选;2、做题原则:正向原则,积极乐观向上3、时间为 25-30 分钟,会频繁出现重复选项重复出现的题目注意一致性。

笑着的程序员的博客 3万+

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 a

shakalaka1的博客 237

EMB电子机械制动系统和EHB电子液压制动系统控制功能的对比

EMB电子机械制动系统相比EHB液压制动系统具有显著优势:响应速度更快(<100ms)、控制精度更高(±2N·m)、能实现单轮独立控制,支持0.1%级制动力调节。在ABS、ESC等安全功能上表现更优,制动平顺性和NVH性能更好,同时提升能量回收效率15%-20%。特别适合智能驾驶需求,可使AEB等系统响应时间缩短50ms,紧急制动距离减少1米以上。EMB系统完美契合高阶自动驾驶对快速响应和精准控制的要求。

m0_59012156的博客 885

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[LeetCode Problem]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 alwa...

muluochangan的博客 222

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

leetcode学习笔记169 Majority Element

leetcode学习笔记169问题方法1方法2方法3 问题 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 i

kuma的博客 401

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【LeetCode】169. Majority Element Majority Element Given an array of size n, find the majority eleme

【LeetCode】169. Majority Element 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 ass

liuheng0111的博客 681

[LeetCode]169. Majority Element 解题报告(C++)

[LeetCode]169. Majority Element 解题报告(C++) 题目描述 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 t...

【黑键】 440

Easy-38

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

一步一脚印 296

leetcode169. Majority Element

题目leetcode169. Majority Element 难度等级:easy思路由于majority member 出现次数大于总数一半,实际上就是求数组的中位数。最简单的思路,就是先排序,然后二分查找到中位数。但其实,可以直接利用快排算法的partition函数,就可以把中位数找出来。具体的做法就是,分块,但分块的中间元素index 等于 中位数的index时,结束。这样,时间复杂度虽然和

alxandral_brother的博客 310

leetcode 169 Majority Element 多数元素

leetcode 169 Majority Element leetcode 2020年3月 每日一题打卡 思路: python 细节: 查找list某元素的位置:list.index(i) 向下取整:int() 代码: class Solution(object): def majorityElement(self, nums): """ :type ...

weixin_43973433的博客 173

【leetcode】(python)169. Majority Element多数元素

多数元素DescriptionExample题意解体思路1code解题思路2code 169. Majority Element Easy Description Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ...

。 590

【LeetCode】169. Majority Element 多数元素(Easy)(JAVA)

【LeetCode】169. Majority Element 多数元素(Easy)(JAVA) 题目地址: https://leetcode.com/problems/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

qq_16927853的博客 171

【LeetCode】 169. Majority Element 多数元素(Easy)(JAVA)每日一题

【LeetCode】 169. Majority Element 多数元素(Easy)(JAVA) 题目地址: https://leetcode.com/problems/majority-element/ 题目描述: Given an array of size n, find the majority element. The majority element is the element t...

qq_16927853的博客 185

LeetCode 169. 多数元素 Majority Element

Table of Contents 一、中文版 二、英文版 三、My answer 四、解题报告 一、中文版 给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于⌊ n/2 ⌋的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例1: 输入: [3,2,3] 输出: 3 示例2: 输入: [2,2,1,1,1,2,2] 输出...

三妹的博客 260
上一篇: 215. Kth Largest Element in an Array(找出第K大的数)
下一篇: 240. Search a 2D Matrix II(搜索二维矩阵之二)
BetweenRain
博客等级 码龄9年 0粉丝 18原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值