二分查找 - 20180906
Posted lizzyluvcoding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分查找 - 20180906相关的知识,希望对你有一定的参考价值。
14. First Position of Target
基本题
1 public int binarySearch(int[] nums, int target) { 2 // write your code here 3 if(nums==null || nums.length==0){ 4 return -1; 5 } 6 7 int start =0; 8 int end = nums.length-1; 9 while(start+1<end){ 10 int mid = start +(end-start)/2; 11 if(nums[mid]==target){ 12 end = mid; 13 } 14 else if(nums[mid]<target){ 15 start =mid; 16 }else{ 17 end =mid; 18 } 19 } 20 21 if(nums[start]==target){ 22 return start; 23 } 24 25 if(nums[end]==target){ 26 return end; 27 } 28 29 return -1; 30 }
74. First Bad Version
14的变种
1 public int findFirstBadVersion(int n) { 2 // write your code here 3 4 5 if(n<1){ 6 return -1; 7 } 8 9 int start = 1; 10 int end = n; 11 while(start+1<end){ 12 int mid = start+(end-start)/2; 13 if(SVNRepo.isBadVersion(mid)){ 14 end = mid; 15 } 16 else { 17 start = mid; 18 } 19 } 20 21 if(SVNRepo.isBadVersion(start)){ 22 return start; 23 } 24 25 if(SVNRepo.isBadVersion(end)){ 26 return end; 27 } 28 29 return -1; 30 }
458. Last Position of Target (同14,基本题)
1 public int lastPosition(int[] nums, int target) { 2 // write your code here 3 if(nums==null || nums.length==0){ 4 return -1; 5 } 6 7 int start =0; 8 int end = nums.length-1; 9 while(start+1<end){ 10 int mid = start +(end-start)/2; 11 if(nums[mid]==target){ 12 start = mid; 13 } 14 else if(nums[mid]<target){ 15 start =mid; 16 }else{ 17 end =mid; 18 } 19 } 20 21 if(nums[end]==target){ 22 return end; 23 } 24 25 if(nums[start]==target){ 26 return start; 27 } 28 29 return -1; 30 }
447. Search in a Big Sorted Array
倍增的思想
1.数组扩容
2.网络爬虫
1 public int searchBigSortedArray(ArrayReader reader, int target) { 2 // write your code here 3 if(reader.get(0)==target){ 4 return 0; 5 } 6 int index =1; 7 while(reader.get(index)<target){ 8 index= index*2; 9 } 10 11 int start = index/2; 12 int end = index; 13 14 while(start+1<end){ 15 int mid = start+(end-start)/2; 16 if(reader.get(mid)==target){ 17 end =mid; 18 } 19 else if(reader.get(mid)>target){ 20 end =mid; 21 }else{ 22 start = mid; 23 } 24 } 25 26 if(reader.get(start)==target){ 27 return start; 28 } 29 30 if(reader.get(end) == target){ 31 return end; 32 } 33 34 return -1; 35 }
159. Find Minimum in Rotated Sorted Array
1 public int findMin(int[] nums) { 2 // write your code here 3 if(nums==null || nums.length==0){ 4 return -1; 5 } 6 7 int start =0; 8 int end = nums.length-1; 9 while(start+1<end){ 10 int mid = start + (end-start)/2; 11 12 if(nums[mid]<nums[nums.length-1]){ 13 end = mid; 14 } 15 //注意判断条件不要写成nums[mid]>=nums[0],这种情况对于[1,2,3,4]这种未翻转的case会fail 16 else if(nums[mid]>=nums[nums.length-1]){ 17 start =mid; 18 } 19 } 20 21 if(nums[start]<nums[end]){ 22 return nums[start]; 23 }else{ 24 return nums[end]; 25 } 26 }
585. Maximum Number in Mountain Sequence
此题需再次做,边界条件设置有问题提交一直不过
1 public int mountainSequence(int[] nums) { 2 // write your code here 3 if(nums==null || nums.length==0){ 4 return -1; 5 } 6 7 int start = 0; 8 int end = nums.length-1; 9 10 11 while(start+1<end){ 12 int mid = start+(end-start)/2; 13 if(nums[mid]-nums[mid+1]>0){ 14 end = mid; 15 } 16 else { 17 start =mid; 18 } 19 } 20 21 if(nums[start]<nums[end]){ 22 return nums[end]; 23 }else{ 24 return nums[start]; 25 } 26 }
75. Find Peak Element
和585其实一样
1 public int findPeak(int[] A) { 2 // write your code here 3 4 int start =0; 5 int end = A.length-2; 6 7 while(start+1<end){ 8 int mid = start +(end-start)/2; 9 if(A[mid]>A[mid+1]){ 10 end = mid; 11 }else{ 12 start =mid; 13 } 14 } 15 16 return A[start]<A[end]?end:start; 17 18 }
62. Search in Rotated Sorted Array
此题稍微复杂一点,分析一下几种情况再写,是经典题
1 public int search(int[] A, int target) { 2 // write your code here 3 if(A==null || A.length==0){ 4 return -1; 5 } 6 7 int start =0; 8 int end = A.length-1; 9 10 while(start+1<end){ 11 int mid = start+(end-start)/2; 12 if(A[mid]==target){ 13 return mid; 14 } 15 16 if(target>=A[0]){ 17 if(A[mid]>A[0]){ 18 if(A[mid]>target){ 19 end = mid; 20 }else{ 21 start =mid; 22 } 23 }else{ 24 end =mid; 25 } 26 } 27 28 if(target<A[0]){ 29 if(A[mid]<A[0]){ 30 if(A[mid]>target){ 31 end =mid; 32 }else{ 33 start = mid; 34 } 35 }else{ 36 start =mid; 37 } 38 } 39 } 40 41 if(A[start]==target){ 42 return start; 43 } 44 if(A[end]==target){ 45 return end; 46 } 47 48 return -1; 49 50 }
28. Search a 2D Matrix
防出错,用了两次二分,其实也可以用一次二分解决(二刷时试试)
1 public boolean searchMatrix(int[][] matrix, int target) { 2 // write your code here 3 4 if(matrix==null || matrix.length==0){ 5 return false; 6 } 7 8 //如何获取矩阵的行列 9 int r = matrix.length; 10 int c = matrix[0].length; 11 12 //先找出在哪一行 13 int start = 0; 14 int end = r-1; 15 while(start+1<end){ 16 int mid = start + (end-start)/2; 17 if(matrix[mid][0]==target){ 18 return true; 19 } 20 else if(matrix[mid][0]>target){ 21 end = mid; 22 } 23 else{ 24 start = mid; 25 } 26 } 27 28 int m = (matrix[end][0]<=target)?end:start; 29 30 int mstart =0; 31 int mend = c-1; 32 while(mstart+1<mend){ 33 int mmid = mstart +(mend-mstart)/2; 34 if(matrix[m][mmid]==target){ 35 return true; 36 } 37 else if(matrix[m][mmid]>target){ 38 mend = mmid; 39 }else{ 40 mstart = mmid; 41 } 42 } 43 44 if(matrix[m][mstart]==target){ 45 return true; 46 } 47 if(matrix[m][mend]==target){ 48 return true; 49 } 50 51 return false; 52 }
一刷主要刷基本题,二刷时可以考虑刷medium和hard,可以按高频次来刷
https://www.lintcode.com/problem/?tag=binary-search
以上是关于二分查找 - 20180906的主要内容,如果未能解决你的问题,请参考以下文章