【leecode刷题】初级算法-设计问题

tech2025-01-20  1

1、打乱数组

class Solution: def __init__(self, nums): self.array = nums self.original = list(nums) def reset(self): self.array = self.original self.original = list(self.original) return self.array def shuffle(self): for i in range(len(self.array)): swap_idx = random.randrange(i, len(self.array)) self.array[i], self.array[swap_idx] = self.array[swap_idx], self.array[i] return self.array # Your Solution object will be instantiated and called as such: # obj = Solution(nums) # param_1 = obj.reset() # param_2 = obj.shuffle()

2、打乱栈

class MinStack(object): def __init__(self): """ initialize your data structure here. """ self.items = [] def push(self, x): """ :type x: int :rtype: None """ self.items.append(x) return None def pop(self): """ :rtype: None """ self.items.pop() return None def top(self): """ :rtype: int """ return self.items[len(self.items)-1] def getMin(self): """ :rtype: int """ return min(self.items) # Your MinStack object will be instantiated and called as such: # obj = MinStack() # obj.push(x) # obj.pop() # param_3 = obj.top() # param_4 = obj.getMin()
最新回复(0)