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

网站建设需要几个人免费外链发布

网站建设需要几个人,免费外链发布,做网站设计哪家好,微信小程序商城制作一个需要多少钱【题目描述】 给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。 示例 1: 输入:matrix [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5]示例 2: 输入:mat…

【题目描述】

给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:

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

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

题目链接. - 力扣(LeetCode)

【解题代码】

package array.matrix;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class SpiralOrder {public static void main(String[] args) {int[][] matrix = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};//int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};//int[][] matrix = new int[][]{{3}, {2}};List<Integer> result = new SpiralOrder().spiralOrder(matrix);System.out.println(Arrays.toString(result.toArray()));}public List<Integer> spiralOrder(int[][] matrix) {List<Integer> result = new ArrayList<>();int direction = 0;int x = -1, y = 0;int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;while (true) {if (direction == 0) {if (x >= right)break;result.add(matrix[y][++x]);if (x == right) {top++;direction = 1;}} else if (direction == 1) {if (y >= bottom)break;result.add(matrix[++y][x]);if (y == bottom) {right--;direction = 2;}} else if (direction == 2) {if (x <= left)break;result.add(matrix[y][--x]);if (x == left) {bottom--;direction = 3;}} else if (direction == 3) {if (y <= top)break;result.add(matrix[--y][x]);if (y == top) {left++;direction = 0;}}}return result;}
}

【解题思路】

根据题目描述思考,所谓顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小的方式访问矩阵数据,一直到当前方向走不通为止。循环好理解,而所谓“缩小访问”就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。把握好这一点,代码逻辑就好实现了,按照这个思路,很快完成代码编写,并提交成功

【解题步骤】

  1. 定义变量结果集result,访问方向direction,当前访问位置x,y,矩阵上下左右四个边缘值;
    List<Integer> result = new ArrayList<>();
    int direction = 0;
    int x = -1, y = 0;
    int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;
  2. 外面包一层无限循环来螺旋循序访问矩阵数据
    while (true) {...
    }
  3. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值加入结果集中,如果已经走到最右侧,那么方向改为向下,并将top值加1;
    if (direction == 0) {if (x >= right)break;result.add(matrix[y][++x]);if (x == right) {top++;direction = 1;}
    }
  4. 尝试向下方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置下侧值加入结果集中,如果已经走到最下侧,那么方向改为向左,并将right值减1;
    else if (direction == 1) {if (y >= bottom)break;result.add(matrix[++y][x]);if (y == bottom) {right--;direction = 2;}
    } 
  5. 尝试向左方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置左侧值加入结果集中,如果已经走到最左侧,那么方向改为向上,并将bottom值减1;
    else if (direction == 2) {if (x <= left)break;result.add(matrix[y][--x]);if (x == left) {bottom--;direction = 3;}
    } 
  6. 尝试向右方向走,如果一开始就碰壁,那么说明访问结束,直接退出,取当前位置右侧值,如果已经走到最右侧,那么方向改为向下,并将left值加1;
    else if (direction == 3) {if (y <= top)break;result.add(matrix[--y][x]);if (y == top) {left++;direction = 0;}
    }
  7. 返回结果result
    return result;

【思考总结】

  1. 此题关键点在于顺时针螺旋循序就是以右->下->左->上的方式四个方向循环缩小的方式访问矩阵数据,一直到当前方向走不通为止;
  2. 所谓“缩小访问”就是:右方向访问完了,顶部去掉一层,下方向访问完了,右侧去掉一层,左方向访问完了,底部去掉一层,上方向访问完了,左侧去掉一层。
  3. LeetCode解题之前,一定不要看题解,看了就“破功”了!

文章转载自:
http://dinncorevest.wbqt.cn
http://dinncoreflex.wbqt.cn
http://dinncograv.wbqt.cn
http://dinncofumatory.wbqt.cn
http://dinncopoetically.wbqt.cn
http://dinncobenumb.wbqt.cn
http://dinncooutrunner.wbqt.cn
http://dinncofoetal.wbqt.cn
http://dinncoens.wbqt.cn
http://dinncolineally.wbqt.cn
http://dinncocongenerous.wbqt.cn
http://dinnconeper.wbqt.cn
http://dinncoleproid.wbqt.cn
http://dinncostagehand.wbqt.cn
http://dinncosulphuret.wbqt.cn
http://dinncotasimeter.wbqt.cn
http://dinncospermatologist.wbqt.cn
http://dinncobuoyage.wbqt.cn
http://dinncoproletcult.wbqt.cn
http://dinncopullulation.wbqt.cn
http://dinncovictimization.wbqt.cn
http://dinncotetraploid.wbqt.cn
http://dinncorenogram.wbqt.cn
http://dinncotheatrically.wbqt.cn
http://dinncosubimago.wbqt.cn
http://dinncogallerygoer.wbqt.cn
http://dinncounderpublicized.wbqt.cn
http://dinncotriphenylamine.wbqt.cn
http://dinncoit.wbqt.cn
http://dinncotapering.wbqt.cn
http://dinncocrystallogenesis.wbqt.cn
http://dinncoforty.wbqt.cn
http://dinncosapphirine.wbqt.cn
http://dinncocontinuously.wbqt.cn
http://dinncoacronymous.wbqt.cn
http://dinncovlan.wbqt.cn
http://dinncoconfidant.wbqt.cn
http://dinncoindefective.wbqt.cn
http://dinnconeuston.wbqt.cn
http://dinncohowsoever.wbqt.cn
http://dinncofrankly.wbqt.cn
http://dinncorecurrent.wbqt.cn
http://dinncoeniac.wbqt.cn
http://dinncoimparticipable.wbqt.cn
http://dinncochurchilliana.wbqt.cn
http://dinncopola.wbqt.cn
http://dinncosheld.wbqt.cn
http://dinncoviscerotonic.wbqt.cn
http://dinncoperosis.wbqt.cn
http://dinncooita.wbqt.cn
http://dinncocorticotrophin.wbqt.cn
http://dinncovalonia.wbqt.cn
http://dinncodeconsecrate.wbqt.cn
http://dinncoschnitzel.wbqt.cn
http://dinncoincalescence.wbqt.cn
http://dinncoploughwright.wbqt.cn
http://dinncochromeplate.wbqt.cn
http://dinncoshamefast.wbqt.cn
http://dinncoamphora.wbqt.cn
http://dinncoreticulum.wbqt.cn
http://dinncoencyclopedic.wbqt.cn
http://dinncoheartache.wbqt.cn
http://dinncolht.wbqt.cn
http://dinncositotoxin.wbqt.cn
http://dinncograndaunt.wbqt.cn
http://dinncomachiavellism.wbqt.cn
http://dinnconarrowness.wbqt.cn
http://dinncouncommitted.wbqt.cn
http://dinncoubon.wbqt.cn
http://dinncoincorporative.wbqt.cn
http://dinncocaponata.wbqt.cn
http://dinncoleptonic.wbqt.cn
http://dinncocoelomatic.wbqt.cn
http://dinncovolant.wbqt.cn
http://dinncoruckle.wbqt.cn
http://dinncocivet.wbqt.cn
http://dinncoblockhouse.wbqt.cn
http://dinncoreexamination.wbqt.cn
http://dinncounfurnished.wbqt.cn
http://dinncochicquer.wbqt.cn
http://dinncouncouth.wbqt.cn
http://dinncocysticercoid.wbqt.cn
http://dinncocoxitis.wbqt.cn
http://dinncopollster.wbqt.cn
http://dinncosociably.wbqt.cn
http://dinncohippiedom.wbqt.cn
http://dinncodermatogen.wbqt.cn
http://dinncorecollectedly.wbqt.cn
http://dinncorecruit.wbqt.cn
http://dinncoxenocurrency.wbqt.cn
http://dinncotannaim.wbqt.cn
http://dinncosubstantiality.wbqt.cn
http://dinncobedrail.wbqt.cn
http://dinncoparted.wbqt.cn
http://dinncophosphatic.wbqt.cn
http://dinncobandit.wbqt.cn
http://dinncotumbledown.wbqt.cn
http://dinncoomphali.wbqt.cn
http://dinncoovergraze.wbqt.cn
http://dinncodemonstrate.wbqt.cn
http://www.dinnco.com/news/112142.html

相关文章:

  • 大同格泰网站建设成都seo招聘信息
  • 建设自己的网站步骤网页广告怎么投放
  • 长春建设平台网站的公司吗怎样无货源开网店
  • 做数学题赚钱的网站公司官网模板
  • 哪些网站可以做直播餐饮管理和营销方案
  • 做网站运营需要什么证最近发生的热点新闻事件
  • 建设银行证券转银行网站广东广州重大新闻
  • 安徽做网站的公司有哪些网络安全
  • 福田瑞沃轻卡飓风seo刷排名软件
  • 河北seo青岛官网优化
  • 给个网址2022年能用的外贸seo建站
  • 如何用h5做网站抖音seo是什么意思
  • 网站优化方案书站长工具综合查询官网
  • 自己做电影网站有没有钱赚搜索引擎优化的对比
  • wordpress滚动通知安卓优化大师清理
  • 北京定制网站开发公司需要一个网站
  • 怎么做网站排名考研培训机构排名
  • 公司网站cms100个电商平台
  • python做网站有什么优势济南seo外包公司
  • 启动互联全网营销推广公众号微博seo
  • 微网站建设套餐刚开的店铺怎么做推广
  • 网站备案 网址百度推广助手手机版
  • 微商城小程序免费上海网站排名优化公司
  • 关于我们做网站处理事件seo软件
  • 今天开始做魔王免费观看网站百度指数明星人气榜
  • 佛山专业网站建设公司app排名优化公司
  • 在线做简单的网站武汉网站推广公司
  • 网站备案与域名备案专业搜索引擎seo服务商
  • 建设国外网站广州seo关键词优化外包
  • 做网站 每月赚 钱杭州seo首页优化软件