当前位置: 首页 > news >正文

山东济南网站建设seo文章优化技巧

山东济南网站建设,seo文章优化技巧,网站编辑是个长期做的工作吗,建立公司官网多少钱刷题的第二天,希望自己能够不断坚持下去,迎来蜕变。😀😀😀 刷题语言:C / Python Day2 任务 977.有序数组的平方 209.长度最小的子数组 59.螺旋矩阵 II 1 有序数组的平方(重点:双指针…

刷题的第二天,希望自己能够不断坚持下去,迎来蜕变。😀😀😀
刷题语言:C++ / Python
Day2 任务
977.有序数组的平方
209.长度最小的子数组
59.螺旋矩阵 II

1 有序数组的平方(重点:双指针思想

在这里插入图片描述

1.1 暴力

思路:将数组里面所有元素平方,再排序。
时间复杂度: O ( n + n l o g n ) O(n + nlogn) O(n+nlogn)
C++:

class Solution {
public:vector<int> sortedSquares(vector<int>& nums) {for (int i = 0; i < nums.size(); i++){nums[i] *= nums[i];}sort(nums.begin(), nums.end());return nums;}
};

Python:

class Solution(object):def sortedSquares(self, nums):for i in range(len(nums)):nums[i] *= nums[i]nums.sort()return nums
1.2 双指针(非常重要的思想)

在这里插入图片描述
非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序
数组里面的元素平方之后,元素趋势如下图所示
在这里插入图片描述
数组平方的最大值就在数组的两端,考虑双指针,i指向起始位置,j指向终止位置
伪代码:

vector<int> result;
k = nums.size - 1;
for(i = 0, j = nums.size-1;i<=j;) # i <= j,因为最后要处理两个元素if(nums[i]*nums[i]>nums[j]*nums[j])result[k--] = nums[i]*nums[i]i++elseresult[k--] = nums[j]*nums[j]j--
return result

时间复杂度: O ( n ) O(n) O(n)
C++:

class Solution {
public:vector<int> sortedSquares(vector<int>& nums) {int k = nums.size() - 1;vector<int> result(nums.size(), 0);for (int i = 0, j = nums.size() - 1; i <= j; ){if (nums[i] * nums[i] > nums[j] * nums[j]){result[k--] = nums[i] * nums[i];i++;}else{result[k--] = nums[j] * nums[j];j--;}}return result;}
};

Python:

class Solution(object):def sortedSquares(self, nums):i,j,k = 0, len(nums) - 1, len(nums) - 1res = [float('inf')] * len(nums)while i <= j:if nums[i] ** 2 > nums[j] ** 2:res[k] = nums[i] ** 2i += 1else:res[k] = nums[j] ** 2j -= 1k -= 1return res

2 长度最小的子数组 - (滑动窗口

在这里插入图片描述

2.1 暴力解法

两个for循环,不断寻找符合条件的子序列
更新起始位置,终止位置每次都是一直往后遍历
时间复杂度: O ( n 2 ) O(n^2) O(n2)
C++:

class Solution {
public:int minSubArrayLen(int target, vector<int>& nums) {int res = INT32_MAX;// 2147483647int sum = 0;// 子序列数值之和int subLength = 0;// 子序列的长度for (int i = 0; i < nums.size(); i++) // 起点i{sum = 0;for (int j = i; j < nums.size(); j++) // 终止位置j{sum += nums[j];if (sum >= target) // 子序列和超过了s,更新result{subLength = j - i + 1; // 子序列的长度res = res < subLength ? res : subLength;break;}}}return res == INT32_MAX ? 0 : res;}
};
2.2 滑动窗口解法

在这里插入图片描述

时间复杂度 O ( n ) O(n) O(n)
滑动窗口:不断的调节子序列的起始位置和终止位置,得出想要的结果
用一个for循环来做2个for循环所做的事情

索引下标j表示的是滑动窗口里面的终止位置
假设是起始位置,for循环一次一次往后移动,这个终止位置要把后面所有的元素都遍历一遍,这种就和暴力解法没有区别。
因此,这个for循环里面的j一定指向的是终止位置,而起始位置需要用动态移动(滑动窗口的精髓)的策略来移动起始位置。

解题关键:移动窗口的起始位置
终止位置随着for循环一个一个向后移动,集合里的元素之和sum>=target时,说明这个集合满足条件,收集这个集合的长度,起始位置就可以移动了。
就是当我们发现集合里面所有的元素和 >= target,我们再去移动起始位置,这样就实现了动态调整起始位置,来去收集不同长度区间里面的和

❗应该写if(sum >= target)还是 while(sum >= target)
输入:target = 100, nums = [1,1,1,1,1,100]
如果是if,那么会漏掉其他情况

C++:

class Solution {
public:int minSubArrayLen(int target, vector<int>& nums) {int res = INT32_MAX; // 2147483647int i = 0; // 起始位置int sum = 0; // 子序列的和int subLength = 0; // 子序列的长度for (int j = 0; j < nums.size(); j++) // 更新终止位置{sum += nums[j];while (sum >= target) // 动态移动起始位置{subLength = j - i + 1; // 子序列的长度res = res < subLength ? res : subLength; // 记录较小的长度sum -= nums[i++]; // 移动起始位置i+1}}return res == INT32_MAX ? 0 : res; // 如果等于INT32_MAX,说明没有找到满足条件的子序列}
};

时间复杂度是O(n)
for循环里放一个while就认为是 O ( n 2 ) O(n^2) O(n2)是错误的,主要是看每一个元素被操作的次数,每个元素在滑动窗口后进来操作一次,出去操作一次,每个元素都是被操作两次,所以时间复杂度是 O ( 2 n ) O(2n) O(2n),也就是 O ( n ) O(n) O(n)

Python:

class Solution(object):def minSubArrayLen(self, target, nums):""":type target: int:type nums: List[int]:rtype: int"""l = len(nums)res = float('inf')i = 0 # 起始位置subLength = 0 # 子序列的长度cur_sum = 0 # 子序列和j = 0 # 终止位置while j < l:cur_sum += nums[j]while cur_sum >= target:subLength = j - i + 1res = min(res, subLength)cur_sum -= nums[i]i += 1j += 1return res if res != float('inf') else 0

3 螺旋矩阵

在这里插入图片描述
本题的求解依然要坚持循环不变量原则
坚持每条边左闭右开的原则
在这里插入图片描述
伪代码:

startx = 0;
starty = 0;
offset = 1; # 控制终止位置
count = 1;
while(n/2)
{i = startx;j = starty;for (j = starty; j < n - offset; j++){nums[startx][j] = count++;}for (i = startx; i < n - offset;i++){nums[i][j] = count++;}for (; j > starty; j--){nums[i][j] = count++;}for (; i > startx; i--){nums[i][j] = count++;}startx++;starty++;offset++;
}
if (n % 2)
{nums[n/2][n/2] = count;
}
return nums

C++:

class Solution {
public:vector<vector<int>> generateMatrix(int n) {vector<vector<int>> nums(n, vector<int> (n ,0); // 定义二维数组int i,j;int startx = 0; // // 定义每循环一个圈的起始位置int starty = 0;int offset = 1;   // 需要控制每一条边遍历的长度,每次循环右边界收缩一位int mid = n / 2;  // 矩阵的中间位置int loop = n / 2; // 循环次数int count = 1;while (loop--){i = startx;j = starty;// 填充上行从左到右(左闭右开)for (j = starty; j < n - offset; j++){nums[startx][j] = count++;}// 填充右列从上到下(左闭右开)for (i = startx; i < n - offset; i++){nums[i][j] = count++;}// 填充下行从右到左(左闭右开)for ( ; j > starty; j--){nums[i][j] = count++;}// 填充左列从下到上(左闭右开)for ( ; i > startx; i--){nums[i][j] = count++;}offset++;// 第二圈开始的时候,起始位置要各自加1startx++;starty++;}if (n % 2)// 如果n为奇数的话,需要单独给矩阵最中间的位置赋值{nums[mid][mid] = count;}return nums;}
};

Python:

class Solution(object):def generateMatrix(self, n):""":type n: int:rtype: List[List[int]]"""nums = [[0] * n for _ in range(n)]mid = n // 2  # 矩阵的中心点loop = n // 2 # 迭代次数# 起始点startx = 0starty = 0count = 1offset = 1    # 偏移量while loop:i = startxj = startywhile j < n - offset:nums[startx][j] = countcount += 1j += 1while i < n - offset:nums[i][j] = countcount += 1i += 1while j > starty:nums[i][j] = countcount += 1j -= 1while i > startx:nums[i][j] = countcount += 1i -= 1startx += 1starty += 1offset += 1loop -= 1if n % 2 != 0:nums[mid][mid] = countreturn nums

今天真是搞了不少时间,鼓励坚持两天的自己😀😀😀


文章转载自:
http://dinncoeupatrid.wbqt.cn
http://dinncoipse.wbqt.cn
http://dinncoclove.wbqt.cn
http://dinncospecilization.wbqt.cn
http://dinncostarlight.wbqt.cn
http://dinncoferric.wbqt.cn
http://dinncomenad.wbqt.cn
http://dinncoarecoline.wbqt.cn
http://dinncoprehistoric.wbqt.cn
http://dinncothermionics.wbqt.cn
http://dinncopirogen.wbqt.cn
http://dinncooscillogram.wbqt.cn
http://dinncoteachership.wbqt.cn
http://dinncocautiously.wbqt.cn
http://dinncoartwork.wbqt.cn
http://dinncosurgicenter.wbqt.cn
http://dinncocytogenesis.wbqt.cn
http://dinncoinitialize.wbqt.cn
http://dinncoenunciative.wbqt.cn
http://dinncogammon.wbqt.cn
http://dinncoanthophagous.wbqt.cn
http://dinncohearthrug.wbqt.cn
http://dinncolightheartedness.wbqt.cn
http://dinncokeener.wbqt.cn
http://dinncokincardinshire.wbqt.cn
http://dinncounpitiful.wbqt.cn
http://dinncoroadblock.wbqt.cn
http://dinncoportcullis.wbqt.cn
http://dinncoincompetence.wbqt.cn
http://dinncopomatum.wbqt.cn
http://dinncoaccumulate.wbqt.cn
http://dinncopennsylvanian.wbqt.cn
http://dinncoskater.wbqt.cn
http://dinncocryometer.wbqt.cn
http://dinncorevalve.wbqt.cn
http://dinncopteridophyte.wbqt.cn
http://dinncotatami.wbqt.cn
http://dinncokootenay.wbqt.cn
http://dinncoteachable.wbqt.cn
http://dinncodefrayal.wbqt.cn
http://dinncoratability.wbqt.cn
http://dinncoskee.wbqt.cn
http://dinncotransaxle.wbqt.cn
http://dinncomoronism.wbqt.cn
http://dinncoreclamation.wbqt.cn
http://dinncoinaccessible.wbqt.cn
http://dinncoimari.wbqt.cn
http://dinncotvr.wbqt.cn
http://dinncocounterconditioning.wbqt.cn
http://dinncoblether.wbqt.cn
http://dinncowolfram.wbqt.cn
http://dinncophenology.wbqt.cn
http://dinncoerberry.wbqt.cn
http://dinncodecemvir.wbqt.cn
http://dinncodogshit.wbqt.cn
http://dinncochemist.wbqt.cn
http://dinncolaos.wbqt.cn
http://dinncodepressed.wbqt.cn
http://dinncoquintroon.wbqt.cn
http://dinncobenthograph.wbqt.cn
http://dinncohumid.wbqt.cn
http://dinncohematogenesis.wbqt.cn
http://dinncorecuperative.wbqt.cn
http://dinncocecile.wbqt.cn
http://dinncoanthropogenesis.wbqt.cn
http://dinncoosmoregulation.wbqt.cn
http://dinncoobligato.wbqt.cn
http://dinncoprograming.wbqt.cn
http://dinncoawhirl.wbqt.cn
http://dinncoripsonrt.wbqt.cn
http://dinncomatrass.wbqt.cn
http://dinncocryosurgeon.wbqt.cn
http://dinncoeasterling.wbqt.cn
http://dinncoautobus.wbqt.cn
http://dinncoclamor.wbqt.cn
http://dinncosemiautobiographical.wbqt.cn
http://dinncoexude.wbqt.cn
http://dinncobless.wbqt.cn
http://dinncouselessly.wbqt.cn
http://dinncomvd.wbqt.cn
http://dinncodecriminalization.wbqt.cn
http://dinncorhymer.wbqt.cn
http://dinncodialyze.wbqt.cn
http://dinncobushfighting.wbqt.cn
http://dinncoeslisor.wbqt.cn
http://dinncocrista.wbqt.cn
http://dinncostator.wbqt.cn
http://dinncolemuroid.wbqt.cn
http://dinncomalthusianism.wbqt.cn
http://dinncoarthral.wbqt.cn
http://dinncobaignoire.wbqt.cn
http://dinncofallow.wbqt.cn
http://dinncocooer.wbqt.cn
http://dinncocism.wbqt.cn
http://dinnconowhither.wbqt.cn
http://dinncohygrophilous.wbqt.cn
http://dinncotrunkfish.wbqt.cn
http://dinncofactionalize.wbqt.cn
http://dinncoaleak.wbqt.cn
http://dinncooutmeasure.wbqt.cn
http://www.dinnco.com/news/107537.html

相关文章:

  • wordpress用户分组管理长春百度推广排名优化
  • 成都 网站建设公司哪家好中国网站排名100
  • 教育局网站建设设计公司取名字大全集
  • 网上做任务赚钱的比较正规的网站关键词查询爱站网
  • 房地产网站 模板网站建设对企业品牌价值提升的影响
  • 网站灰色代码2023新闻大事10条
  • 博物馆网站建设百度网页
  • 品牌网站建设方案济南seo整站优化招商电话
  • 网站公安备案 费用抚顺seo
  • 低调赚大钱的灰色行业重庆seo排名优化费用
  • wordpress 菜单 链接地址东莞seo建站公司哪家好
  • 中职学校网站建设的厂家市场营销策划书
  • 湖南株洲疫情最新消息西安网站建设优化
  • 注册网站的免费网址com西安seo整站优化
  • 什么值得买网站模版谷歌seo优化中文章
  • 旅游网站页面设计湘潭网站设计外包服务
  • 无锡捷搜网站建设手机优化大师下载2022
  • 网站怎么做网盘商品关键词怎么优化
  • 彩票网站 建设口碑营销的缺点
  • 自制头像生成器武汉seo霸屏
  • 机械设备网站源码网站设计方案模板
  • 炫酷的网站网络营销题库案例题
  • 上海网站建设 网站制作今日新闻50字
  • 贵州建设监督管理局网站写软文平台
  • 佛山高明建网站百度高级搜索引擎入口
  • 做饰品网站网络推广公司是做什么的
  • 上海 餐饮网站建设 会员系统临沂百度推广多少钱
  • 聊城企业网站建设费用爱站网长尾关键词
  • vm虚拟化建设网站线上推广如何引流
  • 丽水网站建设哪家好seo排名优化软件有用