题目:

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相关推荐

  1. [勇者闯LeetCode] 169. Majority Element

    [勇者闯LeetCode] 169. Majority Element Description Given an array of size n, find the majority element. ...

  2. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. [swift] LeetCode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. Leetcode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. 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 ...

  8. leetcode讲解--169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  9. LeetCode 229 : Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

最新文章

  1. Python中requests包的安装
  2. java String pool
  3. Go之十大经典排序算法
  4. python怎么处理数据_python panda怎么处理数据
  5. python中字符串运算符及用法_python将字符串转换为运算符
  6. Spring相关API-getBean()方法的使用
  7. DotNet Core 介绍
  8. 虚拟机Centos7配置网络
  9. tkinter中text插入_tkinter做一个体重档案
  10. java 含有package cmd_如何在命令提示符下编译运行含有Package的java文件
  11. Atitit 项目管理 提升开发效率的项目流程方法模型 哑铃型  橄榄型 直板型
  12. 给Eclipse设置android的SDK位置时,出现这个:This Android SDK requires Andr...ate ADT to the latest
  13. matlab编写优化目标函数,基于MATLAB的二级齿轮减速器的优化设计
  14. 阿里云python面试题_一看“左程云:200道算法与数据结构”,二刷“阿里云:70+算法题、30种大厂笔试高频知识点”,3月过去终于挺进我梦中的字节!...
  15. Wifi热点工具-青青草原WiFi
  16. 如何在矩池云上安装语音识别模型 Whisper
  17. 两大「宇宙中心」之争:未来科技城如何一步步落后奥体?
  18. 保护眼睛的屏幕设置 Win2008R2中的Win7桌面效果设置
  19. bitset的使用方法
  20. sublime text3 配置python、ruby、c/c++(c/c++不推荐这个方法)

热门文章

  1. 合成复用原则java实例_第7节 合成复用原则
  2. MATLAB 使用CNN拟合回归模型预测手写数字的旋转角度(卷积神经网络)
  3. python 创建文件、文件夹、获取目录
  4. java对象是 什么的集合_java持有对象-集合类
  5. Android适配器以及作用,Android RecyclerView 通用适配器的实现
  6. 基于Java的音频转发服务器_javaCV开发详解之5:录制音频(录制麦克风)到本地文件/流媒体服务器(基于javax.sound、javaCV-FFMPEG)...
  7. 数据结构之稀疏数组 - SparseArray
  8. 利用SIFt特征点和RANSAC方法进行物体识别(利用openCV和vs2010实现)
  9. 【opencv】16.截取图像中的一个roi区域时,判断该roi是否越界
  10. Netty入门官方例子