classSolution{ publicbooleansearchMatrix(int[][] matrix, int target){ if (matrix.length == 0 || matrix[0].length == 0) returnfalse; int row = matrix.length; int col = matrix[0].length; int start = 0; int end = row * col - 1;
while (start <= end) { int mid = start + (end - start) / 2; int value = matrix[mid / col][mid % col]; if (value == target) { returntrue; } elseif (value < target) { start = mid + 1; } else { end = mid - 1; } } returnfalse; } }
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
classSolution{ publicintsearchInsert(int[] nums, int target){ int len = nums.length; if (len == 0) { return0; } if (nums[len - 1] < target) { return len; } int left = 0; int right = len - 1; while (left < right) { int mid = (left + right) >>> 1; // 严格小于 target 的元素一定不是解 if (nums[mid] < target) { left = mid + 1; } else { right = mid; } } return left; } }
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n\. You have to guess which number I picked.
Every time you guess wrong, I’ll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):
1 2 3
-1 : My number is lower 1 : My number is higher 0 : Congrats! You got it!
Example :
1 2
Input: n = 10, pick = 6 Output: 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicclassSolutionextendsGuessGame{ publicintguessNumber(int n){ int left = 1; int right = n; while (left < right) { int mid = (left + right + 1) >>> 1; int guessNum = guess(mid); if (guessNum == -1) { right = mid - 1;// 中位数比猜的数大,因此比中位数大的数包括中位数都不是目标元素 } else { left = mid; } } return left; } }
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example:
1 2 3 4 5 6 7
Given n = 5, and version = 4 is the first bad version.
/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */
publicclassSolutionextendsVersionControl{ publicintfirstBadVersion(int n){ int left = 1; int right = n; while (left < right) { int mid = left + (right - left) / 2; if(isBadVersion(mid)) { right = mid; } else { left = mid + 1; } } return left; } }
A peak element is an element that is greater than its neighbors.
Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞.
Example 1:
1 2 3
Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
1 2 3 4
Input: nums = [1,2,1,3,5,6,4] Output: 1 or 5 Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution{ publicintfindPeakElement(int[] nums){ int start = 0; int end = nums.length - 1; while (start <= end) { if (start == end) return start; int mid = start + (end - start) / 2; if(nums[mid] >= nums[mid + 1]) { end = mid; } else { start = mid + 1; } } return start; } }
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Example 1:
1 2 3
Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
1 2 3
Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
1 2 3
Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
1 2 3
Input: head = [3,2,0,-4], pos = 1 Output: tail connects to node index 1 Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
1 2 3
Input: head = [1,2], pos = 0 Output: tail connects to node index 0 Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
1 2 3
Input: head = [1], pos = -1 Output: no cycle Explanation: There is no cycle in the linked list.
Follow-up: Can you solve it without using extra space?