LeetCode79 Word Search
来源:互联网 发布:杰威尔面膜好吗 知乎 编辑:程序博客网 时间:2024/06/06 14:20
LeetCode79 Word Search
问题描述
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.
问题分析
这道题我感觉就是简单的考验对数组的操作,因为还有一个状态的问题,所以,需要保存整个二维数组的状态来代表这个单元格的元素有没有被使用。
其中需要多考虑到的一点是避免出现越界的情况。
代码如下
public boolean exist(char[][] board, String word) { if(board==null||board.length<1||board[0].length<1){ return false; } int m = board.length; int n = board[0].length; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if(board[i][j]==word.charAt(0)){ boolean[][] dp = new boolean[m][n]; dp[i][j]=true; help(board,word,dp,1,i,j); if (result) { return result; } } } } return result;}boolean result =false;private void help(char[][] board,String word,boolean[][] dp,int index,int i,int j){ if(index==word.length()){ result=true; }else { if(result==true){ return; } if(i<board.length-1&&board[i+1][j]==word.charAt(index)&& !dp[i+1][j]){ dp[i+1][j]=true; help(board,word,dp,index+1,i+1,j); dp[i+1][j]=false; } if(j<board[0].length-1&&board[i][j+1]==word.charAt(index)&& !dp[i][j+1]){ dp[i][j+1]=true; help(board,word,dp,index+1,i,j+1); dp[i][j+1]=false; } if(i>0&&board[i-1][j]==word.charAt(index)&& !dp[i-1][j]){ dp[i-1][j]=true; help(board,word,dp,index+1,i-1,j); dp[i-1][j]=false; } if(j>0&&board[i][j-1]==word.charAt(index)&&!dp[i][j-1]){ dp[i][j-1]=true; help(board,word,dp,index+1,i,j-1); dp[i][j-1]=false; } }}
LeetCode学习笔记持续更新
GitHub地址 https://github.com/yanqinghe/leetcode
CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678
阅读全文
0 0
- LeetCode79:Word Search
- leetcode79. Word Search
- LeetCode79 Word Search
- leetcode79——Word Search
- LeetCode79——Word Search
- leetcode79 word serach 解题报告
- Word Search
- Word Search
- word search
- Word Search
- Word Search
- Word Search
- Word Search
- Word Search
- Word Search
- Word Search
- Word Search
- Word Search
- python数据可视化——散点图实例之随机漫步
- 内存与日志
- 学习日志10.10
- poj1308 Is It A Tree
- Win7下安装双系统Ubuntu16.04
- LeetCode79 Word Search
- 整流堆
- 游戏分包机制实现
- GreenDao的应用
- java基础之map遍历
- centos-7配置静态ip
- 最全金融人士必读书单:10个类别的精荐书籍(58本)
- 0.写在学习之前
- leetcode 4. Median of Two Sorted Arrays