leetcode summary 10/17
Contents
33. Search in Rotated Sorted Array
https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/
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 | |
""" | |
# corner case: input is None or empty list | |
if not nums: | |
return -1 | |
# binary search | |
left = 0 | |
right = len(nums) - 1 | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if nums[mid] == target: | |
return mid | |
# sorted from left to mid | |
if nums[left] <= nums[mid]: | |
# target in left and mid | |
if target < nums[mid] and target >= nums[left]: | |
right = mid - 1 | |
else: | |
left = mid + 1 | |
# sorted from mid to right | |
if nums[mid] <= nums[right]: | |
if target > nums[mid] and target <= nums[right]: | |
left = mid + 1 | |
else: | |
right = mid - 1 | |
return -1 | |
341. Flatten Nested List Iterator
- generator, sepecific to this question
- stack, interative https://leetcode.com/problems/flatten-nested-list-iterator/discuss/
155. Min Stack
https://discuss.leetcode.com/topic/7020/java-accepted-solution-using-one-stack
Author Chen Tong
LastMod 2017-10-17