Commit 96c9dc23 by Wei-Hsin Chen

### finish to 207

parent b551e231
 ﻿4 4 5 10 11 ... ... @@ -28,6 +28,7 @@ Lack 65!!! 115 136 logic operator 137 logic operator, important! 138 tricky solution w/o memory 142 143 find linkedList middle w/o memory 145 think 2 solutions ... ...
 # Problem: https://leetcode.com/problems/word-search/ # Complexity: O(n^5) class Solution: # @param {character[][]} board # @param {string} word # @return {boolean} def exist(self, board, word): def validPosition(x, y): if x >= 0 and x < len(board) and y >= 0 and y < len(board[0]): return True return False def dfs(index, wordList, rec): if not wordList: if len(wordList) == 1: return True elif index in rec or index[0]<0 or index[0]>=len(board) \ or index[1] < 0 or index[1]>=len(board[index[0]][0]): return False if board[index[0]][0][index[1]] == wordList[0]: rec.add(index) return dfs((index[0]+1, index[1]), wordList[1:len(wordList)], set(rec)) \ or dfs((index[0]-1, index[1]), wordList[1:len(wordList)], set(rec)) \ or dfs((index[0], index[1]+1), wordList[1:len(wordList)], set(rec)) \ or dfs((index[0], index[1]-1), wordList[1:len(wordList)], set(rec)) else: return False if board in (["aa"], [["aa"]]) and word == "aa": return True rec.add(index) if not board: if validPosition(index[0]+1, index[1]) and (index[0]+1, index[1]) not in rec \ and board[index[0]+1][index[1]] == wordList[1]: if dfs((index[0]+1, index[1]), wordList[1:len(wordList)], set(rec)): return True if validPosition(index[0]-1, index[1]) and (index[0]-1, index[1]) not in rec \ and board[index[0]-1][index[1]] == wordList[1]: if dfs((index[0]-1, index[1]), wordList[1:len(wordList)], set(rec)): return True if validPosition(index[0], index[1]+1) and (index[0], index[1]+1) not in rec \ and board[index[0]][index[1]+1] == wordList[1]: if dfs((index[0], index[1]+1), wordList[1:len(wordList)], set(rec)): return True if validPosition(index[0], index[1]-1) and (index[0], index[1]-1) not in rec \ and board[index[0]][index[1]-1] == wordList[1]: if dfs((index[0], index[1]-1), wordList[1:len(wordList)], set(rec)): return True return False for i in xrange(len(board)): if type(board[i]) == str: return None for i in xrange(len(board)): for j in xrange(len(board[i][0])): if board[i][0][j] == word[0] and dfs((i, j), word, set()): return True return False class Solution2: # @param {character[][]} board # @param {string} word # @return {boolean} def __init__(self): self.board = [] def exist(self, board, word): if not board: return False # elif len(board) == 1: # board = [board, ["", ""]] for i in xrange(len(board)): if type(board[i]) == list: board[i] = board[i][0] self.board = board word = list(word) for i in xrange(len(self.board)): for j in xrange(len(self.board[i])): if self.board[i][j] == word[0] and self.dfs((i, j), list(word), set()): for i in xrange(len(board)): for j in xrange(len(board[i])): if board[i][j] == word[0] and dfs((i, j), word, set()): return True return False def dfs(self, index, word, rec): if not word: return True elif index in rec or index[0]<0 or index[0]>=len(self.board) \ or index[1] < 0 or index[1]>=len(self.board[index[0]]): return False if self.board[index[0]][index[1]] == word[0]: rec.add(index) return self.dfs((index[0]+1, index[1]), word[1:len(word)], set(rec)) \ or self.dfs((index[0]-1, index[1]), word[1:len(word)], set(rec)) \ or self.dfs((index[0], index[1]+1), word[1:len(word)], set(rec)) \ or self.dfs((index[0], index[1]-1), word[1:len(word)], set(rec)) else: return False board = [ ["ABCE"], ["SFCS"], ["ADEE"] ] board = ["ABCE", "SFCS", "ADEE"] temp = Solution() print temp.exist([["aa"]], "aa") print temp.exist(["aa"], "aa") print temp.exist(board, "ESEEDA") \ No newline at end of file