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

狗和女主人做爰网站网站建设报价请示

狗和女主人做爰网站,网站建设报价请示,软件设计公司排名,西安到北京火车时刻表查询LeetCode:48. 旋转图像 受到力扣hot100:54. 螺旋矩阵的启发,我们可以对旋转图像按层旋转,我们只需要记录四个顶点,并且本题是一个方阵,四个顶点就能完成图像的旋转操作。 1、逐层旋转 注意到&#xff0…

LeetCode:48. 旋转图像
在这里插入图片描述

受到力扣hot100:54. 螺旋矩阵的启发,我们可以对旋转图像按层旋转,我们只需要记录四个顶点,并且本题是一个方阵,四个顶点就能完成图像的旋转操作。

1、逐层旋转

注意到,一层的四个顶点存在一定的位置关系,我们只需要记录四个值:
top_rowbottom_rowleft_colright_col,则上右下左四个顶点分别为:

  • (top_row,left_col)、(top_row,right_col)、(bottom_row,right_col)、(bottom_row,left_col)

当我们需要更新层时,注意矩阵的下标,只需进行如下操作:

  • top_row++
  • bottom_row--
  • left_col++
  • right_col--

这样我们就找到了一层的四个顶点,以及更新层的操作。

现在我们只需要逐层更新即可。
时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( 1 ) O(1) O(1)

在这里插入图片描述

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int top_row = 0, left_col = 0;int bottom_row = matrix.size() - 1, right_col = matrix.size() - 1;//由于size() > 1,所以可以这样做while(top_row < bottom_row){//方阵,结束条件int step = right_col - left_col;for(int i = 0; i < step; ++ i){int temp;//上换到右temp = matrix[top_row + i][right_col];matrix[top_row + i][right_col] = matrix[top_row][left_col + i];//右换到下int temp2 = temp;temp = matrix[bottom_row][right_col - i];matrix[bottom_row][right_col - i] = temp2;//下换到左temp2 = temp;temp = matrix[bottom_row - i][left_col];matrix[bottom_row - i][left_col] = temp2;//左换到上matrix[top_row][left_col + i] = temp;}//更新层top_row++;bottom_row--;left_col++;right_col--;}return ;}
};
  • 我们需要注意一个问题,判断结束条件时,由于方阵行数是n可以是偶数也可以是奇数,奇数时,上行和下行相等则结束。但如果是偶数时,他俩会交叉,因此是下行大于上行时结束!
    • 为了在编程时忽略奇偶数的这个问题,我们可以编程时将判断条件更宽泛
    • 如果top_row > bottom_row也不满足条件,那不要写top_row == bottom_row,而是将两者结合起来写,这样可以避免自己的遗漏。

为了节省临时变量,我们也可以按左下转到左上,右下转到左下,右上转到右下,左上转到右上的顺序旋转,这样只需要存储左上的值即可。

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int top_row = 0, left_col = 0;int bottom_row = matrix.size() - 1, right_col = matrix.size() - 1;//由于size() > 1,所以可以这样做while(top_row < bottom_row){//方阵,结束条件int step = right_col - left_col;for(int i = 0; i < step; ++ i){int temp = matrix[top_row][left_col + i];matrix[top_row][left_col + i] = matrix[bottom_row - i][left_col];左换到上matrix[bottom_row - i][left_col] = matrix[bottom_row][right_col - i];//下换到左matrix[bottom_row][right_col - i] = matrix[top_row + i][right_col];//右换到下matrix[top_row + i][right_col] = temp;//上换到右}//更新层top_row++;bottom_row--;left_col++;right_col--;}return ;}
};

和官解的方法二类似。

2、两次翻转等于旋转

在这里插入图片描述

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();// 水平翻转for (int i = 0; i < n / 2; ++i) {for (int j = 0; j < n; ++j) {swap(matrix[i][j], matrix[n - i - 1][j]);}}// 主对角线翻转for (int i = 0; i < n; ++i) {for (int j = 0; j < i; ++j) {swap(matrix[i][j], matrix[j][i]);}}}
};
http://www.15wanjia.com/news/195188.html

相关文章:

  • 门户网站开发教程wordpress主题sns
  • 宁波网站推广合作商wordpress 视频播放
  • 做高端企业网站舞美设计制作公司
  • 汕头市企业网站建设服务机构中山网站优化营销
  • 阿里巴巴做网站教程城市文化网站开发背景
  • 用html做网站的心得体会淮安做网站的公司有哪些公司
  • 顶尖的锦州网站建设国外网站大全帝国cms模板
  • 公司网站设计案例湖南佳程建设有限公司网站
  • 做电商网站电商公司广州互助网站开发
  • 网站开发员纵向发展安全的营销型网站制作
  • 做第一个网站什么类型界面设计图片素材
  • 网站搭建行业wordpress代码实现
  • 门户网站安全建设方案开发小程序费用
  • 大连网站推广爱得科技网站后角色管理权限怎么设置?
  • 恋爱网站建设seo关键词布局技巧
  • 注册电气工程师考试科目搜索引擎seo如何优化
  • 网站后期维护怎么做wordpress 粘贴图片文档
  • 建站平台在线提交功能wordpress分类主题模板下载
  • 企业站网站建设制作平台品牌建设包括
  • 湖南雷锋建设有限公司网站wordpress cms
  • 广东平台网站建设平台中外商贸网站建设平台
  • 乐清网站开发公司上海网页设计公司费用
  • 绵阳做网站的有哪些网站规划建设论文
  • 合肥最好的网站建设公司排名微信运营工具
  • 大同建设局网站钻石网站建设
  • wordpress站点进入时弹窗竞价托管一般要多少钱
  • 下列不属于网站建设规划青州网站建设推广
  • editplus怎么创网站有专门做牙膏的网站吗
  • 263网站建设怎么样wordpress 注册没密码错误
  • 中国建设银行官方招聘网站南宁seo企业优化