刚注册在域名可以自己做网站吗,网站建设公司发展历程,做个简单的企业小网站,wordpress怎么设置用户收费给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中#xff0c;返回 true #xff1b;否则#xff0c;返回 false 。 单词必须按照字母顺序#xff0c;通过相邻的单元格内的字母构成#xff0c;其中“相邻”单元格是那些水平相邻或垂直相… 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中返回 true 否则返回 false 。 单词必须按照字母顺序通过相邻的单元格内的字母构成其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 这种是不是和岛屿搜索的类型题是相似的每个点都有8个位置的选择这种类型题就可以用我们上次讲的岛屿数量的解法通过深度优先遍历dfs进行解决 //设置方向 上右下左int[] xnum{-1,0,1,0};int[] ynum{0,1,0,-1};
我们可以维护一个visited数组防止走回头路 boolean[][] visited; 递归函数中入参的变量我们看需要哪些原数组肯定是需要的然后我们也需要知道我们已经遍历到哪个点了因为我们要找的是字符串我们也要知道当前遍历到字符串的哪个索引上函数签名如下 private boolean dfs(char[][] board, String word, int startIndex, int x, int y) {} 如果当前遍历到字符串索引的最后一位且网格中也有相同的字符那就说明该路径我们在网格中是可以找到的如果找不到直接返回false,如果当前不是字符串的最后一个索引对应的位置在从当前元素的相邻元素不断的去进行寻找直到找到返回true或者fasle为止
源码如下 //设置方向 上右下左int[] xnum{-1,0,1,0};int[] ynum{0,1,0,-1};boolean[][] visited;int row;int column;public boolean exist(char[][] board, String word) {//对入参进行判断if(boardnull||board.length0||board[0].length0){return false;}//从每一个点都开始进行遍历rowboard.length;columnboard[0].length;visitednew boolean[row][column];for (int i 0; i row; i) {for (int j 0; j column; j) {//如果存在一种情况则返回trueif(dfs(board,word,0,i,j)){return true;}}}return false;}private boolean dfs(char[][] board, String word, int startIndex, int x, int y) {if(startIndexword.length()-1){if(word.charAt(startIndex)board[x][y]){return true;}}if(word.charAt(startIndex)!board[x][y]){return false;}else{//向四个方向进行寻找visited[x][y]true;for (int i 0; i 4; i) {int newxxxnum[i];int newyyynum[i];//如果越界的话则不需要进行考虑if(newx0||newxrow||newy0||newycolumn||visited[newx][newy]){continue;}if(dfs(board,word,startIndex1,newx,newy)){return true;} }//回溯visited[x][y]false;}return false;}
文章转载自: http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.jsxrm.cn.gov.cn.jsxrm.cn http://www.morning.rmyt.cn.gov.cn.rmyt.cn http://www.morning.gryzk.cn.gov.cn.gryzk.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.nqlkb.cn.gov.cn.nqlkb.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn