leetcode 日经贴,python code -Word Search II
来源:互联网 发布:java 垃圾评论过滤 编辑:程序博客网 时间:2024/06/05 15:22
Word Search II
struct TrieNode { TrieNode (){ memset(next, 0, sizeof(next)); id = -1; count = 0; } TrieNode* next[26]; int id, count;};class Solution {public: Solution() { root = new TrieNode(); } vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { vector<string> ans; int R = board.size(); if (!R) { return ans; } int C = board[0].size(); if (!C) { return ans; } sort(words.begin(), words.end()); int wordslen = words.size(); if (!wordslen) { return ans; } for (int i = 0; i < wordslen; ++i) { //did't insert the same word twice if (i == 0 || words[i] != words[i - 1]) { insert(words[i].c_str(), i); } } vector<vector<bool> > visit(R); for (int i = 0; i < R; ++i) { visit[i].resize(C); } vector<int> matchids; for (int i = 0; i < R; ++i) { for (int j = 0; j < C; ++j) { match(root, board, visit, R, C, i, j, matchids); } } sort(matchids.begin(), matchids.end()); for (int i = 0; i < matchids.size(); ++i) { ans.push_back(words[matchids[i]]); } return ans; } private: TrieNode* root; const static int dir[4][2]; void insert(const char* s, int id) { TrieNode* p = root; p->count++; while (*s) { if (!p->next[*s - 'a']) { p->next[*s - 'a'] = new TrieNode(); } p = p->next[*s - 'a']; p->count++; ++s; } p->id = id; } int match(TrieNode* cur, vector<vector<char> >& board, vector<vector<bool> >& visit, int R, int C, int r, int c, vector<int>& matchids) { if (!cur) { return 0; } int child = board[r][c] - 'a'; if (!cur->next[child]) { return 0; } TrieNode* next = cur->next[child]; int matchcnt = 0; if (next->id != -1) { matchids.push_back(next->id); if (next->count == 1) { delete next; cur->next[child] = NULL; return 1; } else { matchcnt++; next->id = -1; } } visit[r][c] = true; for (int d = 0; d < 4; ++d) { int nr = r + dir[d][0]; int nc = c + dir[d][1]; if (nr >= 0 && nr < R && nc >= 0 && nc < C && !visit[nr][nc]) { matchcnt += match(next, board, visit, R, C, nr, nc, matchids); } } visit[r][c] = false; if (next->count - matchcnt == 0) { delete next; cur->next[child] = NULL; } else { next->count -= matchcnt; } return matchcnt; }};const int Solution::dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
0 0
- leetcode 日经贴,python code -Word Search II
- LeetCode Word Search II
- [leetcode] Word Search II
- LeetCode Word Search II
- leetcode Word Search II
- LeetCode - Word Search II
- [leetcode]Word Search II
- leetcode Word Search II
- Leetcode -- Word Search II
- LeetCode Word Search II
- Leetcode @ Word Search II
- 【Leetcode】Word Search II
- leetcode:Word Search II
- 【leetCode】Word Search II
- leetcode 日经贴,Cpp code -Word Break II
- 【Leetcode】【python】Word Search
- leetcode 212: Word Search II
- Word Search II Leetcode Java
- HDU5365Run(计算几何)详解
- Android studio两个便利的方法
- atitit. 分销系统规划p8k
- X-code插件资源下载
- Android基础:内容提供者简介
- leetcode 日经贴,python code -Word Search II
- 关于IT的一些想法
- Hdu oj1008 Elevator
- hdoj今年暑假不AC
- 幸运数
- UrlReWriter的使用
- NYOJ 1036 非洲小孩(贪心 水)
- html5开发页游(前话)
- Java字符串题目--去除重复字符并排序