Last active
September 28, 2017 21:31
-
-
Save cixuuz/9fb76ba12a34fd8a89aabf6dbef11d24 to your computer and use it in GitHub Desktop.
[153. Find Minimum in Rotated Sorted Array] #leetcode
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 { | |
| // O(n) | |
| public int findMin(int[] nums) { | |
| int res = nums[0]; | |
| for (int n : nums) { | |
| if (n >= res) { | |
| res = n; | |
| } else { | |
| return n; | |
| } | |
| } | |
| return nums[0]; | |
| } | |
| } | |
| class Solution { | |
| // O(lg(n)) | |
| public int findMin(int[] nums) { | |
| if (nums == null || nums.length == 0) return 0; | |
| int left = 0, right = nums.length-1; | |
| while (left < right) { | |
| if (nums[left] < nums[right]) { | |
| return nums[left]; | |
| } | |
| int mid = (left + right) / 2; | |
| if (nums[mid] >= nums[left] && nums[mid] > nums[right]) { | |
| left = mid + 1; | |
| } else { | |
| right = mid; | |
| } | |
| } | |
| return nums[left]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment