LintCode 14---二分查找
Posted cnmoti
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LintCode 14---二分查找相关的知识,希望对你有一定的参考价值。
public class Solution { /** * @param nums: The integer array. * @param target: Target to find. * @return: The first position of target. Position starts from 0. */ public int binarySearch(int[] nums, int target) { int left = 0,right = nums.length-1; int mid = (left + right) / 2; while (left < right) { if (nums[mid] < target) left = mid + 1; if (nums[mid] >= target) right = mid; mid = (left + right) / 2; } return nums[mid] == target ? mid : -1; } }
以上是关于LintCode 14---二分查找的主要内容,如果未能解决你的问题,请参考以下文章