-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
160 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,50 @@ | ||
""" | ||
非常复杂,思考的方向错了。应该首先判断mid | ||
""" | ||
|
||
|
||
class Solution: | ||
def search(self, nums, target) -> int: | ||
left, right = 0, len(nums) - 1 | ||
mid = 0 | ||
|
||
while left <= right: | ||
mid = (left + right) // 2 | ||
# if nums[left]<target<nums[mid]: | ||
# right = mid | ||
if target > nums[left]: | ||
if nums[mid] > target: | ||
# nums[left] < target < nums[mid] | ||
right = mid - 1 | ||
elif nums[mid] < nums[left]: | ||
# nums[mid] < nums[left] < target | ||
right = mid - 1 | ||
elif nums[mid] < target: | ||
# nums[left] <= nums[mid]<=target | ||
left = mid + 1 | ||
else: | ||
return mid | ||
|
||
elif target < nums[left]: | ||
if nums[mid] < target: | ||
# nums[mid] < target < nums[left] | ||
left = mid + 1 | ||
elif nums[mid] > nums[left]: | ||
# target<nums[left]<nums[mid] | ||
left = mid + 1 | ||
elif nums[mid] > target: | ||
# target<nums[mid]<=nums[left] | ||
right = mid - 1 | ||
else: | ||
return mid | ||
else: | ||
return left | ||
|
||
return -1 | ||
|
||
|
||
nums = [4, 5, 6, 7, 0, 1, 2] | ||
target = 0 | ||
nums = [1] | ||
target = 0 | ||
print(Solution().search(nums, target)) |
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,33 @@ | ||
package problem33 | ||
|
||
func search(nums []int, target int) int { | ||
if len(nums) == 0 { | ||
return -1 | ||
} | ||
|
||
left, right := 0, len(nums)-1 | ||
var mid int | ||
for left < right { | ||
mid = (left + right) / 2 | ||
if nums[mid] < nums[left] { | ||
if target > nums[mid] && target <= nums[right] { | ||
// 为什么left要mid+1,而right可以=mid? | ||
// 因为mid=(left+right)/2,mid会=left,所以要+1 | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} else { // nums[mid] >= nums[left] | ||
if nums[left] <= target && target <= nums[mid] { | ||
right = mid | ||
} else { | ||
left = mid + 1 | ||
} | ||
} | ||
} | ||
|
||
if nums[left] == target { | ||
return left | ||
} | ||
return -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,61 @@ | ||
* other solutions | ||
|
||
** find minimum | ||
Firstly, find the minimum. Then, see the List as a ascending list which binary search can be applied to. | ||
Time complexity: O(logN). | ||
#+BEGIN_SRC python | ||
|
||
class Solution: | ||
def search(self, nums: 'List[int]', target: 'int') -> 'int': | ||
left = 0 | ||
right = len(nums) - 1 | ||
while left < right: | ||
mid = int((left + right)/2) | ||
if nums[mid] < nums[right]: | ||
right = mid | ||
elif nums[mid] > nums[right]: | ||
left = mid + 1 | ||
print(left) | ||
lo = left | ||
left = 0 | ||
right = len(nums) - 1 | ||
while left <= right: | ||
mid = int((left + right)/2) | ||
real_mid = (mid + lo ) % len(nums) | ||
if nums[real_mid] == target: | ||
return real_mid | ||
if nums[real_mid] < target: | ||
left = mid + 1 | ||
elif nums[real_mid] > target: | ||
right = mid - 1 | ||
|
||
return -1 | ||
|
||
#+END_SRC | ||
|
||
** if | ||
|
||
py soln 看到的。用 go 重写。 | ||
#+BEGIN_SRC golang | ||
|
||
func search(nums []int, target int) int { | ||
left, right := 0, len(nums)-1 | ||
var mid int | ||
for left <= right { | ||
mid = (left + right) / 2 | ||
if nums[mid] == target { | ||
return mid | ||
} | ||
switch { | ||
case target < nums[0] && nums[0] <= nums[mid], | ||
nums[0] <= nums[mid] && nums[mid] < target, | ||
nums[mid] < target && target <= nums[len(nums)-1]: | ||
left = mid + 1 | ||
default: | ||
right = mid - 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
#+END_SRC |
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,16 @@ | ||
#+TITLE: 文件说明。 | ||
* O(N) | ||
The solution with O(N) time complexity and that with O(logN) cost almost the same time. | ||
|
||
It seems that time used in leetcode does not have reference value. | ||
|
||
* complex | ||
main.py | ||
情况太多。复杂到自己都搞不懂,放弃分析。 | ||
也可能是因为自己笨,没有化繁为简的能力。 | ||
|
||
* 标准解法 | ||
normal.go | ||
|
||
* others | ||
其他解法 |