首页 > 其他分享 >Study Plan For Algorithms - Part30

Study Plan For Algorithms - Part30

时间:2024-09-15 11:47:12浏览次数:1  
标签:排列 matrix int Study num Algorithms Part30 self def

1. 螺旋矩阵 II
给定一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        matrix = [[0] * n for _ in range(n)]
        num = 1
        left, right, top, bottom = 0, n - 1, 0, n - 1

        while num <= n * n:
            for i in range(left, right + 1):
                matrix[top][i] = num
                num += 1
            top += 1

            for i in range(top, bottom + 1):
                matrix[i][right] = num
                num += 1
            right -= 1

            if top <= bottom:
                for i in range(right, left - 1, -1):
                    matrix[bottom][i] = num
                    num += 1
                bottom -= 1

            if left <= right:
                for i in range(bottom, top - 1, -1):
                    matrix[i][left] = num
                    num += 1
                left += 1

        return matrix

2. 排列序列
给出集合 [1,2,3,...,n],其所有元素共有 n! 种排列。
按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

  • "123"
  • "132"
  • "213"
  • "231"
  • "312"
  • "321"

给定 n 和 k,返回第 k 个排列。

import math

class Solution:
    def getPermutation(self, n: int, k: int) -> str:
        def factorial(n):
            if n <= 1:
                return 1
            return n * factorial(n - 1)

        nums = list(range(1, n + 1))

        result = []
        k -= 1  

        for i in range(n - 1, -1, -1):
            fact = factorial(i)
            index = k // fact
            result.append(str(nums.pop(index)))
            k %= fact

        return ''.join(result)

标签:排列,matrix,int,Study,num,Algorithms,Part30,self,def
From: https://www.cnblogs.com/stephenxiong001/p/18415106

相关文章

  • Study Plan For Algorithms - Part31
    1.旋转链表给定一个链表的头节点head,旋转链表,将链表每个节点向右移动k个位置。classListNode:def__init__(self,val=0,next=None):self.val=valself.next=nextclassSolution:defrotateRight(self,head:Optional[ListNode],k:int)->O......
  • phpStudy下载和使用方法
    phpStudy是一个PHP调试环境的程序集成包,它集成了Apache、PHP、MySQL、phpMyAdmin等多种开发工具,为PHP开发者提供了一个快速搭建和调试PHP开发环境的解决方案。phpStudy是一款集成开发环境(IDE)软件,主要用于在Windows系统上快速搭建和调试PHP、MySQL、Apache等网页开发环境。以下是......
  • kuangStudy
    JVM探究对JVM的理解?Java8虚拟机和之前的变化什么是OOM,什么是栈溢出,怎么分析JVM的常用调优参数内存快照如何抓取,怎么分析Dump文件JVM中的类加载ProcessOn思维导图工具1.JVM的位置操作系统之上,与其他应用程序平行的地位2.JVM的体系结构详细图解网上找classfile......
  • Study Plan For Algorithms - Part28
    1.跳跃游戏题目链接:https://leetcode.cn/problems/jump-game/给定一个非负整数数组nums,你最初位于数组的第一个下标。数组中的每个元素代表你在该位置可以跳跃的最大长度。判断你是否能够到达最后一个下标,如果可以,返回true;否则,返回false。classSolution:defca......
  • Study Plan For Algorithms - Part29
    1.在排序数组中查找数字统计一个数字在排序数组中出现的次数。方法一:defsearch(nums,target):returnhelper(nums,target)-helper(nums,target-1)defhelper(nums,target):i=0j=len(nums)-1whilei<=j:m=(i+j)//......
  • COMP2230/COMP6230 Algorithms
    TheUniversityofNewcastle,AustraliaSchoolofInformationandPhysicalSciencesCOMP2230/COMP6230AlgorithmsAssignment1Marks100Weight15%IndividualSubmissionviaCanvas1LearningOutcomesThisassignmentwillrequirestudentsto:Applyspecific......
  • COMP3506/7505  Algorithms and Data Structures
    Assignment Two – 25%Algorithms and Data Structures – COMP3506/7505 – Semester 2, 2024Due: 3pm on Friday October 18th (week 12)SummaryThe main objective ofthis assignment is to extend your knowledge from assignment one ......
  • comp10002 Foundations of Algorithms
    SchoolofComputing andInformationSystemscomp10002 Foundations of AlgorithmsSemester 2,2024Assignment 1LearningOutcomesIn this assignment you will demonstrate your understanding of arrays,  strings, functions, and the typedeffac......
  • Study Plan For Algorithms - Part26
    1.礼物的最大价值在一个m*n的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?方法一:de......
  • Study Plan For Algorithms - Part27
    1.最长不含重复字符的子字符串请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。方法一:deflengthOfLongestSubstring(s):n=len(s)max_len=0start=0char_index={}forendinrange(n):ifs[en......