为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode79. 单词搜索 | Word Search

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9934341.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

Example:

board =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.

给定一个二维网格和一个单词,找出该单词是否存在于网格中。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例:

board =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.

100ms
 1 class Solution {
 2     
 3     func searchSolution(_ board: inout [[Character]],
 4                         _ letter: inout [Character],
 5                         _ visited: inout [[Bool]],
 6                         _ x: Int,
 7                         _ y: Int,
 8                         _ index: Int) -> Bool {
 9         
10         if board[x][y] == letter[index] {
11             
12             visited[x][y] = true
13             
14             if (index + 1) >= letter.count {
15                 return true
16             }
17             
18             if x > 0 {
19                 if visited[x-1][y] == false && searchSolution(&board, &letter, &visited, x - 1, y, index + 1) {
20                     return true
21                 }
22             }
23             if x < (board.count - 1) {
24                 if visited[x+1][y] == false && searchSolution(&board, &letter, &visited, x + 1, y, index + 1) {
25                     return true
26                 }
27             }
28             if y > 0 {
29                 if visited[x][y-1] == false && searchSolution(&board, &letter, &visited, x, y - 1, index + 1) {
30                     return true
31                 }
32             }
33             if y < (board[0].count - 1) {
34                 if visited[x][y+1] == false && searchSolution(&board, &letter, &visited, x, y + 1, index + 1) {
35                     return true
36                 }
37             }
38             
39             visited[x][y] = false
40             
41         }
42         
43         return false
44     }
45     
46     func exist(_ board: [[Character]], _ word: String) -> Bool {
47     
48         var letters = Array(word)
49         var board = board
50         
51         if board.count <= 0 { return false }
52         if board[0].count <= 0 { return false }
53         
54         var visited = [[Bool]](repeating: [Bool](repeating: false, count: board[0].count), count: board.count)
55         
56         for x in 0..<board.count {
57             for y in 0..<board[0].count {
58                 
59                 if searchSolution(&board, &letters, &visited, x, y, 0) {
60                     return true
61                 }
62             }
63         }
64         
65         return false
66     }
67 }

128ms

 1 class Solution {
 2         func exist(_ board: [[Character]], _ word: String) -> Bool {
 3         
 4         if board.count == 0 {
 5             return false
 6         }
 7         let m = board.count
 8         let n = board[0].count
 9         var board = board
10         var wordChars = Array(word)
11         for i in 0..<m {
12             for j in 0..<n {
13                 if helper(&board, i, j, &wordChars, 0) {
14                     return true
15                 }
16             }
17         }
18         return false
19         
20     }
21     
22     func helper(_ board: inout [[Character]], _ i: Int, _ j: Int, _ wordChars: inout [Character], _ cur: Int) -> Bool {
23         let m = board.count
24         let n = board[0].count
25         if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] == "0" || board[i][j] != wordChars[cur]) {
26             return false
27         }
28         if cur == wordChars.count - 1 {
29             return true
30         }
31         let temp = board[i][j]
32         board[i][j] = "0"
33         let res =   helper(&board, i-1, j, &wordChars, cur+1) ||
34                     helper(&board, i+1, j, &wordChars, cur+1) ||
35                     helper(&board, i, j-1, &wordChars, cur+1) ||
36                     helper(&board, i, j+1, &wordChars, cur+1)
37         board[i][j] = temp
38         return res
39     }
40 }

168ms

 1 class Solution {
 2     func exist(_ board: [[Character]], _ word: String) -> Bool {
 3         
 4         let char = word[word.index(word.startIndex, offsetBy: 0)]
 5         
 6         for row in 0..<board.count {
 7             for col in 0..<board[0].count {
 8                 if board[row][col] == char && search(board, word, row, col, 1) { 
 9                     return true
10                 }
11             }
12         }
13         
14         return false
15     }
16     
17     func search(_ board: [[Character]], _ word: String, _ row: Int, _ col: Int, _ index: Int) -> Bool {
18         if index >= word.count { return true }
19         
20         var board = board
21         board[row][col] = "-"
22         
23         let char = word[word.index(word.startIndex, offsetBy: index)]
24          
25         if (row+1) < board.count && board[row+1][col] == char && search(board, word, row+1, col, index+1) {
26             return true
27         } 
28         
29         else if row > 0 && board[row-1][col] == char && search(board, word, row-1, col, index+1) {
30             return true
31         } 
32         
33         else if (col+1) < board[row].count && board[row][col+1] == char && search(board, word, row, col+1, index+1) {
34             return true
35         } 
36         
37         else if col > 0 && board[row][col-1] == char && search(board, word, row, col-1, index+1) {
38             return true
39         }
40         
41         return false
42     }
43 }

172ms

 1 class Solution {
 2     func exist(_ board: [[Character]], _ word: String) -> Bool {
 3         var board = board
 4         var word = Array(word)
 5         for y in stride(from:0,to:board.count,by:1) {
 6             for x in stride(from:0,to:board[0].count,by:1) {
 7                 if dfs(&board,y,x,word,0) {
 8                     return true
 9                 }
10             }
11         }
12         return false
13     }
14     
15     func dfs(_ board:inout[[Character]], _ y:Int,_ x:Int,_ word:Array<Character>,_ wordPos:Int) -> Bool {
16         if wordPos == word.count {
17             return true
18         } else if y < 0 || y >= board.count || x < 0 || x >= board[0].count || board[y][x] != word[wordPos]{
19             return false
20         }
21         let temp = board[y][x]
22         board[y][x] = "#"
23         let res = dfs(&board,y + 1,x,word,wordPos + 1) || 
24         dfs(&board,y - 1,x,word,wordPos + 1) || 
25         dfs(&board,y,x + 1,word,wordPos + 1) || 
26         dfs(&board,y,x - 1,word,wordPos + 1)
27         
28         board[y][x] = temp
29         return res
30     }
31 }

192ms

 1 class Solution {
 2     let rowDirection = [0,0,1,-1]
 3     let colDiriction = [1,-1,0,0]
 4     
 5     func exist(_ board: [[Character]], _ word: String) -> Bool {
 6         guard !board.isEmpty else {
 7             return false
 8         }
 9         
10         let rowCount = board.count
11         let colCount = board[0].count
12         
13         if rowCount * colCount < word.count {
14             return false
15         }
16         
17         var wordChars = [Character](word)
18         
19         var tempBoard = board
20         
21         for rowIndex in 0..<rowCount {
22             for colIndex in 0..<colCount {  
23                 var temp = tempBoard[rowIndex][colIndex]
24                 if temp == wordChars[0]  {    
25                     tempBoard[rowIndex][colIndex] = "0"
26                     if existHelper(&tempBoard, 1, &wordChars, rowIndex, colIndex, 1, rowCount, colCount) {
27                         return true
28                     }
29                     tempBoard[rowIndex][colIndex] = temp
30                 } 
31             }
32         }
33         
34         return false
35     }
36     
37     func existHelper (_ board: inout [[Character]], _ count: Int, _ word: inout [Character], _ rowIndex: Int, _ colIndex: Int, _ index: Int, _ rowCount: Int, _ colCount: Int) -> Bool {
38             
39 
40         if count == word.count {
41             return true
42         }
43         let newCount = count + 1
44         for i in 0..<rowDirection.count {
45             let newRow = rowIndex + rowDirection[i]
46             let newCol = colIndex + colDiriction[i]
47             
48             
49             
50             if newRow < 0 || newRow >= rowCount
51             || newCol < 0 || newCol >= colCount 
52             || board[newRow][newCol] == "0" || board[newRow][newCol] != word[index] {
53                 continue 
54             }
55             
56             let newStr = board[newRow][newCol] 
57             board[newRow][newCol] = "0"
58             
59             if existHelper(&board, newCount, &word, newRow, newCol, index+1, rowCount, colCount) {
60                 return true
61             } 
62             
63             board[newRow][newCol] = newStr
64         }
65         
66         return false
67     }
68 }

316ms

 1 class Solution {
 2     func exist(_ board: [[Character]], _ word: String) -> Bool {
 3                 if board.count == 0 {
 4             return false
 5         }
 6         var visit = Array.init(repeating: Array.init(repeating: false, count: board[0].count), count: board.count)
 7         for i in 0..<board.count {
 8             for j in 0..<board[0].count {
 9                 if exist(board, word, 0, i, j, &visit) {
10                     return true
11                 }
12             }
13         }
14         return false
15 
16     }
17       func exist(_ board: [[Character]], _ word: String,_ index: Int, _ i: Int,_ j: Int, _ visit:inout [[Bool]]) -> Bool {
18         if index == word.count {//已经到字符串最后
19             return true
20         }
21         if i < 0 || j < 0 || i >= board.count || j >= board[0].count {//越界了
22             return false
23         }
24         if visit[i][j] {//访问过了
25             return false
26         }
27         if board[i][j] != word[word.index(word.startIndex, offsetBy: index)] {
28             return false
29         }
30         //该字符符合条件,搜索下一个字符
31         visit[i][j] = true
32         let result = exist(board, word, index + 1, i + 1, j, &visit) || exist(board, word, index + 1, i - 1, j, &visit) || exist(board, word, index + 1, i , j - 1, &visit) || exist(board, word, index + 1, i , j + 1, &visit)
33         visit[i][j] = false
34         return result
35     }
36 }

 

posted @ 2018-11-09 12:02  为敢技术  阅读(481)  评论(0编辑  收藏  举报