日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二分查找、变形及应用

發(fā)布時(shí)間:2023/11/29 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二分查找、变形及应用 小編覺得挺不錯(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)行如下兩層二分查找:

  • 先在第一列中進(jìn)行二分。找出 target 或者找出其所有的行。當(dāng)二分查找的返回值為 -1時(shí),表示矩陣中不存在 target。
  • 在上次二分查找返回的行中再次二分。
  • 代碼如下

    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.

    思路

  • 每次在矩陣的左邊一列進(jìn)行二叉查找,若查找到 target,返回 true。否則設(shè)查找結(jié)束后返回值為end。end = -1時(shí),直接返回false。
  • 取出子矩陣 [0 ~ end][1~ n - 1]。
  • 重復(fù)上述過程共 n 次(因?yàn)槊坎檎乙淮?#xff0c;矩陣列數(shù)減1)。
  • 時(shí)間復(fù)雜度為 \(O(nlgm)\)。當(dāng) n > m時(shí),先在列中進(jìn)行二分查找。
  • 代碼如下

    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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。