- 浏览: 183515 次
- 性别:
- 来自: 济南
文章分类
最新评论
Majority Element II
给定一个长度为n的数组,找出所有出现次数大于[n/3]的元素。
这道题是Majority Element的变形,如果次数大于[n/2]时,只可能有一个元素,这里要求出现次数多于[n/3]次,也就是说有可能有两个元素,同样我们可以用哈希表来做,这样时间复杂度为O(n),空间复杂度为O(n)。代码如下:
用哈希来做勉强可以通过测试,我们也可以用Moore‘s voting算法,与上一题不同的是这里需要两个带比较的元素,因为结果可能有两个,我们遍历完之后,还需要再遍历一遍数组,确定是否它们的出现次数大于[n/3]。这样时间复杂度为O(n),空间复杂度为O(1),代码如下:
给定一个长度为n的数组,找出所有出现次数大于[n/3]的元素。
这道题是Majority Element的变形,如果次数大于[n/2]时,只可能有一个元素,这里要求出现次数多于[n/3]次,也就是说有可能有两个元素,同样我们可以用哈希表来做,这样时间复杂度为O(n),空间复杂度为O(n)。代码如下:
public class Solution { public List<Integer> majorityElement(int[] nums) { HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); List<Integer> list = new ArrayList<Integer>(); if(nums == null || nums.length == 0) return list; for(int i = 0; i < nums.length; i++) { if(hm.containsKey(nums[i])) { hm.put(nums[i],hm.get(nums[i]) + 1); } else { hm.put(nums[i], 1); } if(hm.get(nums[i]) > (nums.length / 3) && !list.contains(nums[i])) list.add(nums[i]); } return list; } }
用哈希来做勉强可以通过测试,我们也可以用Moore‘s voting算法,与上一题不同的是这里需要两个带比较的元素,因为结果可能有两个,我们遍历完之后,还需要再遍历一遍数组,确定是否它们的出现次数大于[n/3]。这样时间复杂度为O(n),空间复杂度为O(1),代码如下:
public class Solution { public List<Integer> majorityElement(int[] nums) { List<Integer> list = new ArrayList<Integer>(); if(nums == null || nums.length == 0) return list; int result1 = 0; int result2 = 0; int count1 = 0; int count2 = 0; for(int i = 0; i < nums.length; i++) { if(count1 == 0 && nums[i] != result2) { result1 = nums[i]; } else if(count2 == 0 && nums[i] != result1) { result2 = nums[i]; } if(nums[i] == result1) { count1 ++; } else if(nums[i] == result2) { count2 ++; } else { count1 --; count2 --; } } count1 = 0; count2 = 0; for(int i = 0; i < nums.length; i++) { if(nums[i] == result1) count1 ++; if(nums[i] == result2) count2 ++; } if(count1 > nums.length / 3) list.add(result1); if(count2 > nums.length / 3 && !list.contains(result2)) list.add(result2); return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java郑 java_leetcode题解之Online Majority Element In Subarray.java
c c语言_leetcode题解之0229_majority_element_ii
python python_leetcode题解之229_Majority_Element_II.py
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-...
javascript js_leetcode题解之169-majority-element.js
java lru leetcode leetcode_java Java版中的解决方案。 Dectinc_Chen 解决问题清单 已解决问题列表 [除Self之外的数组乘积](md/除Self.md之外的数组...II](md/Majority Element II.md) - 2015-09-22 [摘要范围](md/Sum
27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) [Python](./Python/remove-element.py) | _O(n)_ | _O(1)_ | Easy || 31 | [Next Permutation]...
Element II 解决方法:Majority Voting算法的变种。但是最终的算法实现形式,很难理解。 2018-08-19 19:16 LeetCode: 79. Word Search 解决方法:DFS LeetCode: 31. Next Permutation 解决方法:掌握排列组合的字典...
[C ++](./ Algorithms / Majority Element II / Source.cpp) 中等的 2015年7月4日 228 [C ++](./算法/摘要范围/Source.cpp) 简单的 2015年7月1日 227 [C ++](./ Algorithms / Basic Calculator II / Sour
majority element ii . 使用 "摩尔投票法"。 LCA 二叉树最近共同祖先问题. 递归结题的思路,在左子树、右子树查找两个节点,可能的结果有: 该节点就是其中的一个节点,则返回该节点 两个节点都不在左边,那么肯定都...
leetcode添加元素使和等于 本项目为Njueers所共享 仓库内容主要为平时刷题的submit、遇到的...Majority Element II,类似求一个数组中,超过一定重复次数的数,可以考虑摩尔投票算法 2019/07/17 新增 307. Range Sum Que
def majorityElementII(nums): table = {} for i, num in enumerate(nums): if num not in table: table[num] = 1 else: table[num] += 1 for key, value in table.items(): if value > len(nums) // 3: ...
在编程领域,寻找多数元素(Majority Element)是一项常见的任务,尤其在算法设计和数据处理中。多数元素指的是在一个整数数组中出现次数超过数组长度一半的元素。本主题将详细探讨如何用C++语言来解决这个问题。 ...
leetcode有效期 algorithmTask 数据结构与算法练习 Task.1 数组 实现一个支持动态扩容的数组,支持增删改操作 ...中文版:https://leetcode-cn.com/problems/majority-element/ Missing Positive(求缺失
加油站 ...229_Majority_Element_II 274_H_索引 275_H_Index_II 217_Contain_Duplicate 55_Jump_Game 45_Jump_Game_II 121_Best_Time_to_Buy_and_Sell_Stock 122_Best_Time_to_Buy_and_Sell_Stock_
#169 Majority Element #171 Excel Sheet Column Number #217 Contains Duplicate #226 Invert Binary Tree #237 Delete Node in a Linked List #238 Product of Array Except Self #242 Valid Anagram #258 Add ...
Find a majority element in an array of size 'n'3. Find the number occuring odd number of times in a given array of size 'n'4. Algorithm to reverse an array5. Algorithm to rotate array of size 'n' by ...
Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array Shortest Unsorted Continuous Subarray Rotate Image 宝石与石头Jewels and Stones Kids With the Greatest Number of Candies 美团...
圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 ...是一个常数,所以可以计算出缺失的那个169.Majority Element -> Hashtable | Boyer-Moore 多数投票算法283. 移零 -> 27. 移除元素243.Shortest Word Dista