458. Last Position of Target 思路: 相等的時候拋棄左邊java
public class Solution {
/** * @param nums: An integer array sorted in ascending order * @param target: An integer * @return: An integer */
public int lastPosition(int[] nums, int target) {
// write your code here
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] <= target) {
start = mid;
} else {
end = mid;
}
}
if (nums[end] == target) {
return end;
}
if (nums[start] == target) {
return start;
}
return -1;
}
}
複製代碼
14. First Position of Targetspa
public class Solution {
/** * @param nums: The integer array. * @param target: Target to find. * @return: The first position of target. Position starts from 0. */
public int binarySearch(int[] nums, int target) {
// write your code here
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] >= target) {
end = mid;
} else {
start = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}
}
複製代碼
28. Search a 2D Matrixcode
public class Solution {
/** * @param matrix: matrix, a list of lists of integers * @param target: An integer * @return: a boolean, indicate whether matrix contains target */
public boolean searchMatrix(int[][] matrix, int target) {
// write your code here
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int start = 0;
int rows = matrix.length;
int cols = matrix[0].length;
int end = rows * cols - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (matrix[mid / cols][mid % cols] <= target) {
start = mid;
} else {
end = mid;
}
}
if (matrix[end / cols][end % cols] == target || matrix[start / cols][start % cols] == target) {
return true;
}
return false;
}
}
複製代碼
LeetCode. 240. Search a 2D Matrix II 思路: 左下 到 右上element
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int rows = matrix.length;
int cols = matrix[0].length;
int startRow = rows - 1;
int startCol = 0;
// System.out.println("startRow: " + startRow);
// System.out.println("startCol: " + startCol);
// 注意判斷這裏的越界條件
while (startRow >= 0 && startRow < rows && startCol >= 0 && startCol < cols) {
// System.out.println("startRow: " + startRow);
// System.out.println("startCol: " + startCol);
if (matrix[startRow][startCol] == target) {
return true;
} else if (matrix[startRow][startCol] < target) {
startCol = startCol + 1;
} else {
startRow = startRow - 1;
}
}
return false;
}
}
複製代碼
Lint61. Search for a Rangeget
public class Solution {
/** * @param A: an integer sorted array * @param target: an integer to be inserted * @return: a list of length 2, [index1, index2] */
public int[] searchRange(int[] nums, int target) {
// write your code here
if (nums == null || nums.length == 0) {
return new int[]{-1, -1};
}
int[] res = new int[2];
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] >= target) {
end = mid;
} else {
start = mid;
}
}
if (nums[start] == target) {
res[0] = start;
} else if (nums[end] == target) {
res[0] = end;
} else {
return new int[]{-1, -1};
}
start = 0;
end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] <= target) {
start = mid;
} else {
end = mid;
}
}
if (nums[end] == target) {
res[1] = end;
} else if (nums[start] == target) {
res[1] = start;
} else {
return new int[]{-1, -1};
}
return res;
}
}
複製代碼
Lint62. Search in Rotated Sorted Arrayit
剛纔想複雜了,其實就是在判斷二分的時候,須要check target和兩個端點值,而不能只是check一個值, 由於僅僅知足target > nums[start] 或 target < nums[mid] 都不可以保證捨棄右半邊的全部元素 來讓end = mid
, 只有當io
切割完成以後仍是一個rotated sorted array, 能夠繼續二分下去ast
public class Solution {
/** * @param A: an integer rotated sorted array * @param target: an integer to be searched * @return: an integer */
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[start] < nums[mid]) {
if (nums[start] <= target && target <= nums[mid]) {
end = mid;
} else {
start = mid;
}
} else {
if (nums[mid] <= target && target <= nums[end]) {
start = mid;
} else {
end = mid;
}
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}
}
複製代碼
Follow Up: 存在重複值function
159. Find Minimum in Rotated Sorted Arrayclass
public class Solution {
/** * @param nums: a rotated sorted array * @return: the minimum number in the array */
public int findMin(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}
int start = 0, end = nums.length - 1;
int target = nums[nums.length - 1];
// find the first element <= target
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] <= target) {
end = mid;
} else {
start = mid;
}
}
// 這裏一定存在最小值,因此不須要返回 -1
if (nums[start] <= target) {
return nums[start];
} else {
return nums[end];
}
}
}
複製代碼
Lint585.Maximum Number in Mountain Sequence
public class Solution {
/** * @param nums a mountain sequence which increase firstly and then decrease * @return then mountain top */
public int mountainSequence(int[] nums) {
// Write your code here
if (nums == null || nums.length == 0) {
return 0;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
// 這裏的OOXX的特色是 nums[mid] > nums[mid + 1] 找到第一個nums[mid] <= nums[mid + 1]的時候就停了
if (nums[mid] > nums[mid + 1]) {
end = mid;
} else {
start = mid;
}
}
return Math.max(nums[start], nums[end]);
}
}
複製代碼
Find Peak Element
public class Solution {
/* * @param A: An integers array. * @return: return any of peek positions. */
public int findPeak(int[] A) {
// write your code here
int start = 0;
int end = A.length - 2;
while(start + 1 < end){
int mid = start + (end - start) / 2;
//若是中間的數比後一位數大的話,peek點確定在mid左邊或是mid。
//若是中間的數比前一位數小的話,peek點確定在mid右邊或是mid。
if(A[mid]>A[mid + 1]){
end = mid;
} else {
start = mid;
}
}
if(A[start] < A[end]){
return end;
} else {
return start;
}
}
}
複製代碼