Skip to content

Commit 0fc5bd3

Browse files
committed
11.12
1 parent 1f3ab90 commit 0fc5bd3

File tree

7 files changed

+263
-0
lines changed

7 files changed

+263
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# @lc app=leetcode.cn id=215 lang=python
3+
#
4+
# [215] 数组中的第K个最大元素
5+
#
6+
# https://leetcode-cn.com/problems/kth-largest-element-in-an-array/description/
7+
#
8+
# algorithms
9+
# Medium (58.80%)
10+
# Likes: 283
11+
# Dislikes: 0
12+
# Total Accepted: 54.1K
13+
# Total Submissions: 91.8K
14+
# Testcase Example: '[3,2,1,5,6,4]\n2'
15+
#
16+
# 在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
17+
#
18+
# 示例 1:
19+
#
20+
# 输入: [3,2,1,5,6,4] 和 k = 2
21+
# 输出: 5
22+
#
23+
#
24+
# 示例 2:
25+
#
26+
# 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
27+
# 输出: 4
28+
#
29+
# 说明:
30+
#
31+
# 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。
32+
#
33+
#
34+
35+
# @lc code=start
36+
class Solution(object):
37+
def findKthLargest(self, nums, k):
38+
"""
39+
:type nums: List[int]
40+
:type k: int
41+
:rtype: int
42+
"""
43+
l,r,k=0,len(nums)-1,k-1
44+
while 1:
45+
index=self.partition(nums,l,r)
46+
if index==k:
47+
return nums[index]
48+
elif index>k:
49+
r=index-1
50+
else:
51+
l=index+1
52+
53+
def partition(self,nums,l,r):
54+
mid=nums[l]
55+
while l<r:
56+
while l<r and nums[r]<=mid:
57+
r-=1
58+
nums[l]=nums[r]
59+
while l<r and nums[l]>mid:
60+
l+=1
61+
nums[r]=nums[l]
62+
nums[l]=mid
63+
return l
64+
65+
66+
# @lc code=end
67+

414.第三大的数.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# @lc app=leetcode.cn id=414 lang=python
3+
#
4+
# [414] 第三大的数
5+
#
6+
# https://leetcode-cn.com/problems/third-maximum-number/description/
7+
#
8+
# algorithms
9+
# Easy (32.97%)
10+
# Likes: 74
11+
# Dislikes: 0
12+
# Total Accepted: 13.4K
13+
# Total Submissions: 40.6K
14+
# Testcase Example: '[3,2,1]'
15+
#
16+
# 给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
17+
#
18+
# 示例 1:
19+
#
20+
#
21+
# 输入: [3, 2, 1]
22+
#
23+
# 输出: 1
24+
#
25+
# 解释: 第三大的数是 1.
26+
#
27+
#
28+
# 示例 2:
29+
#
30+
#
31+
# 输入: [1, 2]
32+
#
33+
# 输出: 2
34+
#
35+
# 解释: 第三大的数不存在, 所以返回最大的数 2 .
36+
#
37+
#
38+
# 示例 3:
39+
#
40+
#
41+
# 输入: [2, 2, 3, 1]
42+
#
43+
# 输出: 1
44+
#
45+
# 解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。
46+
# 存在两个值为2的数,它们都排第二。
47+
#
48+
#
49+
#
50+
51+
# @lc code=start
52+
class Solution(object):
53+
def thirdMax(self, nums):
54+
"""
55+
:type nums: List[int]
56+
:rtype: int
57+
"""
58+
first,second,third=-float('inf'),-float('inf'),-float('inf')
59+
for i in nums:
60+
if i>first:
61+
first,second,third=i,first,second
62+
elif second<i<first:
63+
second,third=i,second
64+
elif third<i<second:
65+
third=i
66+
return third if third!=-float('inf') else first
67+
68+
# @lc code=end
69+

69.x-的平方根.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# @lc app=leetcode.cn id=69 lang=python
3+
#
4+
# [69] x 的平方根
5+
#
6+
# https://leetcode-cn.com/problems/sqrtx/description/
7+
#
8+
# algorithms
9+
# Easy (37.11%)
10+
# Likes: 242
11+
# Dislikes: 0
12+
# Total Accepted: 67K
13+
# Total Submissions: 180.3K
14+
# Testcase Example: '4'
15+
#
16+
# 实现 int sqrt(int x) 函数。
17+
#
18+
# 计算并返回 x 的平方根,其中 x 是非负整数。
19+
#
20+
# 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
21+
#
22+
# 示例 1:
23+
#
24+
# 输入: 4
25+
# 输出: 2
26+
#
27+
#
28+
# 示例 2:
29+
#
30+
# 输入: 8
31+
# 输出: 2
32+
# 说明: 8 的平方根是 2.82842...,
33+
# 由于返回类型是整数,小数部分将被舍去。
34+
#
35+
#
36+
#
37+
38+
# @lc code=start
39+
class Solution(object):
40+
def mySqrt(self, x):
41+
"""
42+
:type x: int
43+
:rtype: int
44+
"""
45+
if x==1:return 1
46+
l,r=0,x
47+
while l<=r:
48+
mid=int(l+(r-l)/2)
49+
if mid*mid<=x<(mid+1)*(mid+1):
50+
return mid
51+
elif mid*mid>x:
52+
r=mid
53+
else:
54+
l=mid
55+
56+
# @lc code=end
57+

旋转数组最小值.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def minNumberInRotateArray(self, rotateArray):
4+
# write code here
5+
if not rotateArray:return 0
6+
l,r=0,len(rotateArray)
7+
while l+1<r:
8+
mid=(l+r)//2
9+
if rotateArray[mid]>=rotateArray[l]:
10+
l=mid
11+
else:
12+
r=mid
13+
14+
return rotateArray[r]
15+
16+

算法导论/p17归并排序.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def merge(A,p,q,r):
2+
n1=q-p+1
3+
n2=r-q
4+
L=[0]*(n1+1)
5+
R=[0]*(n2+1)
6+
for i in range(n1):
7+
L[i]=A[p+i]
8+
for j in range(n2):
9+
R[j]=A[q+j+1]
10+
L[n1]=float('inf')
11+
R[n2]=float('inf')
12+
i,j=0,0
13+
for k in range(p,r+1):
14+
if L[i]<=R[j]:
15+
A[k]=L[i]
16+
i+=1
17+
else:
18+
A[k]=R[j]
19+
j+=1
20+
21+
def merge_sort(A,p,r):
22+
if p<r:
23+
q=(p+r)//2
24+
merge_sort(A,p,q)
25+
merge_sort(A,q+1,r)
26+
merge(A,p,q,r)
27+
28+
A=[1,3,5,7,2,4,6,8]
29+
merge_sort(A,0,7)
30+
print(A)
31+
32+
a='dsds'
33+
34+

算法导论/快速排序.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def quick_sort(alist, first, last): #原地排序
2+
if first >= last: #当只有一个数的时候不用排序
3+
return
4+
mid = alist[first] #最左边的数设为分界点
5+
low = first
6+
high = last #两个移动的指针
7+
while low < high:
8+
while low < high and alist[high] >= mid:
9+
high -= 1
10+
alist[low] = alist[high] #先右边再左边,而且由于alist[low]已经存起来了所以不用担心覆盖掉
11+
while low < high and alist[low] < mid:
12+
low += 1
13+
alist[high] = alist[low]
14+
alist[low] = mid #当low==high后, 记得将分界点 也就是第一个数 移动到中间来
15+
quick_sort(alist, first, low-1) #mid位置已经确定了,只需要管两边
16+
quick_sort(alist, low+1, last)
17+
18+
numbers = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
19+
quick_sort(numbers,0,len(numbers)-1)
20+
assert numbers == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

0 commit comments

Comments
 (0)