用来做区位分析的地图网站,集团网站品牌建设特点,建设银行招标网站,商城做网站哪家好目录 209. 长度最小的子数组1、题目描述2、思路3、code4、复杂度分析 LC59 螺旋矩阵 II1、题目描述2、思路3、code4、复杂度分析 209. 长度最小的子数组
题目链接#xff1a;209
1、题目描述 给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于… 目录 209. 长度最小的子数组1、题目描述2、思路3、code4、复杂度分析 LC59 螺旋矩阵 II1、题目描述2、思路3、code4、复杂度分析 209. 长度最小的子数组
题目链接209
1、题目描述 给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其总和大于等于 target 的长度最小的 子数组 [numsl, numsl1, …, numsr-1, numsr] 并返回其长度。如果不存在符合条件的子数组返回 0。 示例 1 输入target 7, nums [2,3,1,2,4,3] 输出2 解释子数组 [4,3] 是该条件下的长度最小的子数组。 2、思路
1️⃣ 暴力法两个for循环嵌套时间复杂度 O ( n 2 ) O(n^2) O(n2) 2️⃣ 题目基本是根据连续子序列的情况不断调节子序列的起始和终止位置滑动窗口 模板
3、code
class Solution:def minSubArrayLen(self, target: int, nums: List[int]) - int:# 找一个数组的满足条件的最短或者最长连续子数组滑动窗口minlen float(inf)start 0sum_sub 0for end in range(0,len(nums)):sum_sub nums[end]while sum_sub target:minlen min(minlen, end-start1)sum_sub - nums[start]start 1if minlen len(nums):return minlenelse:return 04、复杂度分析
时间复杂度 每个元素在滑动窗后进来操作一次出去操作一次每个元素都是被操作两次所以时间复杂度是 2 × n 也就是O(n)空间复杂度没有创建数组 O ( 1 ) O(1) O(1)
LC59 螺旋矩阵 II
题目链接59
1、题目描述 给你一个正整数 n 生成一个包含 1 到 n2 所有元素且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 示例 1 2、思路
要控制每次循环的区间范围循环不变量 按照左闭右开的原则来画一圈大家看一下
3、code
class Solution:def generateMatrix(self, n: int) - List[List[int]]:# 首先先初始化一个全是0的nxn二维矩阵mat [[0 for _ in range(n)] for _ in range(n)]# 定义每一圈的起始点坐标start_x 0start_y 0# 定义表示不同圈的每一条边的倒数第二个节点的偏移量# 比如第1圈是j (n-1) - 1# 第2圈就是j (n-1) - 2offset 1# 定义要往矩阵中填入的数count 1loop n //2# 循环开始for time in range(0,loop):# 填充上行从左到右:横坐标不变且是start_x纵坐标从start_y到(n - 1) - 1for j in range(start_y, n - offset):# 因为range“顾头不顾腚”所以可以少写一个-1mat[start_x][j] countcount 1# 填充右列从上到下横坐标从start_x到(n-1)-offset纵坐标不变且是上行最后一个元素的坐标加一(j n - offset) # 此时到达了上行的倒数第二个元素(start_x,j (n-1)-offset)# 那么右列的第一个元素就是(start_x,n - offset)for i in range(start_x, n - offset):mat[i][n - offset] countcount 1# 填充下行从右到左横坐标不变是上列最后一个元素的横坐标加一(i n - offset)纵坐标从上一次的j n - offset 一直减到start_y 1for j in range(n - offset, start_y, -1):mat[n - offset][j] countcount 1# 填充左列从下到上横坐标从上一次的i n - offset一只减到start_x 1纵坐标不变就是上一行最后一个元素的纵坐标减一start_yfor i in range(n - offset, start_x, -1):mat[i][start_y] countcount 1# 更新起始点坐标start_x 1start_y 1offset 1mid n // 2if n%2 ! 0:mat[mid][mid] count return mat 4、复杂度分析
1️⃣ 时间复杂度 n / 2 ∗ 4 ∗ ( n − k ) n 2 n/2 * 4 *(n-k) n^2 n/2∗4∗(n−k)n2 2️⃣ 空间复杂度1 文章转载自: http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.plwfx.cn.gov.cn.plwfx.cn http://www.morning.mumgou.com.gov.cn.mumgou.com http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.jkpnm.cn.gov.cn.jkpnm.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.mhbcy.cn.gov.cn.mhbcy.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.nmfml.cn.gov.cn.nmfml.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.lwdzt.cn.gov.cn.lwdzt.cn http://www.morning.tyjp.cn.gov.cn.tyjp.cn http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn