LeetCode刷题【Array】 Word Search
来源:互联网 发布:ros bumper Python 编辑:程序博客网 时间:2024/05/21 09:35
题目:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E']]word =
"ABCCED"
, -> returns true
,word =
"SEE"
, -> returns true
,word =
"ABCB"
, -> returns false
.解决方法一:Runtime: 14 ms
public class Solution { public boolean exist(char[][] board, String word) {if(null==board) return false;if(null==word) return true;int [][] roadMap = new int[board.length][];for(int i=0;i<board.length;i++){roadMap[i] = new int[board[i].length];}for(int i=0;i<board.length;i++){for(int j=0;j<board[i].length;j++){if(board[i][j]==word.charAt(0)){roadMap[i][j] = 1;boolean flag = backTraceWord(board,i,j,roadMap,word,1);if(flag==true) return true;roadMap[i][j] = 0;}}} return false; }private boolean backTraceWord(char[][] board, int r,int c, int[][] roadMap, String word, int index){if(index>=word.length()) return true;boolean flag = false;if(c-1>=0&&roadMap[r][c-1]!=1&&board[r][c-1]==word.charAt(index)){roadMap[r][c-1]=1;flag|=backTraceWord(board,r,c-1,roadMap,word,index+1);if(flag==true) return true;roadMap[r][c-1]=0;}if(r-1>=0&&roadMap[r-1][c]!=1&&board[r-1][c]==word.charAt(index)){roadMap[r-1][c]=1;flag|=backTraceWord(board,r-1,c,roadMap,word,index+1);if(flag==true) return true;roadMap[r-1][c]=0;}if(c+1<roadMap[r].length&&roadMap[r][c+1]!=1&&board[r][c+1]==word.charAt(index)){roadMap[r][c+1]=1;flag|=backTraceWord(board,r,c+1,roadMap,word,index+1);if(flag==true) return true;roadMap[r][c+1]=0;}if(r+1<roadMap.length&&roadMap[r+1][c]!=1&&board[r+1][c]==word.charAt(index)){roadMap[r+1][c]=1;flag|=backTraceWord(board,r+1,c,roadMap,word,index+1);if(flag==true) return true;roadMap[r+1][c]=0;}return flag;}}解决方法二: 不用额外空间
public boolean exist(char[][] board, String word) { char[] w = word.toCharArray(); for (int y=0; y<board.length; y++) { for (int x=0; x<board[y].length; x++) { if (exist(board, y, x, w, 0)) return true; } } return false;}private boolean exist(char[][] board, int y, int x, char[] word, int i) {if (i == word.length) return true;if (y<0 || x<0 || y == board.length || x == board[y].length) return false;if (board[y][x] != word[i]) return false;board[y][x] ^= 256;boolean exist = exist(board, y, x+1, word, i+1)|| exist(board, y, x-1, word, i+1)|| exist(board, y+1, x, word, i+1)|| exist(board, y-1, x, word, i+1);board[y][x] ^= 256;return exist;}
参考:
【1】https://leetcode.com/
0 0
- LeetCode刷题【Array】 Word Search
- leetcode.array--79. Word Search
- 【leetcode】Array——word search(79)
- LeetCode刷题【Array】Search Insert Position
- LeetCode: Word Search
- [Leetcode] Word Search
- LeetCode : Word Search
- Leetcode: Word Search
- [LeetCode] Word Search
- leetcode 56: Word Search
- Leetcode 79 Word Search
- [leetcode ] word search
- [Leetcode] Word Search
- [LeetCode]Word Search
- Leetcode Word Search
- [leetcode]word search
- [leetcode] Word Search
- LeetCode-Word Search
- ubuntu14.04confluence5.4.4、5.6.6、5.9.4破解版安装
- Ubuntu搜狗输入法
- oracle中行级锁与表级锁
- C/C++: 指针数组、数组指针、定长数组
- wnmp(windows+nginx+mysql+php)的环境搭建和配置
- LeetCode刷题【Array】 Word Search
- Java中抽象类和接口区别
- 快速创建表的方法
- linux下tomcat 80端口被占用的解决方法
- IntelliJ Idea 常用快捷键列表
- Linux安装Redis服务
- md5和base64加密后
- 《ACM书中题目》 W
- SVN检出checkout时报错errror: Unable to connect to a repository at URL'svn://localhost'