标签:
题目描述:public class Solution { public int Search(int[] nums, int target) { if(nums.Length == 0){ return -1; } if(nums.Length == 1){ return nums[0] == target ? 0 : -1; } var l = 0; var r = nums.Length - 1; while(l < r - 1){ var m = (l + r) / 2; if(nums[m] == target){ return m; } if(nums[r] == target){ return r; } if(nums[l] == target){ return l; } if(nums[m] > nums[l] && nums[m] > nums[r]){ if(target < nums[r]){ l = m; } else { if(target > nums[m]){ l = m; } else{ r = m; } } } else if(nums[m] < nums[l] && nums[m] < nums[r]){ if(target > nums[l]){ r = m; } else{ if(target > nums[m]){ l = m; } else{ r = m; } } } else if(nums[m] > nums[l] && nums[m] < nums[r]){ if(target > nums[m]){ l = m; } else { r = m; } } } if(target == nums[l]){ return l; } if(target == nums[r]){ return r; } return -1; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Search in Rotated Sorted Array
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/49531323