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

政府门户网站模板百度视频推广

政府门户网站模板,百度视频推广,亚瑟中文 在线,成都网站建设定制开发服务给定一个正整数 n,生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 解: class Solution {public int[][] generateMatrix(int n) {int[][] ar…

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

示例:

输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]

解:

class Solution {public int[][] generateMatrix(int n) {int[][] arr = new int[n][n]; // 创建 n * n 数组
​int topRow = 0, bottomRow = n - 1; // 定义上边界和下边界int leftCol = 0, rightCol = n - 1; // 定义左边界和右边界int direction = 0; // 初始方向,0代表向右
​int num = 1; // 要赋的值
​while (topRow <= bottomRow && leftCol <= rightCol) {if (direction == 0) { // 向右for (int i = leftCol; i <= rightCol; i++) {arr[topRow][i] = num++;}topRow++; // 上边界下移} else if (direction == 1) { // 向下for (int i = topRow; i <= bottomRow; i++) {arr[i][rightCol] = num++;}rightCol--; // 右边界左移} else if (direction == 2) { // 向左for (int i = rightCol; i >= leftCol; i--) {arr[bottomRow][i] = num++;}bottomRow--; // 下边界上移} else if (direction == 3) { // 向上for (int i = bottomRow; i >= topRow; i--) {arr[i][leftCol] = num++;}leftCol++; // 左边界右移}direction = (direction + 1) % 4; // 切换方向}return arr;}
}

另解:

class Solution {public int[][] generateMatrix(int n) {int loop = 0;  // 控制循环次数int[][] res = new int[n][n];int start = 0;  // 每次循环的开始点(start, start)int count = 1;  // 定义填充数字int i, j;
​while (loop++ < n / 2) { // 判断边界后,loop从1开始// 模拟上侧从左到右for (j = start; j < n - loop; j++) {res[start][j] = count++;}
​// 模拟右侧从上到下for (i = start; i < n - loop; i++) {res[i][j] = count++;}
​// 模拟下侧从右到左for (; j >= loop; j--) {res[i][j] = count++;}
​// 模拟左侧从下到上for (; i >= loop; i--) {res[i][j] = count++;}start++;}
​if (n % 2 == 1) {res[start][start] = count;}
​return res;}
}

http://www.tj-hxxt.cn/news/127279.html

相关文章:

  • 温州网站建设温州网站制作打开app下载
  • 怎么弄微信小程序卖东西天猫seo搜索优化
  • 武大网站建设搜索引擎入口大全
  • app 网站开发培训机构推荐
  • 企业首次建设网站的策划方案企业网站seo
  • wordpress+翻页函数杭州哪家seo公司好
  • 晋中市政府门户网站seo完整教程视频教程
  • 性用品微商做的最好的网站广东深圳龙华区
  • 不需要网站备案的空间seo首页优化
  • 长沙抖音代运营南阳seo
  • 现在如何给网站做外链网络营销策略包括
  • 网站需要什么服务器品牌推广方案怎么写
  • 淘宝上做网站SEO靠谱吗网站seo提升
  • 做微网站的第三方登录拓客团队怎么联系
  • 什么视频直播网站做挣钱百度网站统计
  • 网站的 联系我们怎么做长春seo主管
  • 做房间预定网站需要什么软件免费发广告的平台有哪些
  • 做室内效果图网站淘宝seo具体优化方法
  • 网站建设多少钱百度如何发布信息推广
  • 五莲网站设计百度指数有三个功能模块
  • 温州网站设计平台重庆百度seo代理
  • 做内贸在哪些网站上找客户b2b多平台一键发布
  • 网站制作排名黑帽seo排名优化
  • 网站的登录弹窗怎么做最近的电脑培训班在哪里
  • 校园环境设计规划及实施方案seo运营学校
  • 天津网站建设排名比较好的网络推广平台
  • 网站建设技术团队有多重要微信公众号运营推广方案
  • 什么做网站开发软文媒体发稿平台
  • 网站中的搜索框图标怎么做的武汉推广服务
  • n怎样建立自己的网站厦门人才网招聘官网