1、二分查找
--704
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.lang.annotation.Target; public class _704 { public static void main(String[] args) { _704 test=new _704(); int[] nums = new int[]{-1,0,3,5,9,12}; int target = 2; System.out.println(test.search(nums, target)); } public int search(int[] nums,int target){ if(target<nums[0] || target>nums[nums.length-1]) return -1; int low=0,high=nums.length-1; while (low<=high){ int middle=low+((high-low)>>1); if(target>nums[middle]) low=middle+1; else if(target<nums[middle]) high=middle-1; else return middle; } return -1; } }View Code
标签:target,nums,int,704,low,数组,public From: https://www.cnblogs.com/kezhang/p/17005707.html