You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations.
Return *the minimum number of operations to reduce xto exactly0if it is possible, otherwise, return *-1.
Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.
Return *the quotient after dividing dividend by *divisor.
**Note: **Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2<sup>31</sup>, 2<sup>31</sup><span> </span>− 1]. For this problem, if the quotient is strictly greater than2<sup>31</sup><span> </span>- 1, then return 2<sup>31</sup><span> </span>- 1, and if the quotient is strictly less than-2<sup>31</sup>, then return -2<sup>31</sup>.
Given a string array words, return the maximum value oflength(word[i]) * length(word[j])where the two words do not share common letters. If no such two words exist, return 0.
You are given a network of n nodes, labeled from 1 to n. You are also given times, a list of travel times as directed edges times[i] = (u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>), where u<sub>i</sub> is the source node, v<sub>i</sub> is the target node, and w<sub>i</sub> is the time it takes for a signal to travel from source to target.
We will send a signal from a given node k. Return the time it takes for all the n nodes to receive the signal. If it is impossible for all the n nodes to receive the signal, return -1.
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:
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 directed acyclic graph, with n vertices numbered from 0 to n-1, and an array edges where edges[i] = [from i, to] represents a directed edge from node from i to node to i .
Find the smallest set of vertices from which all nodes in the graph are reachable. It’s guaranteed that a unique solution exists.
Notice that you can return the vertices in any order.
Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
Return the shortest such subarray and output its length.
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.