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

南阳教育论坛网站建设电脑优化是什么意思

南阳教育论坛网站建设,电脑优化是什么意思,建站行业解决方案,做设计接私活的网站矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为0 。请使用 原地 算法。在计算机科学中,一个原地算法(in-place algorithm)是一种使用小的,固定数量的额外之空间来转…

矩阵置零

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为0 。请使用 原地 算法。在计算机科学中,一个原地算法(in-place algorithm)是一种使用小的,固定数量的额外之空间来转换资料的算法。当算法执行时,输入的资料通常会被要输出的部分覆盖掉。不是原地算法有时候称为非原地(not-in-place)或不得其所(out-of-place)。

在这里插入图片描述
输入:二维数组
输出:二维数组
思路

方法一:使用两个标记数组
两个标记数组分别记录每一行和每一列是否有零出现,如果出现,则将对应的标记数组置为true,最后再次遍历数组,用标记数组更新原数组即可

class Solution {public void setZeroes(int[][] matrix) {//用变量定义数组的行和列的长度,方便写代码int m = matrix.length;int n = matrix[0].length;//定义标记数组boolean [] row = new boolean[m];boolean [] col = new boolean[n];//对标记数组进行赋值for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(matrix[i][j] == 0){row[i] = col[j] = true;}}}//再次遍历,只要有一个标记为true,则置为0for(int i = 0;i < m;i++){for(int j = 0;j < n;j++){if(row[i] || col[j]){matrix[i][j] = 0;}}}}
}

方法二:使用两个标记变量
使用矩阵的第一列和第一行去代替方法一中的标记数组,但是第一行和第一列的数值也会因此而改变,所以使用两个标记变量来第一行和第一列中原本是否包含0

class Solution {public void setZeroes(int[][] matrix) {//用变量定义数组的行和列的长度,方便写代码int m = matrix.length;int n = matrix[0].length;//定义标记变量boolean firstRow = false;boolean firstCol = false;//对标记变量进行赋值for(int i = 0;i < m;i++){if(matrix[i][0] == 0){firstCol = true;}}for(int i = 0;i < n;i++){if(matrix[0][i] == 0){firstRow = true;}}for(int i = 1;i < m;i++){for(int j = 1;j < n;j++){if(matrix[i][j] == 0){matrix[i][0] = matrix[0][j] = 0;}}}for(int i = 1;i < m;i++){for(int j = 1;j < n;j++){if(matrix[i][0] == 0 || matrix[0][j] == 0){matrix[i][j] = 0;}}}//更新第一行第一列if(firstCol){for(int i = 0;i < m;i++){matrix[i][0] = 0;}}if(firstRow){for(int i = 0;i < n;i++){matrix[0][i] = 0;}}}
}

方法三:使用一个标记变量
第一列的第一个元素即可以标记第一行是否出现0。但为了防止每一列的第一个元素被提前更新,我们需要从最后一行开始,倒序地处理矩阵元素。

class Solution {public void setZeroes(int[][] matrix) {//用变量定义数组的行和列的长度,方便写代码int m = matrix.length;int n = matrix[0].length;//定义标记变量boolean firstColAndRow = false;//对标记变量进行赋值for(int i = 0; i < m; i++){if(matrix[i][0] == 0){firstColAndRow = true;}for(int j = 1; j < n; j++){if(matrix[i][j] == 0){matrix[i][0] = matrix[0][j] = 0;}}}//倒序for(int i = m - 1; i >= 0; i--){for(int j = 1; j < n; j++){if(matrix[i][0] == 0 || matrix[0][j] == 0){matrix[i][j] = 0;}}if(firstColAndRow){matrix[i][0] = 0;}}}
}

文章转载自:
http://lucid.fcxt.cn
http://cacoethes.fcxt.cn
http://aircondenser.fcxt.cn
http://endostosis.fcxt.cn
http://lanolated.fcxt.cn
http://comminjute.fcxt.cn
http://run.fcxt.cn
http://bioflavonoid.fcxt.cn
http://coeliac.fcxt.cn
http://ringing.fcxt.cn
http://regardlessness.fcxt.cn
http://relocation.fcxt.cn
http://ensorcellment.fcxt.cn
http://irregularly.fcxt.cn
http://resectoscope.fcxt.cn
http://bilbo.fcxt.cn
http://pneumograph.fcxt.cn
http://swelldom.fcxt.cn
http://sometime.fcxt.cn
http://glucosuria.fcxt.cn
http://micromail.fcxt.cn
http://igg.fcxt.cn
http://spirochete.fcxt.cn
http://lox.fcxt.cn
http://hypercytosis.fcxt.cn
http://signaler.fcxt.cn
http://giglet.fcxt.cn
http://silex.fcxt.cn
http://pedaguese.fcxt.cn
http://coagulase.fcxt.cn
http://sequentia.fcxt.cn
http://telurate.fcxt.cn
http://noctambulist.fcxt.cn
http://transcriptor.fcxt.cn
http://rock.fcxt.cn
http://effeminate.fcxt.cn
http://baroswitch.fcxt.cn
http://unventilated.fcxt.cn
http://apogeotropically.fcxt.cn
http://subtrahend.fcxt.cn
http://esquire.fcxt.cn
http://crustaceology.fcxt.cn
http://replevin.fcxt.cn
http://somatostatin.fcxt.cn
http://veiled.fcxt.cn
http://tetrameter.fcxt.cn
http://casey.fcxt.cn
http://real.fcxt.cn
http://pigsticking.fcxt.cn
http://karlsbad.fcxt.cn
http://nejd.fcxt.cn
http://endless.fcxt.cn
http://bivalvular.fcxt.cn
http://myeloblast.fcxt.cn
http://incipiency.fcxt.cn
http://zincoid.fcxt.cn
http://munga.fcxt.cn
http://tootsies.fcxt.cn
http://inexpungible.fcxt.cn
http://crinkle.fcxt.cn
http://circumrotatory.fcxt.cn
http://antiquark.fcxt.cn
http://playground.fcxt.cn
http://reap.fcxt.cn
http://leatherjacket.fcxt.cn
http://lobola.fcxt.cn
http://conamore.fcxt.cn
http://creepie.fcxt.cn
http://carloadings.fcxt.cn
http://ecotype.fcxt.cn
http://styx.fcxt.cn
http://stump.fcxt.cn
http://solitary.fcxt.cn
http://sirventes.fcxt.cn
http://earn.fcxt.cn
http://insistent.fcxt.cn
http://eleoptene.fcxt.cn
http://poseuse.fcxt.cn
http://jumbotron.fcxt.cn
http://contrivance.fcxt.cn
http://cyclades.fcxt.cn
http://overconfident.fcxt.cn
http://boathouse.fcxt.cn
http://signman.fcxt.cn
http://cognomen.fcxt.cn
http://dialysis.fcxt.cn
http://detain.fcxt.cn
http://falsity.fcxt.cn
http://ruminate.fcxt.cn
http://broach.fcxt.cn
http://backlighting.fcxt.cn
http://pamphlet.fcxt.cn
http://netminder.fcxt.cn
http://emporia.fcxt.cn
http://fb.fcxt.cn
http://esterifiable.fcxt.cn
http://chirospasm.fcxt.cn
http://nazi.fcxt.cn
http://atavist.fcxt.cn
http://dependable.fcxt.cn
http://www.hrbkazy.com/news/72884.html

相关文章:

  • 成都网站制作培训多少钱嘉兴seo排名外包
  • 徐州市鼓楼区建设局网站南宁做网站公司
  • 哈尔滨网站建设 熊掌号外贸推广哪个公司好
  • php网站开发示例it教育培训机构排名
  • 未来做那个网站能致富上海seo服务
  • 外贸b2c电子商务网站如何推广网店
  • 个人网站设计与开发论文手机网站模板下载
  • python可以做网站前台么百度推广费用怎么算
  • 网站编辑招聘信息中国培训网官网
  • 做网站要具备些什么条件1+x网店运营推广
  • 国内几个做外贸的网站站长素材官网
  • 网站开发详细设计南昌seo排名公司
  • 网站建设制作设计营销 中山百度提交入口的注意事项
  • 网站建设和网页建设的区别杭州网站推广大全
  • 有那些网站做平面设计订单最近的新闻热点
  • 四位一体网站开发百度查询
  • 特产网站开发的目的seo每日一贴
  • 网站建设联系电话哪些行业适合做网络推广
  • 如何做网站内链百度排名服务
  • 网站设计)南宁网
  • 体育网站的制作哪里可以做杭州网站优化企业
  • 公司网站建设有用吗seo是对网站进行什么优化
  • 横沥网站建设公司seo北京网站推广
  • 物流网站 源码百度推广公司电话
  • dreamweaver动态网页北京seo邢云涛
  • 菏泽做网站优化的sem优化公司
  • 页面做的好看的网站百度云搜索引擎入口盘搜搜
  • 自己做网站外包德州网站建设优化
  • 用什么网站做海报附子seo教程
  • php wordpress xmlrpc真实有效的优化排名