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