思路:可以使用三向切分的快速排序算法,因为这种算法会把小于某个数,等于某个数,和大于某个数的分开,分成三部分。很容易判断等于某个数的全部数量,对于这种重复值问题,很有效。
public class Solution {List<Integer> ret = new ArrayList<Integer>();public List<Integer> majorityElement(int[] nums) {int len=nums.length;quickSort(nums,0,len-1);return ret;}private void quickSort(int[] nums,int low,int high){int n=nums.length;if((high-low+1) <= n/3)return;int lt=low,gt=high,i=low+1,temp=nums[low];while(i<=gt){if(nums[i]<temp){swap(nums,i++,lt++);}else if(nums[i]>temp){swap(nums,i,gt--);}else{i++;}}if(i-lt > n/3){ret.add(nums[lt]);}quickSort(nums,low,lt-1);quickSort(nums,i,high);}private void swap(int[] nums,int i,int j){int temp=nums[i];nums[i]=nums[j];nums[j]=temp;        }
}

[leetcode]Majority Element II相关推荐

  1. [LeetCode] Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  2. LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)

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

  3. Majority Element(169) Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法) 1.Majority Element 1)description Given an array of s ...

  4. [Leetcode] Majority Element 众数

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

  5. LeetCode 229 : Majority Element II

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

  6. LeetCode Majority Element

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

  7. [LeetCode] Majority Element

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

  8. [LeetCode]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  9. leetcode 229. Majority Element II | 229. 求众数 II(找出现次数超过n/k的元素)

    题目 https://leetcode.com/problems/majority-element-ii/ 题解 思路来源于左程云<程序员代码面试指南> 问题描述 原问题:给定一个整型数组 ...

最新文章

  1. [译] RabbitMQ tutorials (3) ---- 'Pub/Sub' (Javascript)
  2. 第五课.可变图结构下的归纳式学习图注意力
  3. Python控制流---while循环
  4. linux扩充消息队列,Linux关于消息队列的使用分享
  5. Spark练习 - 提交作业到集群 - submit job via cluster
  6. iOS API离线文档下载和查阅
  7. 破站www.2637.cn居然敢截持偶的IE!
  8. JS在html中加法器,JavaScript_JavaScript程序设计之JS调试,本文主要通过一个加法器,介 - phpStudy...
  9. 设计潮流趋势|背景图案素材,增加设计对比和补充前景元素
  10. 基于树莓派的远程开锁器
  11. SQLsever --数据库置疑
  12. 【超级鼠标键盘锁】之HOOK钩子屏蔽鼠标和Ctrl+Alt+Del、Win+L之外的按键
  13. aliddns ipv6_AliDDNS v2.0.0.1 - 阿里云DNS动态解析Windows客户端 支持IPv6 微信推送
  14. 今日分享|闪魔钢化膜5.8又来了/iPhone数据线2条10.8/adidas三叶草同款89/空气炸锅/内裤3条14.9/更多...
  15. 热播剧《延禧攻略》进入台湾2019年学测语文试题
  16. 关于提高游戏中的打击感
  17. 【课上笔记】第五章 数组和广义表
  18. 《人民日报》刊登时评:《壮大国产工业软件,坚定自主创新信念》
  19. Python爬虫实战(五) :下载百度贴吧帖子里的所有图片
  20. DAS、NAS、SAN三种存储架构

热门文章

  1. 2018Java阿里编程测验_2018阿里编程测验题:最短汉密尔顿回路
  2. 快速电商排版网页布局ps神器插件_安装教程
  3. easyExcel导入导出(列锁定单元格、表头合并、导出类型限制、锁定单元格增加底色、设置密码、隐藏列等)
  4. 产品:“写个banner这么费劲?”
  5. pg数据库numeric对应oracle,Oracle类型number与PG类型numeric对比和转换策略
  6. python血脉贲张的cosplay小姐姐图片
  7. 韩服17+苹果ID验证
  8. 03SpringMVC的使用
  9. springboot集成 security 更改中英文提示信息
  10. php 只取汉字,php怎么只保留汉字