[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.
思路:直接查找,使用arraylist记录结果,出现超过一半的数就返回
代码:
public class Solution {public static int bExist(ArrayList<int[]> nums,int intInput) {for(int i = 0;i<nums.size();i++){if( nums.get(i)[0]== intInput){return i;}}return -1;}public static int majorityElement(int[] nums) {ArrayList<int[]> arrResult = new ArrayList<int[]>();for(int i = 0;i<nums.length;i++){int intPos = bExist(arrResult,nums[i]);if( intPos == -1){int[] arrTemp = {nums[i],0};arrResult.add(arrTemp);}else{arrResult.get(intPos)[1] += 1; if(arrResult.get(intPos)[1] > (nums.length/2)){return arrResult.get(intPos)[0];}}}int intTemp = 0;int intPos = 0;for(int i = 0;i<arrResult.size();i++ ){if(intTemp <= arrResult.get(i)[1]){intTemp =arrResult.get(i)[1];intPos = i;}}return arrResult.get(intPos)[0];} }
网上高人的思路:每找出两个不同的element,则成对删除。最终剩下的一定就是所求的。
代码:
public static int majorityElement(int[] nums) {int candidate = 0;int count = 0;for(int i = 0; i < nums.length; i ++){if(count == 0){candidate = nums[i];count = 1;}else{if(nums[i] == candidate)count ++;elsecount --;}}return candidate;}
转载于:https://www.cnblogs.com/savageclc26/p/4830970.html
[LeetCode]: 169: Majority Element相关推荐
- [勇者闯LeetCode] 169. Majority Element
[勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode - 169. Majority Element (多数投票问题)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [swift] LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169 Majority Element(Majority Voting Algorithm)
Majority Element Given an array of size n, find the majority element. The majority element is the el ...
- leetcode讲解--169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode 229 : Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
最新文章
- Python中requests包的安装
- java String pool
- Go之十大经典排序算法
- python怎么处理数据_python panda怎么处理数据
- python中字符串运算符及用法_python将字符串转换为运算符
- Spring相关API-getBean()方法的使用
- DotNet Core 介绍
- 虚拟机Centos7配置网络
- tkinter中text插入_tkinter做一个体重档案
- java 含有package cmd_如何在命令提示符下编译运行含有Package的java文件
- Atitit 项目管理 提升开发效率的项目流程方法模型 哑铃型 橄榄型 直板型
- 给Eclipse设置android的SDK位置时,出现这个:This Android SDK requires Andr...ate ADT to the latest
- matlab编写优化目标函数,基于MATLAB的二级齿轮减速器的优化设计
- 阿里云python面试题_一看“左程云:200道算法与数据结构”,二刷“阿里云:70+算法题、30种大厂笔试高频知识点”,3月过去终于挺进我梦中的字节!...
- Wifi热点工具-青青草原WiFi
- 如何在矩池云上安装语音识别模型 Whisper
- 两大「宇宙中心」之争:未来科技城如何一步步落后奥体?
- 保护眼睛的屏幕设置 Win2008R2中的Win7桌面效果设置
- bitset的使用方法
- sublime text3 配置python、ruby、c/c++(c/c++不推荐这个方法)
热门文章
- 合成复用原则java实例_第7节 合成复用原则
- MATLAB 使用CNN拟合回归模型预测手写数字的旋转角度(卷积神经网络)
- python 创建文件、文件夹、获取目录
- java对象是 什么的集合_java持有对象-集合类
- Android适配器以及作用,Android RecyclerView 通用适配器的实现
- 基于Java的音频转发服务器_javaCV开发详解之5:录制音频(录制麦克风)到本地文件/流媒体服务器(基于javax.sound、javaCV-FFMPEG)...
- 数据结构之稀疏数组 - SparseArray
- 利用SIFt特征点和RANSAC方法进行物体识别(利用openCV和vs2010实现)
- 【opencv】16.截取图像中的一个roi区域时,判断该roi是否越界
- Netty入门官方例子