[leetcode] Python(8)--设计循环队列(622)、岛屿的个数(200)、打开转盘锁(752)、完全平方数(279)

从零开始的力扣(第八天)~

1.设计循环队列

设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。

循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。

你的实现应该支持如下操作:

MyCircularQueue(k): 构造器,设置队列长度为 k 。
Front: 从队首获取元素。如果队列为空,返回 -1 。
Rear: 获取队尾元素。如果队列为空,返回 -1 。
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
isEmpty(): 检查循环队列是否为空。
isFull(): 检查循环队列是否已满。

示例:

MyCircularQueue circularQueue = new MycircularQueue(3); // 设置长度为 3

circularQueue.enQueue(1); // 返回 true

circularQueue.enQueue(2); // 返回 true

circularQueue.enQueue(3); // 返回 true

circularQueue.enQueue(4); // 返回 false,队列已满

circularQueue.Rear(); // 返回 3

circularQueue.isFull(); // 返回 true

circularQueue.deQueue(); // 返回 true

circularQueue.enQueue(4); // 返回 true

circularQueue.Rear(); // 返回 4

提示:

所有的值都在 0 至 1000 的范围内;
操作数将在 1 至 1000 的范围内;
请不要使用内置的队列库。
—————————————————————————————————————————

先对满队列与空队列做出判断,这里我用list作为存储空间,并且将list长度设为k + 1,入队与出队就判定front与rear指针的位置就好
class MyCircularQueue(object):

    def __init__(self, k):
        """
        Initialize your data structure here. Set the size of the queue to be k.
        :type k: int
        """
        self.k = k
        self.list = [None] * (k + 1)
        self.rear = 0
        self.front = 0 

    
    def enQueue(self, value):
        """
        Insert an element into the circular queue. Return true if the operation is successful.
        :type value: int
        :rtype: bool
        """        
        if self.isFull():
            return False          
        self.list[self.rear] = value
        self.rear += 1  
        self.rear %= self.k + 1
        return True
    
    
    def deQueue(self):
        """
        Delete an element from the circular queue. Return true if the operation is successful.
        :rtype: bool
        """
        if self.isEmpty():        
            return False
        self.front += 1      
        self.front %= self.k + 1
        return True
        

    def Front(self):
        """
        Get the front item from the queue.
        :rtype: int
        """
        if self.isEmpty():
            return -1
        return self.list[self.front]
        

    def Rear(self):
        """
        Get the last item from the queue.
        :rtype: int
        """
        if self.isEmpty():
            return -1
        return self.list[(self.rear - 1) % (self.k + 1)]
        

    def isEmpty(self):
        """
        Checks whether the circular queue is empty or not.
        :rtype: bool
        """
        return self.rear == self.front

    
    def isFull(self):
        """
        Checks whether the circular queue is full or not.
        :rtype: bool
        """
        return (self.front - self.rear) % (self.k + 1) == 1


# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()

在这里插入图片描述
这里说几个技术要点:

  1. 队列遵循先入先出原则(FIFO),判断队列满与空的条件是当头指针在位置上比尾指针大1时,队列为满,而当头指针与尾指针指向一个位置时,队列为空;
  2. 在后面的负数除法取余数的过程里,商是要取向负无穷的方向,这里给一个例子: − 1 ÷ 5 = − 0.2 -1÷5=-0.2 1÷5=0.2,而当商向负无穷方向取为 − 1 -1 1,那么余数就是 − 1 − ( 5 × − 1 ) = 4 -1-(5×-1) = 4 1(5×1)=4

2.岛屿的个数

给定一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例 1:
输入:
11110
11010
11000
00000
输出: 1

示例 2:
输入:
11000
11000
00100
00011
输出: 3
—————————————————————————————————————————

通过深度优先遍历的思想,将这个grid想成图,从左上角开始遍历,将每一个"1"看作是一个结点,当它的右方或下方为"1"时,递归下去,并且在每一次发现"1"时,将此处变为"0"
class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        if not grid:
            return 0
        rows = len(grid)
        cols = len(grid[0])
        list = []
        def DFS(i,j):
            if i < 0 or i > rows - 1 or j < 0 or j > cols - 1 or grid[i][j] == "0":
                pass
            else:
                grid[i][j] = "0"
                DFS(i + 1,j)
                DFS(i,j + 1)
                DFS(i - 1,j)
                DFS(i,j - 1)
        for i in range(rows):
            for j in range(cols):
                if grid[i][j] == "1":
                    list.append(1)
                    DFS(i,j)
        return len(list)

在这里插入图片描述
深度优先搜索就是先在某一结点上找出他的所有子节点的情况,若没有找到,那么返回上一层,遍历并行节点的情况,这里有一个关于深度优先搜索和广度优先搜索的解释很好:

  1. 深度优先遍历:https://blog.csdn.net/raphealguo/article/details/7560918
  2. 广度优先遍历:https://blog.csdn.net/raphealguo/article/details/7523411
这里还有一个做法,和深度优先遍历想法一样,只不过使用map函数来对grid循环染色,将"1"染成"0"
class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        def sink(i, j):
            if 0 <= i < len(grid) and 0 <= j < len(grid[i]) and grid[i][j] == '1':
                grid[i][j] = '0'
                list(map(sink, (i+1, i-1, i, i), (j, j, j+1, j-1)))
                return 1
            return 0
        return sum(sink(i, j) for i in range(len(grid)) for j in range(len(grid[i])))

在这里插入图片描述

3.打开转盘锁

你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ 。每个拨轮可以自由旋转:例如把 ‘9’ 变为 ‘0’,‘0’ 变为 ‘9’ 。每次旋转都只能旋转一个拨轮的一位数字。

锁的初始数字为 ‘0000’ ,一个代表四个拨轮的数字的字符串。

列表 deadends 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。

字符串 target 代表可以解锁的数字,你需要给出最小的旋转次数,如果无论如何不能解锁,返回 -1。

示例 1:
输入:deadends = [“0201”,“0101”,“0102”,“1212”,“2002”], target = “0202”
输出:6
解释:
可能的移动序列为 “0000” -> “1000” -> “1100” -> “1200” -> “1201” -> “1202” -> “0202”。
注意 “0000” -> “0001” -> “0002” -> “0102” -> “0202” 这样的序列是不能解锁的,
因为当拨动到 “0102” 时这个锁就会被锁定。

示例 2:
输入: deadends = [“8888”], target = “0009”
输出:1
解释:
把最后一位反向旋转一次即可 “0000” -> “0009”。

示例 3:
输入: deadends = [“8887”,“8889”,“8878”,“8898”,“8788”,“8988”,“7888”,“9888”], target = “8888”
输出:-1
解释:
无法旋转到目标数字且不被锁定。

示例 4:
输入: deadends = [“0000”], target = “8888”
输出:-1

提示:

死亡列表 deadends 的长度范围为 [1, 500]。
目标数字 target 不会在 deadends 之中。
每个 deadends 和 target 中的字符串的数字会在 10,000 个可能的情况 ‘0000’ 到 ‘9999’ 中产生。
—————————————————————————————————————————

这道题使用广度优先遍历,具体操作是将每一步所能得到的结果枚举出来放入queue中,看哪一步中有target
class Solution(object):
    def openLock(self, deadends, target):
        """
        :type deadends: List[str]
        :type target: str
        :rtype: int
        """
        deadendset = set(deadends)#去除deadends重复值
        if "0000" in  deadendset:
            return -1
        que = []#构建队列实现BFS
        que.append("0000")#添加初值
        visited = set(['0000'])#存储已经用过的数值
        step = 0#步数
        while que:
            step += 1
            size = len(que)
            for i in range (size):
                point=que.pop(0)#左端取出
                for j in range(4):
                    for k in [-1,1]:
                        newpoint = [i for i in point]
                        newpoint[j] = str((int(newpoint[j]) + k) % 10)#实现某一位加一或减一
                        newpoint = "".join(newpoint)
                        if newpoint==target:
                            return step
                        if newpoint in deadendset or newpoint in visited:
                            continue
                        que.append(newpoint)#添加的队尾
                        visited.add(newpoint)#添加到已用过的值
        return -1

在这里插入图片描述
讲一下大致过程:

  1. 先把特殊情况过滤掉,并给deadends去重;
  2. 第一步先找初始值"0000",在初始值找寻第一步中所有可能的情况,例如step 1:“9000”、“1000”、“9900”、“1100”、“9990”、“1110”、“9999”、“1111”,如果第一步中没有target,那么会继续在第一步的基础上遍历所有延伸情况,也就是广度优先遍历。

4.完全平方数

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。

示例 1:
输入: n = 12
输出: 3
解释: 12 = 4 + 4 + 4.

示例 2:
输入: n = 13
输出: 2
解释: 13 = 4 + 9.
—————————————————————————————————————————

使用广度优先搜索,同样将每一步所等达到的数字与步数记录下来
class Solution(object):
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        que = [(0,0)]#构建队列
        visited = set()
        while que:
            val,step = que.pop(0)#取队首值
            for i in range(1,n+1):
                if val+i**2 == n:
                    return step + 1
                if val+i**2 > n:
                    break
                if val+i**2 not in visited:
                    que.append((val+i**2,step+1))
                    visited.add(val+i**2)
                else:
                    continue

在这里插入图片描述
给出当n为12时所有的数字与步数情况:(1, 1)、(4, 1)、(9, 1)、(2, 2)、(5, 2)、(10, 2)、(8, 2)、(3, 3)、(6, 3)、(11, 3)、(12, 3)、(7, 4)

通过动态搜索,将所范围n内的情况记录到字典内
class Solution(object):
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        f = {0: 0}
        x = 1
        for i in xrange(1, n + 1):
            if i == x ** 2:
                x += 1
                f[i] = 1
            else:
                f[i] = min([1 + f[i - j ** 2] for j in xrange(1, x)])
        return f[n]

与上一种方法的思路大致相同,对每个平方数置为1,对两个平方数之间的量在前项中找取最小值,动态存储变量。

不可思议的方法!
class Solution:
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        while n % 4 == 0: 
            n /= 4 
        if n % 8 == 7: 
            return 4 
        a = 0 
        while a**2 <= n: 
            b = int((n - a**2)**0.5) 
            if a**2 + b**2 == n: 
                return (not not a) + (not not b) 
            a += 1 
        return 3

在这里插入图片描述
前一段引用四平方定理: 任何一个正整数都可以表示成不超过四个整数的平方之和。 推论:满足四数平方和定理的数n(四个整数的情况),必定满足 n=4a(8b+7);下一段判断这个数是否可以由一或两个平方数组成;这样就剩下三了,简直神奇!

以上就是今日经验!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值