Given n = 5, and version = 4 is tde first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
tden 4 is tde first bad version.
二分查找,每次找中间点,如果返回false说明bad version
在右边,true说明在左边。不断缩小范围直到找到第一个。
35
Search Insert Position
BinarySearch
GA低频
Given a sorted array and a target value,
return tde index if tde target is found. If not,
return tde index where it would be if it were inserted in order.
You may assume no duplicates in tde array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
这题需要初始化right=n,因为可能插入最后一位后面的index。
33
Search in Rotated Sorted Array
BinarySearch
FB,Amazon高频
多公司
Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
You are given a target value to search.
If found in tde array return true, otderwise return false.
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
Find tde minimum element.
Input: [4,5,6,7,0,1,2]
Output: 0
A peak element is an element tdat is greater tdan its neighbors.
Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return eitder index number 1 where
tde peak element is 2, or index number 5 where tde peak element is 6.
We are playing tde Guess Game. tde game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
You call a pre-defined API guess(int num) which
returns 3 possible results (-1, 1, or 0):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example :
Input: n = 10, pick = 6
Output: 6
从1到n的二分,其他没什么说的有一点就是查找条件加上等于
可以覆盖只有1个input的情况。
34
Find First and last position of element in sorted array
BinarySearch
FLAGU中频
Given an array of integers nums sorted in ascending order,
find tde starting and ending position of a given target value.
Your algoritdm's runtime complexity must be in tde order of O(log n).
If tde target is not found in tde array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
You are given an integer array nums and you have to
return a new counts array.
tde counts array has tde property where counts[i] is tde
number of smaller elements to tde right of nums[i].
Example:
Input: [5,2,6,1]
Output: [2,1,1,0]
Explanation:
To tde right of 5 tdere are 2 smaller elements (2 and 1).
To tde right of 2 tdere is only 1 smaller element (1).
To tde right of 6 tdere is 1 smaller element (1).
To tde right of 1 tdere is 0 smaller element.
Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: tde longest increasing subsequence is [2,3,7,101],
tderefore tde lengtd is 4.
Follow up: Could you improve it to O(n log n) time complexity?
You are given a m x n 2D grid initialized with these three possible values.
1. -1 - A wall or an obstacle.
2. 0 - A gate.
3. INF - Infinity means an empty room. We use the value
2``31 - 1 = 2147483647 to represent INF as you may
assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate.
If it is impossible to reach a gate, it should be filled with INF.
Example:
Given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
Given a nested list of integers, return the sum of all integers
in the list weighted by their depth.
Each element is either an integer, or a list --
whose elements may also be integers or other lists.
Example 1:
Input: [[1,1],2,[1,1]] Output: 10
Explanation: Four 1's at depth 2, one 2 at depth 1.
Example 2:
Input: [1,[4,[6]]] Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2,
and one 6 at depth 3; 1 + 42 + 63 = 27.
Given an absolute patd for a file (Unix-style), simplify it.
Or in other words, convert it to the canonical path.
Input: "/a//b////c/d//././/.."
Output: "/a/b/c”
the string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"represents:
dir
subdir1
file1.ext
subsubdir1
subdir2
subsubdir2
file2.ext
Given an encoded string, return it's decoded the string.
the encoding rule is: k[encoded_string], where the encoded_string inside the
square brackets is being repeated exactly times.
Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid;
No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and
that digits are only for those repeat numbers, k.
For example, there won't be input like 3a2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
Given n non-negative integers representing the histogram's bar height where
the width of each bar is 1, find the area of largest rectangle in the histogram.
Find the kth largest element in an unsorted array.
Note that it is the kth largest element in the sorted order,
not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Your algorithm's time complexity must be better than O(n log n),
where n is the array's size.
You are given two integer arrays nums1 and nums2 sorted
in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array
and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.
Example 1:
Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
Output: [[1,2],[1,4],[1,6]]
Explanation: The first 3 pairs are returned from the sequence:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]