Skip to content

Commit

Permalink
更新二分查找库
Browse files Browse the repository at this point in the history
  • Loading branch information
kengerlwl committed Apr 9, 2021
1 parent aa78de2 commit 019b5b0
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 5 deletions.
15 changes: 10 additions & 5 deletions 排序和查找/有序二分查找/有序二分查找.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ def __init__(self):


# 查找k在有序数组nums 中得位置。 nums是升序得
#return index, flag flag是代表是否有和k匹配得数得bool。
#return index, flag flag是代表是否有和k匹配得数得bool。这里没有考虑数组中有很多相同的元素
def search(self, nums, k):
l = 0
r = len(nums)-1

while l<r:
while l<=r:



mid = (l + r) // 2

if nums[mid]> k: # 向左边找
Expand All @@ -23,8 +26,10 @@ def search(self, nums, k):

if nums[l] == k:
return l, True
else:
elif nums[l] >k: # 更小就插入当前位置前面
return l, False
elif nums[l] < k:# 向右边插入这个位置
return l+1, False



Expand All @@ -33,8 +38,8 @@ def search(self, nums, k):
d = DichotomousSearch()

print(d.search(
[9, 16],
5
[1,9, 9,9,9,11,11,11,16],
9

)

Expand Down
83 changes: 83 additions & 0 deletions 排序和查找/有序二分查找/有重复二分查找.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import math

class DichotomousSearchRight(): # 向右边找
def __init__(self):
pass

# 查找k在有序数组nums 中得位置。 nums是升序得
#return index, flag flag是代表是否有和k匹配得数得bool。这里没有考虑数组中有很多相同的元素
def search(self, nums, k):
l = 0
r = len(nums)-1# 取右边

while l<r:
mid =math.ceil((l + r) / 2)

if nums[mid]> k: # 向左边找
r = mid - 1
elif nums[mid] <k: # 向右边找
l = mid +1
elif nums[mid] == k:
l = mid



if nums[l] == k:
return l, True
elif nums[l] >k:
return l, False
elif nums[l] < k:
return l+1, False


class DichotomousSearchLeft(): # 向右边找
def __init__(self):
pass


# 查找k在有序数组nums 中得位置。 nums是升序得
#return index, flag flag是代表是否有和k匹配得数得bool。这里没有考虑数组中有很多相同的元素
def search(self, nums, k):
l = 0
r = len(nums)-1# 取右边

while l<r:
# print(l, r)

mid =math.floor((l + r) / 2)

if nums[mid]> k: # 向左边找
r = mid - 1
elif nums[mid] <k: # 向右边找
l = mid +1
elif nums[mid] == k:
r = mid

if nums[l] == k:
return l, True
elif nums[l] >k:
return l, False
elif nums[l] < k:
return l+1, False



d = DichotomousSearchRight()

print('向右边查找',d.search(
[1,9, 9,9,9,11,11,11,16],
9
)
)



d = DichotomousSearchLeft()

print('向左边查找',d.search(
[1,9, 9,9,9,11,11,11,16],
8

)

)
72 changes: 72 additions & 0 deletions 练手/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

Mod = 10**9+7

class DichotomousSearch():
def __init__(self):
pass


# 查找k在有序数组nums 中得位置。 nums是升序得
#return index, flag flag是代表是否有和k匹配得数得bool。 这里
def search(self, nums, k,r):
l = 0
r = r

while l<r:
mid = (l + r) // 2

if nums[mid]> k: # 向左边找
r = mid - 1
elif nums[mid] <k: # 向右边找
l = mid +1
elif nums[mid] == k:
l = mid +1


if nums[l] == k:
return l, True
else:
return l, False
d= DichotomousSearch()

def find(index,nums, tar):
# print(tar)
n, f = d.search(nums, tar,r=index)
# print(n, f)
if f:
while nums[n] <= tar and n <= index:
n+=1
if n> index:
n = index+1

return n
else:
while nums[n] <= tar and n <= index:
n+=1
if n> index:
n = index+1
return n




class Solution(object):
def purchasePlans(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums = sorted(nums)
length = len(nums)
ans = 0
for i in range(1, length):
index =find(i-1, nums, target- nums[i])
# print(index)
ans+= index

print(ans)
return ans % Mod


Solution.purchasePlans(None,nums = [1,1,1,1,1,1], target = 6)
21 changes: 21 additions & 0 deletions 练手/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def orchestraLayout(self, num: int, xPos: int, yPos: int) -> int:
layer = min([xPos, num - xPos - 1, yPos, num - yPos - 1]) # 位于第几圈
v = (num*layer*4 - layer*layer*4) % 9 # 前几圈有多少个元素

start, end = layer, num - layer

if xPos == start:
return (v + yPos - start) % 9 + 1

if yPos == end - 1:
return (v + end - start - 1 + xPos - start) % 9 + 1

if xPos == end - 1:
return (v + (end - start)*2 - 2 + end - yPos - 1) % 9 + 1

if yPos == start:
return (v + (end - start)*3 - 3 + end - xPos - 1) % 9 + 1

return 0

Empty file added 练手/3.py
Empty file.

0 comments on commit 019b5b0

Please sign in to comment.