Skip to content

Commit f63b9ee

Browse files
committed
ana
1 parent 5e9257f commit f63b9ee

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

kthLargestinTwoSortedArray.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
K-th Element of Two Sorted Arrays
3+
Given two sorted arrays of size m and n respectively, you are tasked with finding the element that would be at the k’th position of the final sorted array
4+
5+
Input : Array 1 - 2 3 6 7 9
6+
Array 2 - 1 4 8 10
7+
k = 5
8+
Output : 6
9+
Explanation: The final sorted array would be -
10+
1, 2, 3, 4, 6, 7, 8, 9, 10
11+
The 5th element of this array is 6.
12+
Input : Array 1 - 100 112 256 349 770
13+
Array 2 - 72 86 113 119 265 445 892
14+
k = 7
15+
Output : 256
16+
Explanation: Final sorted array is -
17+
72, 86, 100, 112, 113, 119, 256, 265, 349, 445, 770, 892
18+
7th element of this array is 256.
19+
'''
20+
21+
22+
def kthLargest(a,b,k):
23+
c = []
24+
i = 0
25+
j = 0
26+
while i < len(a) or j < len(b):
27+
if a[i] < b[j]:
28+
c.append(b[j])
29+
j += 1
30+
else:
31+
c.append(a[i])
32+
i += 1
33+
34+
if i < len(a):
35+
c.append(a[i])
36+
i += 1
37+
if j < len(b):
38+
c.append(b[j])
39+
j += 1
40+
print(c)
41+
42+
43+
def findkth(a,b,k):
44+
45+
if not a:
46+
return b[k]
47+
if not b:
48+
return a[k]
49+
50+
la, lb = len(a)//2, len(b)//2
51+
ma, mb = a[la], a[lb]
52+
53+
if la + lb < k:
54+
#第k大的值在右边,下面判断要去掉哪一部分的值
55+
if ma > mb:
56+
#说明肯定不在b的前半部分中
57+
return findkth(a, b[lb:], k - ib)
58+
else:
59+
return findkth(a[:ia + 1], k - ia])
60+
else:
61+
if ma > mb:
62+
return findkth(a[:ia], b, k)
63+
else:
64+
return findkth(a. b[:ib], k)
65+
66+
67+
68+
if __name__ == '__main__':
69+
a = [2,3,6,7,9]
70+
b = [1,4,8,10]
71+
print(findkth(a,b,3))·

src/252.py

+7
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ def canAttendMeetings(self, intervals):
3232
我们只关心开始时间和结束时间,让它们排序,使用容器中元素的数量来进行判断;
3333
通过遍历时间,根据两个时间的差值来改变容器状态,通过状态来进行条件判断;
3434
35+
intervals time --> greedy
36+
37+
排序之后对我们的结果没有影响,因为可以转化为人进入房间的问题; 我们不需要知道
38+
每一个人进去出去的时间,我们只需要在某一个时刻有人进入,有人出去,通过他们的关系
39+
去判断。
40+
41+
3542
'''

src/367.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isPerfectSquare(self, num):
3+
"""
4+
:type num: int
5+
:rtype: bool
6+
"""
7+
if num < 2:
8+
return True
9+
10+
start = 1
11+
end = num
12+
13+
while start + 1 < end:
14+
mid = start + (end - start) // 2
15+
if mid * mid == num:
16+
return True
17+
elif mid * mid < num:
18+
start = mid
19+
else:
20+
end = mid
21+
return False

src/56.py

Whitespace-only changes.

src/69.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
vsclass Solution:
2+
def mySqrt(self, x):
3+
"""
4+
:type x: int
5+
:rtype: int
6+
"""
7+
if x < 2:
8+
return x
9+
10+
start = 1
11+
end = x
12+
while start + 1 < end:
13+
mid = start + (end - start) // 2
14+
if mid * mid == x:
15+
return mid
16+
elif mid * mid < x:
17+
start = mid
18+
else:
19+
end = mid
20+
21+
if start * start < x:
22+
return start
23+
24+
25+

src/82.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def deleteDuplicates(self, head: 'ListNode') -> 'ListNode':
9+
if not head:
10+
return None
11+
12+
preHead = ListNode(-1)
13+
preHead.next = head
14+
pre = preHead
15+
tmp = head
16+
while pre.next != None and pre.next.next != None:
17+
if pre.next.val == pre.next.next.val:
18+
sameNums = pre.next.val
19+
while pre.next != None and pre.next.val == sameNums:
20+
pre.next = pre.next.next
21+
else:
22+
pre = pre.next
23+
24+
return preHead.next
25+

0 commit comments

Comments
 (0)