力扣——题解

题目:

1、螺旋矩阵
2、移除链表元素

##代码实现

#螺旋矩阵
class Solution(object):
    def generateMatrix(self, n):

        res = [[0] * n for i in range(n)]

        a = 1

        # 初始化左、右、上、下边界。
        left, right, up, down = 0, n - 1, 0, n - 1

        while a <= n * n:
            # 从左到右
            for i in range(left, right + 1):
                res[up][i] = a
                a += 1
            up += 1
            # 从上到下
            for i in range(up, down + 1):
                res[i][right] = a
                a += 1
            right -= 1
            # 从右到左
            for i in range(right, left - 1, -1):
                res[down][i] = a
                a += 1
            down -= 1
            # 从下到上
            for i in range(down, up - 1, -1):
                res[i][left] = a
                a += 1
            left += 1

        return res
#移除链表元素
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        head = f = ListNode(0, head)
        while f and f.next:
            while f and f.next and f.next.val == val:
                f.next = f.next.next
            f = f.next
        return head.next

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值