Last active
May 26, 2017 22:14
-
-
Save BiruLyu/e8c0c14aeb8fe7420b50bd3350db4dee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int search(int[] nums, int target) { | |
| if(nums == null || nums.length == 0) return -1; | |
| int start = 0; | |
| int end = nums.length - 1; | |
| while(start <= end){ | |
| int mid = (start + end) / 2; | |
| if(nums[mid] == target) { | |
| return mid; | |
| } | |
| if (nums[start] <= nums[mid]){ | |
| if (target < nums[mid] && target >= nums[start]){ | |
| end = mid - 1; | |
| } else { | |
| start = mid + 1; | |
| } | |
| } | |
| if(nums[mid] <= nums[end]){ | |
| if (target > nums[mid] && target <= nums[end]){ | |
| start = mid + 1; | |
| } else { | |
| end = mid - 1; | |
| } | |
| } | |
| } | |
| return -1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution(object): | |
| def search(self, nums, target): | |
| """ | |
| :type nums: List[int] | |
| :type target: int | |
| :rtype: int | |
| """ | |
| if not nums: | |
| return -1; | |
| start = 0; | |
| virtualEnd = len(nums) - 1; | |
| for i in range(len(nums)): | |
| if i > 0 and nums[i] < nums[i - 1]: | |
| start = i; | |
| break; | |
| virtualEnd = start + len(nums) - 1; | |
| while(start <= virtualEnd): | |
| midpoint = (start + virtualEnd) / 2 ; | |
| if target == nums[midpoint % len(nums)]: | |
| return midpoint % len(nums); | |
| elif target < nums[midpoint % len(nums)]: | |
| virtualEnd = midpoint - 1 ; | |
| else: | |
| start = midpoint + 1; | |
| return -1; | |
| ############################################################## | |
| #Using once binary search | |
| class Solution(object): | |
| def search(self, nums, target): | |
| """ | |
| :type nums: List[int] | |
| :type target: int | |
| :rtype: int | |
| """ | |
| if not nums: | |
| return -1; | |
| start = 0; | |
| end = len(nums) - 1; | |
| while(start <= end): | |
| midpoint = (start + end) / 2 ; | |
| if target == nums[midpoint]: | |
| return midpoint; | |
| if nums[start] <= nums[midpoint]: | |
| if target < nums[midpoint] and target >= nums[start]: | |
| end = midpoint - 1; | |
| else: | |
| start = midpoint + 1; | |
| if nums[midpoint] <= nums[end]: | |
| if target > nums[midpoint] and target <= nums[end]: | |
| start = midpoint + 1; | |
| else: | |
| end = midpoint - 1; | |
| return -1; | |
| """ | |
| TESTCASES: | |
| Input: | |
| [] | |
| 5 | |
| [1] | |
| 1 | |
| [1] | |
| 0 | |
| [1,3] | |
| 0 | |
| [4,5,6,7,0,1,2] | |
| 3 | |
| [4,5,6,7,0,1,2] | |
| 6 | |
| [0,1,2,3,4,5,6] | |
| 5 | |
| [0,1,2,3,4,5,6] | |
| 3 | |
| [0,1,2,3,4,5,6] | |
| 0 | |
| [0,1,2,3,4,5,6] | |
| 7 | |
| Output: | |
| -1 | |
| 0 | |
| -1 | |
| -1 | |
| -1 | |
| 2 | |
| 5 | |
| 3 | |
| 0 | |
| -1 | |
| """ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment