asp学习网站,网站设计收费标准,中国进出口贸易网,装修案例图二分查找#xff0c;闭区间与开区间的不同解法。
题目 乍一看#xff0c;不是遍历一下找到元素就可以了。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {for (int[] ints : matrix) {for (int ans : ints) {if (ans target) return true;}}…二分查找闭区间与开区间的不同解法。
题目 乍一看不是遍历一下找到元素就可以了。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {for (int[] ints : matrix) {for (int ans : ints) {if (ans target) return true;}}return false;}
}可以通过的但还是可以优化一下的。这题可以用二分的思路可以把整个矩阵看成一大个数组若将矩阵每一行拼接在上一行的末尾则会得到一个升序数组我们可以在该数组上二分找到目标元素。但是这种方法在每一行元素个数不一时会失效。
时间复杂度O(logmn)空间复杂度O(1)。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int m matrix.length, n matrix[0].length;int low 0, high m * n - 1;while (low high) {int mid (high - low) / 2 low;int x matrix[mid / n][mid % n];if (x target) {low mid 1;} else if (x target) {high mid - 1;} else {return true;}}return false;}
}以上为标准二分查找的写法其中首尾做为边界条件时while (low high)接着如果目标值偏大则在右边找即low mid 1反之目标值偏小时往左边找即 high mid - 1然后当low指针从左边到target时扫了一遍当high指针从右边到target时扫了一遍当两个指针重叠相遇时进一步判断两个指针定住的数是不是目标数然后退出循环。
这题还可以先对行二分再对列二分进行两次二分查找。每行的第一个元素大于前一行的第一个元素矩阵第一列的元素是升序的。对矩阵的第一列的元素二分查找找到最后一个不大于目标值的元素然后在该元素所在行中二分查找目标值是否存在。
时间复杂度O(logmn)空间复杂度O(1)。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int rowIndex binarySearchFirstColumn(matrix, target);if (rowIndex 0) {return false;}return binarySearchRow(matrix[rowIndex], target);}public int binarySearchFirstColumn(int[][] matrix, int target) {int low -1, high matrix.length - 1;while (low high) {int mid (high - low 1) / 2 low;if (matrix[mid][0] target) {low mid;} else {high mid - 1;}}return low;}public boolean binarySearchRow(int[] row, int target) {int low 0, high row.length - 1;while (low high) {int mid (high - low) / 2 low;if (row[mid] target) {return true;} else if (row[mid] target) {high mid - 1;} else {low mid 1;}}return false;}
}其中用了一个左开右闭区间的二分查找可以先进行扩充边界然后条件改为while (low high)接着low要设置为mid退出的条件即当low跟high重叠时此时的low是mid了看是不是要找的target。当然这题用左闭右开区间的二分查找也是类似不过要注意以下返回值要取到不大于目标值的最大行索引。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int rowIndex binarySearchFirstColumn(matrix, target);if (rowIndex 0) {return false;}return binarySearchRow(matrix[rowIndex], target);}
public int binarySearchFirstColumn(int[][] matrix, int target) {int low 0, high matrix.length; while (low high) { int mid (high - low) / 2 low; if (matrix[mid][0] target) { low mid 1;} else { high mid; }}return high-1;
}public boolean binarySearchRow(int[] row, int target) {int low 0, high row.length - 1;while (low high) {int mid (high - low) / 2 low;if (row[mid] target) {return true;} else if (row[mid] target) {high mid - 1;} else {low mid 1;}}return false;}
}然后也可以用最经典的标准二分模板去写。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int rowIndex binarySearchFirstColumn(matrix, target);if (rowIndex 0) {return false;}return binarySearchRow(matrix[rowIndex], target);}
public int binarySearchFirstColumn(int[][] matrix, int target) {// 初始化low为0high为矩阵最后一行的索引int low 0, high matrix.length - 1;while (low high) { // 当low不大于high时继续循环int mid (high - low) / 2 low; // 计算中间位置// 如果中间位置的行的第一个元素小于或等于目标值则该行及之后的行可能是候选行if (matrix[mid][0] target) {low mid 1; } else {high mid - 1; }}// 返回不大于目标值的最大行索引return high; // 这里返回high因为high指向的是不大于目标值的最大行索引或未找到返回-1}public boolean binarySearchRow(int[] row, int target) {int low 0, high row.length - 1;while (low high) {int mid (high - low) / 2 low;if (row[mid] target) {return true;} else if (row[mid] target) {high mid - 1;} else {low mid 1;}}return false;}
}二分查找模板巧记先写好数组中的左右边界值然后while (low high)接着写mid的求值 int mid (high - low) / 2 low再到判断若目标值偏大往大的找即low指针右移若目标值偏小往小的找即high指针左移最后当low跟high重叠时对当前元素做判断返回值依题而定。 文章转载自: http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.fndfn.cn.gov.cn.fndfn.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.hpggl.cn.gov.cn.hpggl.cn http://www.morning.dqxnd.cn.gov.cn.dqxnd.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.smmrm.cn.gov.cn.smmrm.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.lnrhk.cn.gov.cn.lnrhk.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.cfmrb.cn.gov.cn.cfmrb.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.jqpq.cn.gov.cn.jqpq.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.jmllh.cn.gov.cn.jmllh.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.tjcgl.cn.gov.cn.tjcgl.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.hilmwmu.cn.gov.cn.hilmwmu.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.lwdzt.cn.gov.cn.lwdzt.cn http://www.morning.xfmwk.cn.gov.cn.xfmwk.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.trplf.cn.gov.cn.trplf.cn