Skip to content

Commit 19ce22a

Browse files
author
bjweimengshu
committed
面试题
1 parent 3097b2d commit 19ce22a

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
7+
def is_cycle(head):
8+
p1 = head
9+
p2 = head
10+
while p2 is not None and p2.next is not None:
11+
p1 = p1.next
12+
p2 = p2.next.next
13+
if p1 == p2:
14+
return True
15+
return False
16+
17+
18+
node1 = Node(5)
19+
node2 = Node(3)
20+
node3 = Node(7)
21+
node4 = Node(2)
22+
node5 = Node(6)
23+
node1.next = node2
24+
node2.next = node3
25+
node3.next = node4
26+
node4.next = node5
27+
node5.next = node2
28+
print(is_cycle(node1))
29+

src/chapter5/part3/MinStack.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class MinStack:
2+
def __init__(self):
3+
self.mainStack = []
4+
self.minStack = []
5+
6+
def push(self, element):
7+
self.mainStack.append(element)
8+
# 如果辅助栈为空,或新元素小于等于辅助栈栈顶,则新元素压入辅助栈
9+
if len(self.minStack) == 0 or element <= self.minStack[len(self.minStack)-1]:
10+
self.minStack.append(element)
11+
12+
def pop(self):
13+
# 如果出栈元素和辅助栈栈顶元素值相等,辅助栈出栈
14+
if self.mainStack[len(self.mainStack) - 1] == self.minStack[len(self.minStack) - 1]:
15+
self.minStack.pop()
16+
return self.mainStack.pop()
17+
18+
def get_min(self):
19+
if len(self.mainStack) == 0:
20+
return None
21+
return self.minStack[len(self.minStack) - 1]
22+
23+
24+
myStack = MinStack()
25+
myStack.push(4)
26+
myStack.push(9)
27+
myStack.push(7)
28+
myStack.push(3)
29+
myStack.push(8)
30+
myStack.push(5)
31+
print(myStack.get_min())
32+
myStack.pop()
33+
myStack.pop()
34+
myStack.pop()
35+
print(myStack.get_min())
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def get_greatest_common_divisor(a, b):
2+
big = max(a, b)
3+
small = min(a, b)
4+
if big % small == 0:
5+
return small
6+
for i in range(small//2, 1, -1):
7+
if small % i == 0 and big % i == 0:
8+
return i
9+
return 1
10+
11+
12+
def get_greatest_common_divisor_v2(a, b):
13+
big = max(a, b)
14+
small = min(a, b)
15+
if big % small == 0:
16+
return small
17+
return get_greatest_common_divisor_v2(big % small, small)
18+
19+
20+
def get_greatest_common_divisor_v3(a, b):
21+
if a == b:
22+
return a
23+
big = max(a, b)
24+
small = min(a, b)
25+
return get_greatest_common_divisor_v2(big-small, small)
26+
27+
28+
def gcb(a, b):
29+
if a == b:
30+
return a
31+
if (a & 1) == 0 and (b & 1) == 0:
32+
return gcb(a >> 1, b >> 1) << 1
33+
elif (a & 1) == 0 and (b & 1) != 0:
34+
return gcb(a >> 1, b)
35+
elif (a & 1) != 0 and (b & 1) == 0:
36+
return gcb(a, b >> 1)
37+
else:
38+
big = max(a, b)
39+
small = min(a, b)
40+
return gcb(big-small, small)
41+
42+
43+
print(get_greatest_common_divisor(25, 5))
44+
print(get_greatest_common_divisor_v2(100, 75))
45+
print(get_greatest_common_divisor_v3(99, 55))
46+
print(gcb(100, 80))

src/chapter5/part5/PowerOf2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def is_power_of_2(num):
2+
temp = 1
3+
while temp <= num:
4+
if temp == num:
5+
return True
6+
temp = temp * 2
7+
return False
8+
9+
10+
def is_power_of_2_v2(num):
11+
temp = 1
12+
while temp <= num:
13+
if temp == num:
14+
return True
15+
temp = temp << 1
16+
return False
17+
18+
19+
def is_power_of_2_v3(num):
20+
return (num & num-1) == 0
21+
22+
23+
print(is_power_of_2_v3(19))
24+
print(is_power_of_2_v3(32))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Bucket:
2+
def __init__(self):
3+
self.min = None
4+
self.max = None
5+
6+
7+
def get_max_sorted_distance(array=[]):
8+
# 1.得到数列的最大值和最小值
9+
max_value = array[0]
10+
min_value = array[0]
11+
for i in range(1, len(array)):
12+
if array[i] > max_value:
13+
max_value = array[i]
14+
if array[i] < min_value:
15+
min_value = array[i]
16+
d = max_value - min_value
17+
# 如果max和min相等,说明数组所有元素都相等,返回0
18+
if d == 0:
19+
return 0
20+
# 2.初始化桶
21+
bucket_num = len(array)
22+
buckets = []
23+
for i in range(0, bucket_num):
24+
buckets.append(Bucket())
25+
# 3.遍历原始数组,确定每个桶的最大最小值
26+
for i in range(0, len(array)):
27+
# 确定数组元素所归属的桶下标
28+
index = int((array[i] - min_value) * (bucket_num-1) / d)
29+
if buckets[index].min is None or buckets[index].min > array[i]:
30+
buckets[index].min = array[i]
31+
if buckets[index].max is None or buckets[index].max < array[i]:
32+
buckets[index].max = array[i]
33+
# 4.遍历桶,找到最大差值
34+
left_max = buckets[0].max
35+
max_distance = 0
36+
for i in range(1, len(buckets)):
37+
if buckets[i].min is None:
38+
continue
39+
if buckets[i].min - left_max > max_distance:
40+
max_distance = buckets[i].min - left_max
41+
left_max = buckets[i].max
42+
return max_distance
43+
44+
45+
my_array = list([2, 6, 3, 4, 5, 10, 9])
46+
print(get_max_sorted_distance(my_array))

src/chapter5/part7/StackQueue.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class StackQueue:
2+
def __init__(self):
3+
self.stackA = []
4+
self.stackB = []
5+
6+
def en_queue(self, element):
7+
self.stackA.append(element)
8+
9+
def de_queue(self):
10+
if len(self.stackB) == 0:
11+
if len(self.stackA) == 0:
12+
return None
13+
self.transfer()
14+
return self.stackB.pop()
15+
16+
def transfer(self):
17+
while len(self.stackA) > 0:
18+
self.stackB.append(self.stackA.pop())
19+
20+
21+
stack_queue = StackQueue()
22+
stack_queue.en_queue(1)
23+
stack_queue.en_queue(2)
24+
stack_queue.en_queue(3)
25+
print(stack_queue.de_queue())
26+
print(stack_queue.de_queue())
27+
stack_queue.en_queue(4)
28+
print(stack_queue.de_queue())
29+
print(stack_queue.de_queue())
30+

0 commit comments

Comments
 (0)