二分查找、变形及应用
生活随笔
收集整理的這篇文章主要介紹了
二分查找、变形及应用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
[LeetCode] 35 Search Insert Position
題目
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.You may assume no duplicates in the array.測試案例
Input: [1,3,5,6], 5 Output: 2Input: [1,3,5,6], 2 Output: 1Input: [1,3,5,6], 7 Output: 4Input: [1,3,5,6], 0 Output: 0代碼如下
class Solution {public int searchInsert(int[] nums, int target) {int start = 0, end = nums.length - 1, mid;while(start <= end){mid = (start + end) >> 1;if(nums[mid] == target){return mid;}else if(nums[mid] > target){end = mid - 1;}else{start = mid + 1;}}return start; } }[LeetCode 34] Find First and Last Position of Element in Sorted Array
題目
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1].測試案例
Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1]代碼如下
class Solution {public int[] searchRange(int[] nums, int target) { int[] res = new int[2];if(nums.length == 0){res[0] = res[1] = -1;return res;}res[0] = find(nums, target, true);res[1] = find(nums, target, false);if(res[0] >= nums.length || nums[res[0]] != target){res[0] = -1;res[1] = -1;}return res;}int find(int[] nums, int target, boolean left){int mid;int start = 0, end = nums.length - 1;while(start <= end){mid = (start + end) >> 1;if(nums[mid] > target){end = mid - 1;}else if(nums[mid] == target){if(left){end = mid - 1;}else{start = mid + 1;}}else{start = mid + 1;}}return left ? start : end;} }[LeetCode 153] Find Minimum in Rotated Sorted Array
題目
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). Find the minimum element.You may assume no duplicate exists in the array.測試案例
Input: [3,4,5,1,2] Output: 1Input: [4,5,6,7,0,1,2] Output: 0代碼如下
class Solution {public int findMin(int[] nums) {int n = nums.length, start = 0, end = n - 1, mid;while(start < end){if(nums[start] < nums[end]){return nums[start];}mid = (start + end) >> 1;if(nums[mid] >= nums[start]){start = mid + 1;}else{end = mid;}}return nums[start];} }[LeetCode 74] Search a 2D Matrix
題目
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: - Integers in each row are sorted from left to right. - The first integer of each row is greater than the last integer of the previous row.測試案例
Input: matrix = [[1, 3, 5, 7],[10, 11, 16, 20],[23, 30, 34, 50] ] target = 3 Output: trueInput: matrix = [[1, 3, 5, 7],[10, 11, 16, 20],[23, 30, 34, 50] ] target = 13 Output: false思路
矩陣的特點(diǎn)是當(dāng)進(jìn)行兩層循環(huán)遍歷,外層為行,內(nèi)層為列時(shí),矩陣中所有元素都是遞增的。所以進(jìn)行如下兩層二分查找:
代碼如下
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int m, n;if((m = matrix.length) == 0 || (n = matrix[0].length) == 0){return false;}//在第一列中搜索,找出 target 所在的行end。int start = 0, end = m - 1, mid;while(start <= end){mid = (start + end) >> 1;if(matrix[mid][0] == target){return true;}else if(matrix[mid][0] > target){end = mid - 1;}else{ start = mid + 1;}}int pos = end;if(pos == -1){return false;}for(start = 0, end = n - 1; start <= end;){mid = (start + end) >> 1;if(matrix[pos][mid] == target){return true;}else if(matrix[pos][mid] > target){end = mid - 1;}else{start = mid + 1;}}return false;} }[LeetCode 240] Search a 2D Matrix II
題目
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.測試案例
[[1, 4, 7, 11, 15],[2, 5, 8, 12, 19],[3, 6, 9, 16, 22],[10, 13, 14, 17, 24],[18, 21, 23, 26, 30] ] Given target = 5, return true. Given target = 20, return false.思路
代碼如下
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int m, n, start, end, mid;//初始化 m 和 nif((m = matrix.length) == 0 || (n = matrix[0].length) == 0){reutrn false; } //初始化start 和 mid start = 0;end = m - 1;//進(jìn)行 n 次 二分查找for(int i = 0; i < n; i ++){start = 0;while(start <= end){mid = (start + end) >> 1;if(matrix[mid][i] == target){return true;}else if(matrix[mid][i] < target){start = mid + 1;}else{end = mid - 1;}}if(end == -1){break;}} reutrn false;} }轉(zhuǎn)載于:https://www.cnblogs.com/echie/p/9592186.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的二分查找、变形及应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2. Add Two Numbers
- 下一篇: golang json数组拼接