题解 | #二分查找-I#
二分查找-I
https://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ public int search (List<int> nums, int target) { if( nums.Count == 1 ){ if(nums[0] == target){ return 0; } else{ return -1; } } int left = 0, right = nums.Count -1; while(left <= right){ int leftNum = nums[left]; int rightNum = nums[right]; if(leftNum == target){ return left; }else if (rightNum == target) { return right; }else{ left ++; right --; } } return -1; } }