Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
Only numbers 1 through 9 are used.
Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
Implement the NestedIterator class:
NestedIterator(List<NestedInteger> nestedList) Initializes the iterator with the nested list nestedList.
int next() Returns the next integer in the nested list.
boolean hasNext() Returns true if there are still some integers in the nested list and false otherwise.
Your code will be tested with the following pseudocode:
initialize iterator with nestedList res = [] while iterator.hasNext() append iterator.next() to the end of res return res
If res matches the expected flattened list, then your code will be judged as correct.
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger { * * // @return true if this NestedInteger holds a single integer, rather than a nested list. * public boolean isInteger(); * * // @return the single integer that this NestedInteger holds, if it holds a single integer * // Return null if this NestedInteger holds a nested list * public Integer getInteger(); * * // @return the nested list that this NestedInteger holds, if it holds a nested list * // Return empty list if this NestedInteger holds a single integer * public List<NestedInteger> getList(); * } */ publicclassNestedIteratorimplementsIterator<Integer> { Queue<NestedInteger> q; publicNestedIterator(List<NestedInteger> nestedList) { q = newLinkedList<>(); for(NestedInteger item : nestedList){ buildQueue(item); } } privatevoidbuildQueue(NestedInteger ni){ if(!ni.isInteger()){ for(NestedInteger item : ni.getList()){ buildQueue(item); } } else{ q.offer(ni); } }
@Override public Integer next() { return q.poll().getInteger(); }
/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].
Return true if there is a 132 pattern in nums, otherwise, return false.
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
You are given a string s and an integer k, a kduplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and the right side of the deleted substring to concatenate together.
We repeatedly make kduplicate removals on s until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique.
Given an array of points where points[i] = [x<sub>i</sub>, y<sub>i</sub>] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0).
The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x<sub>1</sub><span> </span>- x<sub>2</sub>)<sup>2</sup><span> </span>+ (y<sub>1</sub><span> </span>- y<sub>2</sub>)<sup>2</sup>).
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).
Solution
此题可以采用快速排序的思想。可以在O(n)时间复杂度里完成排序。
Solution 2
优先级队列,根据每个点距离的平方排序。时间复杂度O(nlogk)。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicint[][] kClosest(int[][] points, int k) { int[][] ret = newint[k][2]; PriorityQueue<int[]> pq = newPriorityQueue<>((a, b) -> squaredDistance(a) - squaredDistance(b)); for(int[] point : points){ pq.add(point); } for(inti=0; i < k; i++){ ret[i] = pq.poll(); } return ret; } privateintsquaredDistance(int[] point){ intx= point[0], y = point[1]; return x * x + y * y; } }
Solution 3
直接排序。时间复杂度为O(nlogn)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { publicint[][] kClosest(int[][] points, int k) { int[][] ret = newint[k][2]; Arrays.sort(points, (a, b) -> squaredDistance(a) - squaredDistance(b)); for(inti=0; i < k; i++){ ret[i] = points[i]; } return ret; } privateintsquaredDistance(int[] point){ intx= point[0], y = point[1]; return x * x + y * y; } }
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.
Return the sorted string. If there are multiple answers, return any of them.
Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
Implement the Solution class:
Solution(int[] nums) Initializes the object with the integer array nums.
int[] reset() Resets the array to its original configuration and returns it.
int[] shuffle() Returns a random shuffling of the array.
classSolution { int[] data; int[] shuffle; Random r; publicSolution(int[] nums) { data = nums; shuffle = newint[nums.length]; r = newRandom(); for(inti=0; i < data.length; i++){ shuffle[i] = nums[i]; } } publicint[] reset() { return data; } publicint[] shuffle() { for(inti= data.length-1 ; i >= 0; i--){ swap(i, r.nextInt(i+1)); } return shuffle; } privatevoidswap(int i, int j){ inttemp= shuffle[i]; shuffle[i] = shuffle[j]; shuffle[j] = temp; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
classSolution { int[] data; Random r; publicSolution(int[] nums) { data = nums; r = newRandom(); } publicint[] reset() { return data; } publicint[] shuffle() { ArrayList<Integer> bin = newArrayList<>(); for(inti=0; i < data.length; i++){ bin.add(data[i]); } int[] ret = newint[data.length]; for(inti=0 ; i < data.length; i++){ intrandom= r.nextInt(bin.size()); ret[i] = bin.get(random); bin.remove(random); } return ret; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.
When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return trueif you can visit all the rooms, orfalseotherwise.