Given a non-empty array of integers, return the k most frequent elements.
2017-11-13 21:30
375 查看
题目描述:
Given a non-empty array of integers, return the k most frequent elements.
事例:
Given [1,1,1,2,2,3] and k = 2, return [1,2]
注意事项:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
解题思想:
通过题目分析,key和value之间存在单向一对一关系,即通过指定的key,总能找到唯一的,确定的value。利用Map集合,从中取出数据时,只要给出指定的key,就可以取出对应的value。
Map提供一个Entry内部类,来封装key-value,以便通过value索取key。
代码:
package ACM;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Solution {
public static List topKFrequent(int[] nums, int k) {
Map
Given a non-empty array of integers, return the k most frequent elements.
事例:
Given [1,1,1,2,2,3] and k = 2, return [1,2]
注意事项:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
解题思想:
通过题目分析,key和value之间存在单向一对一关系,即通过指定的key,总能找到唯一的,确定的value。利用Map集合,从中取出数据时,只要给出指定的key,就可以取出对应的value。
Map提供一个Entry内部类,来封装key-value,以便通过value索取key。
代码:
package ACM;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Solution {
public static List topKFrequent(int[] nums, int k) {
Map
相关文章推荐
- Given an array of integers, sort the array according to frequency of elements
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- given an array of integers Find the index of values that satisfy A+B = C + D
- Find the two non-repeating elements in an array of repeating elements
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- Given an array of positive and negative integers find the first subarray with zero sum
- from given array of n elements find the maximum element for each consecutive sub-array of k elements.
- Given the array of digits (0 is also allowed), what is the minimal sum of two integers that are made
- Given a positive integer N, you should output the most right digit of N^N.
- CareerCup Given a binary matrix of N X N of integers , you need to return only unique rows of binary
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- Given an array of size n, find the majority element. The majority element is the element that appear
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Given an array of integers, every element appears twice except for one. Find that single one.
- The C++ Source - A Pause to Reflect: Five Lists of Five, Part II - The Most Important C++ Non-Book Publications...Ever
- CareerCup Given preorder traversal array of a BST, recontruct the BST.
- Given an array t[100] which contains numbers between 1..99. Return the duplicated value
- Interview Return Products of All Other Elements in Array