-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. | ||
|
||
# (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). | ||
|
||
# You are given a target value to search. If found in the array return its index, otherwise return -1. | ||
|
||
# You may assume no duplicate exists in the array. | ||
|
||
# binary search ! | ||
# http://community.bittiger.io/topic/241/%E4%B8%80%E8%B5%B7lintcode-%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E7%9C%8B%E8%BF%99%E7%AF%87%E5%B0%B1%E5%A4%9F%E4%BA%86/2 | ||
class Solution(object): | ||
def search(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: int | ||
""" | ||
if not nums: | ||
return -1 | ||
l, r = 0, len(nums)-1 | ||
# contain two possible solutions | ||
while l + 1 < r: | ||
mid = l + (r-l)/2 | ||
|
||
if nums[mid] == target: | ||
return mid | ||
|
||
# case 1 over pivot | ||
if nums[mid] < nums[l]: | ||
# imp corner | ||
if nums[mid] < target and target <= nums[r]: | ||
l = mid | ||
else: | ||
r = mid | ||
# case 2 before pivot | ||
else: | ||
# imp corner | ||
if nums[l] <= target and target < nums[mid]: | ||
r = mid | ||
else: | ||
l = mid | ||
# corner case | ||
if nums[l] == target: | ||
return l | ||
if nums[r] == target: | ||
return r | ||
|
||
return -1 | ||
|
||
# follow up duplicate | ||
# # only add this line | ||
# else: | ||
# l += 1 | ||
# 81. Search in Rotated Sorted Array II | ||
class Solution(object): | ||
def search(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: int | ||
""" | ||
if not nums: | ||
return False | ||
l, r = 0, len(nums)-1 | ||
# contain two possible solutions | ||
while l + 1 < r: | ||
mid = l + (r-l)/2 | ||
|
||
if nums[mid] == target: | ||
return True | ||
|
||
# case 1 over pivot | ||
if nums[mid] < nums[l]: | ||
if nums[mid] < target and target <= nums[r]: | ||
l = mid | ||
else: | ||
r = mid | ||
# case 2 before pivot | ||
elif nums[mid] > nums[l]: | ||
if nums[l] <= target and target < nums[mid]: | ||
r = mid | ||
else: | ||
l = mid | ||
# only add this line | ||
else: | ||
l += 1 | ||
# corner case | ||
if nums[l] == target: | ||
return True | ||
if nums[r] == target: | ||
return True | ||
|
||
return False | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words. | ||
|
||
# For example, given | ||
# s = "leetcode", | ||
# dict = ["leet", "code"]. | ||
|
||
# Return true because "leetcode" can be segmented as "leet code". | ||
|
||
# UPDATE (2017/1/4): | ||
# The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes. | ||
|
||
# | ||
class Solution(object): | ||
def wordBreak(self, s, wordDict): | ||
""" | ||
O(n^2) | ||
O(n) | ||
:type s: str | ||
:type wordDict: List[str] | ||
:rtype: bool | ||
""" | ||
if not s: | ||
return True | ||
|
||
# init | ||
# length = len(s) + 1 means s[i:j] does not contain index j of s | ||
# so we need to create one more index of the dp | ||
dp = [False for _ in range(len(s)+1)] | ||
dp[0] = True | ||
|
||
for i in range(1,len(s)+1): | ||
for j in range(i): | ||
if dp[j] and s[j:i] in wordDict: | ||
dp[i] = True | ||
print dp | ||
return dp[-1] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. | ||
|
||
# Solve it without division and in O(n). | ||
|
||
# For example, given [1,2,3,4], return [24,12,8,6]. | ||
|
||
# Follow up: | ||
# Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) | ||
|
||
|
||
# Bi way | ||
class Solution: | ||
# @param {integer[]} nums | ||
# @return {integer[]} | ||
def productExceptSelf(self, nums): | ||
p = 1 | ||
n = len(nums) | ||
output = [] | ||
for i in range(0,n): | ||
output.append(p) | ||
p = p * nums[i] | ||
p = 1 | ||
for i in range(n-1,-1,-1): | ||
output[i] = output[i] * p | ||
p = p * nums[i] | ||
return output |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Given a binary search tree and a node in it, find the in-order successor of that node in the BST. | ||
|
||
# Note: If the given node has no in-order successor in the tree, return null. | ||
|
||
class Solution(object): | ||
def inorderSuccessor(self, root,p): | ||
succ = None | ||
while root: | ||
if p.val < root.val: | ||
succ = root | ||
root = root.left | ||
else: | ||
root = root.right | ||
return succ | ||
|
||
|
||
# follow up | ||
def inorderPrevsessor(self, root, p): | ||
prev = None | ||
while root: | ||
if p.val < root.val: | ||
root = root.left | ||
else: | ||
prev = root | ||
root = root.right | ||
return prev |
This file contains 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