我是靠谱客的博主 舒服毛豆,最近开发中收集的这篇文章主要介绍leetcode 《简单》 设计问题 Python实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

leetcode 《简单》 设计问题 Python实现

'''
Shuffle an Array
打乱一个没有重复元素的数组。

示例:
// 以数字集合 1, 2 和 3 初始化数组。
int[] nums = {1,2,3};
Solution solution = new Solution(nums);

// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
solution.shuffle();

// 重设数组到它的初始状态[1,2,3]。
solution.reset();

// 随机返回数组[1,2,3]打乱后的结果。
solution.shuffle();
Email: yefeng DOT zheng AT gmail DOT com
yefeng.zheng@gmail.com
http://legacydirs.umiacs.umd.edu/~zhengyf/
'''
#定义两个函数,shuffle函数能把数组随机打乱,reset函数能返回初始数组。
#方法1:通过
#思路:调用洗牌函数shuffle
class Solution(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.origin = nums

    def reset(self):
        """
        Resets the array to its original configuration and return it.
        :rtype: List[int]
        """
        return self.origin 

    def shuffle(self):
        """
        Returns a random shuffling of the array.
        :rtype: List[int]
        
        import random 
        random_num = random.randint(len(nums))
        for i in range(len(nums)):
            self.output = nums[]
        """
        import random 
        new_nums = self.origin[:]
        random.shuffle(new_nums)
        return new_nums
        


# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
#方法2:通过
#思路:产生0-n之间的随机数,然后对调i,j的值
class Solution(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.origin = nums[:]
        self.output = nums 

    def reset(self):
        """
        Resets the array to its original configuration and return it.
        :rtype: List[int]
        """
        return self.origin 

    def shuffle(self):
        """
        Returns a random shuffling of the array.
        :rtype: List[int]
        """
        import random 
        len_nums = len(self.output)
        for i in range(len_nums):
            random_num = random.randint(i, len_nums-1) #保证不会取比i小的数字,从而self.output不会重复

            self.output[i], self.output[random_num] = self.output[random_num], self.output[i]
        return self.output

'''
最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
•push(x) -- 将元素 x 推入栈中。
•pop() -- 删除栈顶的元素。
•top() -- 获取栈顶元素。
•getMin() -- 检索栈中的最小元素。

示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.
'''

#方法1:
#思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列。
class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []
        self.minstack = [] #放置碰到的所有最小的push值
        

    def push(self, x):
        """
        :type x: int
        :rtype: void
        """

        self.stack.append(x)
        if len(self.minstack) == 0 or self.minstack[-1] >= x: #如果最小栈列表为空或者最后一个数字大于等于新输入的值x,就把x加入最小栈序列
           self.minstack.append(x)

    def pop(self):
        """
        :rtype: void
        """
        if len(self.stack) == 0:
            return 
        pop_stack = self.stack.pop()
        if pop_stack == self.minstack[-1]:
            self.minstack.pop()
        

    def top(self):
        """
        :rtype: int
        """
        return self.stack[-1]
        

    def getMin(self):
        """
        :rtype: int
        """
        return self.minstack[-1]


# 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()


# 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()
方法2
class MinStack:
    # @param x, an integer
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    # @return an integer
    def push(self, x):
        self.stack1.append(x)
        if len(self.stack2) == 0 or x <= self.stack2[-1]:
            self.stack2.append(x)

    # @return nothing
    def pop(self):
        top = self.stack1[-1]
        self.stack1.pop()
        if top == self.stack2[-1]:
            self.stack2.pop()
        
    # @return an integer
    def top(self):
        return self.stack1[-1]

    # @return an integer
    def getMin(self):
        return self.stack2[-1]


方法3
class MinStack(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack = []
        self.min = None

    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        self.stack.append(x)
        if self.min == None or self.min > x:
            self.min = x

    def pop(self):
        """
        :rtype: void
        """

        popItem = self.stack.pop()
        if len(self.stack) == 0:
            self.min = None
            return popItem

        if popItem == self.min:
            self.min = self.stack[0]
            for i in self.stack:
                if i < self.min:
                    self.min = i
        return popItem

    def top(self):
        """
        :rtype: int
        """
        return self.stack[-1]

    def getMin(self):
        """
        :rtype: int
        """
        return self.min


方法4
class MinStack(object):

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.l = []


    def push(self, x):
        """
        :type x: int
        :rtype: void
        """
        if x is None:
            pass
        else:
            self.l.append(x)


    def pop(self):
        """
        :rtype: void
        """
        if self.l is None:
            return 'error'
        else:
            self.l.pop(-1)


    def top(self):
        """
        :rtype: int
        """
        if self.l is None:
            return 'error'
        else:
            return self.l[-1]


    def getMin(self):
        """
        :rtype: int
        """
        if self.l is None:
            return 'error'
        else:
            return min(self.l)

方法5
class MinStack:
    def __init__(self):
        self.stack = []
        self.minStack = []
    # @param x, an integer
    # @return an integer
    def push(self, x):
        self.stack.append(x)
        if len(self.minStack) == 0 or self.minStack[-1] >= x:
            #print 'minn change'
            self.minStack.append(x)
 
    # @return nothing
    def pop(self):
        p = self.stack.pop()
        #print 'pop ' , p
        if p == self.minStack[-1]:
            #print 'minn pop'
            self.minStack.pop()

    # @return an integer
    def top(self):
        return self.stack[-1]

    # @return an integer
    def getMin(self):
        return self.minStack[-1]

最后

以上就是舒服毛豆为你收集整理的leetcode 《简单》 设计问题 Python实现的全部内容,希望文章能够帮你解决leetcode 《简单》 设计问题 Python实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部