LeetCode-79.Word Search
来源:互联网 发布:时时彩彩票精准算法 编辑:程序博客网 时间:2024/05/17 10:39
https://leetcode.com/problems/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
.使用递归依次判断四个方向即可
代码使用的两个小技巧
1、为了省去边界的判断,在原始数组外嵌套一层
2、使用hashset存放访问过的位置(i*n+j)
public class Solution { public bool Exist(char[,] board, string word) { int m = (int)board.GetLongLength(0); int n = (int)board.GetLongLength(1); char[,] map = new char[m + 2, n + 2]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) map[i + 1, j + 1] = board[i, j]; for (int i = 1; i <= m; i++) { for (int j = 1; j <=n; j++) { if (map[i, j] == word[0]) { HashSet<int> set = new HashSet<int>(); set.Add(i * (n+2) + j); Func(map, word, set, 1, i, j); if (set.Count == word.Length) return true; } } } return false; } private void Func(char[,] map, string word, HashSet<int> set, int index, int i, int j) { int n = (int)map.GetLongLength(1); if (index == word.Length) return; if (map[i, j - 1] == word[index]&&!set.Contains(i*n+j-1)) { set.Add(i * n + j-1); Func(map, word, set, index + 1, i, j - 1); if (set.Count == word.Length) return; set.Remove(i * n + j - 1); } if (map[i-1, j] == word[index] && !set.Contains(i * n + j - n)) { set.Add(i * n + j - n); Func(map, word, set, index + 1, i-1, j); if (set.Count == word.Length) return; set.Remove(i * n + j -n); } if (map[i, j + 1] == word[index] && !set.Contains(i * n + j + 1)) { set.Add(i * n + j + 1); Func(map, word, set, index + 1, i, j + 1); if (set.Count == word.Length) return; set.Remove(i * n + j + 1); } if (map[i + 1, j] == word[index] && !set.Contains(i * n + j + n)) { set.Add(i * n + j + n); Func(map, word, set, index + 1, i + 1, j); if (set.Count == word.Length) return; set.Remove(i * n + j + n); } }}
优化
1、每次只判断新word的第一个字符,新word=旧的word去掉第一个字符
2、将board访问过的替换成特殊字符#,取代hashset
public class Solution { public bool Exist(char[,] board, string word) { int m = (int)board.GetLongLength(0); int n = (int)board.GetLongLength(1); char[,] map = new char[m + 2, n + 2]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) map[i + 1, j + 1] = board[i, j]; for (int i = 1; i <= m; i++) { for (int j = 1; j <=n; j++) { if (map[i, j] == word[0]) { map[i, j] = '0'; if (Func(map, word.Substring(1),i, j)) return true; map[i, j] = word[0]; } } } return false; } private bool Func(char[,] map, string word,int i, int j) { if (word=="") return true; if (map[i, j - 1] == word[0]) { map[i, j - 1] = '0'; if(Func(map, word.Substring(1), i, j - 1)) return true; map[i, j - 1] = word[0]; } if (map[i-1, j] == word[0]) { map[i - 1, j] = '0'; if(Func(map, word.Substring(1), i-1, j)) return true; map[i - 1, j] = word[0]; } if (map[i, j + 1] == word[0]) { map[i, j + 1] = '0'; if(Func(map, word.Substring(1), i, j + 1)) return true; map[i, j + 1] = word[0]; } if (map[i + 1, j] == word[0]) { map[i + 1, j] = '0'; if(Func(map, word.Substring(1), i + 1, j)) return true; map[i + 1, j] = word[0]; } return false; }}
0 0
- LeetCode 79. Word Search
- [LeetCode]79.Word Search
- LeetCode --- 79. Word Search
- [Leetcode] 79. Word Search
- [leetcode] 79.Word Search
- 【leetcode】79. Word Search
- [LeetCode]79. Word Search
- 79. Word Search LeetCode
- leetcode 79. Word Search
- LeetCode 79. Word Search
- LeetCode 79. Word Search
- leetcode 79. Word Search
- LeetCode *** 79. Word Search
- [leetcode] 79. Word Search
- LeetCode-79.Word Search
- Leetcode 79. Word Search
- LeetCode - 79. Word Search
- [LeetCode] 79. Word Search
- Python 3.5 HTTP服务器端重定向测试脚本
- 一个小项目(爬虫)的诞生全过程
- 内容提供者,ContentProvider的使用
- 使用AndroidStudio无法使用drawable下的资源,只能使用mipmap目录下的资源?求救大神
- 友元类与友元函数的继承与传递问题
- LeetCode-79.Word Search
- JavaScript---网络编程(3)-Object、String、Array对象和prototype属性
- Linux学习之字符串截取
- 从零开始学React Native App开发
- UVA 10299 Relatives(欧拉函数)
- 线程相关点滴积累(一)
- iOS点击事件重复执行(取消延迟执行函数)
- Google Java编程风格指南(献给那些没有良好编码习惯的程序员们)转载!
- Qualcomm手机NV中某些项的含义解析