python实现经典算法

1，快速排序

def quick_sort(arr,start=0,end=None):
if end is None:
end = len(arr)-1
if end<=start:
return(arr)
i,j = start,end
ref = arr[start]
while j>i:
if arr[j]>= ref:
j = j - 1
else:
# 此处使用一行语句交换3个元素的值
arr[i],arr[j],arr[i+1] = arr[j],arr[i+1],arr[i]
i = i + 1
quick_sort(arr,start=start,end = i-1)
quick_sort(arr,start=i+1,end = end)
return(arr)

print(quick_sort([1,1,3,3,2,2,6,6,6,5,5,7]))


[1, 1, 2, 2, 2, 3, 5, 5, 6, 6, 6, 7]


2，二分查找

nums = [1, 2, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 10, 11, 11, 11,
11, 12, 13, 13, 15, 16, 16, 20, 21, 21, 23, 24, 26,
26, 27, 28, 28, 31, 33, 33, 34, 35, 38, 38, 39, 40,
42, 43, 45, 45, 46, 46, 47, 47, 51, 52, 52, 53, 53,
55, 55, 56, 56, 57, 57, 57, 58, 59, 61, 62, 64, 66,
66, 67, 68, 69, 69, 71, 72, 72, 74, 74, 75, 76, 78,
78, 79, 79, 79, 79, 80, 82, 85, 88, 89, 90, 90, 91,
91, 91, 94, 99, 99]
def find(num,nums):
mid = len(nums) // 2
if nums[mid] > num:
nums = nums[0:mid]
print(nums)
find(num, nums)
elif nums[mid] < num:
nums = nums[mid+1:]
print(nums)
find(num, nums)
else:
print('找到了')
print(nums[mid])
return nums[mid]
find(40,nums)


• [1, 2, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 10, 11, 11, 11, 11, 12, 13, 13, 15, 16, 16, 20, 21, 21, 23, 24, 26, 26, 27, 28, 28, 31, 33, 33, 34, 35, 38, 38, 39, 40, 42, 43, 45, 45, 46, 46, 47]
[21, 23, 24, 26, 26, 27, 28, 28, 31, 33, 33, 34, 35, 38, 38, 39, 40, 42, 43, 45, 45, 46, 46, 47]
[38, 38, 39, 40, 42, 43, 45, 45, 46, 46, 47]
[38, 38, 39, 40, 42]
[40, 42]
[40]
找到了
40

3，爬楼梯

def climb_stairs(n):
if n==1:
return 1
if n==2:
return 2
a,b = 1,2
i = 3
while i<=n:
a,b = b,a+b
i +=1
return b

print(climb_stairs(10))  ## 也就是斐波那契数组


89


4，两数之和

def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
res = target - nums[i]
if res in nums and nums.index(res) != i:
return [i,nums.index(res)]


(0, 2)


5，最大回撤

def max_drawdown(arr):
assert len(arr)>2, "len(arr) should > 2!"
x,y = arr[0:2]
xmax = x
maxdiff = x-y

for i in range(2,len(arr)):
if arr[i-1] > xmax:
xmax = arr[i-1]
if xmax - arr[i] > maxdiff:
maxdiff = xmax - arr[i]
x,y = xmax,arr[i]

print("x=",x,",y=",y)
return(maxdiff)

print(max_drawdown([3,7,2,6,4,1,9,8,5]))


x= 7 ,y= 16


6，合并两个有序数组


def merge_sorted_array(a,b):
c = []
i,j = 0,0
while True:
if i==len(a):
c.extend(b[j:])
return(c)
elif j==len(b):
c.extend(a[i:])
return(c)
else:
if a[i]<b[j]:
c.append(a[i])
i=i+1
else:
c.append(b[j])
j=j+1

print(merge_sorted_array([1,2,6,8],[2,4,7,10]))


[1, 2, 2, 4, 6, 7, 8, 10]


7，最大连续子数组和


def max_sub_array(arr):
n = len(arr)
maxi,maxall = arr[0],arr[0]
for i in range(1,n):
maxi = max(arr[i],maxi + arr[i])
maxall = max(maxall,maxi)
return(maxall)

print(max_sub_array([1,5,-10,2,5,-3,2,6,-3,1]))


12


8，最长不重复子串

def longest_substr(s):
dic = {}
start,maxlen,substr = 0,0,""

for i,x in enumerate(s):
if x in dic:
start = max(dic[x]+1,start)
dic[x] = i
else:
dic[x] = i

if i-start+1>maxlen:
maxlen = i-start+1
substr = s[start:i+1]
return(substr)

print(longest_substr("abcbefgf"))
print(longest_substr("abcdef"))
print(longest_substr("abbcddefh"))


cbefgabcdefdefh


9，全排列

import numpy as np
def permutations(arr):
if len(arr)<=1:
return([arr])
t = [arr[0:1]]
i = 1
while i<=len(arr)-1:
a = arr[i]
t = [xs[0:j]+[a]+xs[j:] for xs in t for j in range(i+1)]
t = np.unique(t,axis=0).tolist()
i = i+1
return(t)
print(permutations([1,1,3]))


[[1, 1, 3], [1, 3, 1], [3, 1, 1]]


10，三数之和

def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res =[]
i = 0
for i in range(len(nums)):
if i == 0 or nums[i]>nums[i-1]: ## 这个一步很重要
l = i+1
r = len(nums)-1
while l < r:
s = nums[i] + nums[l] +nums[r]
if s ==0:
res.append([nums[i],nums[l],nums[r]])
l +=1
r -=1
while l < r and nums[l] == nums[l-1]: ## 优化
l += 1
while r > l and nums[r] == nums[r+1]: ## 优化
r -= 1
elif s>0:
r -=1
else :
l +=1
return res


[(-2, -1, 3), (-3, 1, 2)]

posted @ 2019-09-19 21:20  oxtime  阅读(7972)  评论(1编辑  收藏  举报