Longest Common Prefix
来源:互联网 发布:阿里云的cdn怎么样 编辑:程序博客网 时间:2024/04/30 14:29
-----QUESTION-----
Write a function to find the longest common prefix string amongst an array of strings.
-----SOLUTION-----
class Solution {public: string longestCommonPrefix(vector&strs) { if(strs.empty()) return ""; if(strs.size() == 1) return strs[0]; int maxPrefix = strs[0].length(); for(int i=1; i maxPrefix = min(maxPrefix, (int)strs[i].length()); for(int j =0; j < maxPrefix; j++){ //traverse the character in the string if(strs[i][j]!=strs[0][j]) maxPrefix = min(maxPrefix, j); } } if(maxPrefix == 0 ) return ""; return strs[0].substr(0,maxPrefix); }};
解法II:使用字典树
class Solution {public: // definition of TreeNode struct TreeNode{ char ch; // character of the node int count; // number of strings vector children; // children of this node TreeNode() : ch ('$'), count(1){} TreeNode(char c) : ch(c){count = 1;} }; // definition of Trie struct Trie{ TreeNode* root; // root node of the Trie // default constructor Trie(){ root = new TreeNode('$'); } // destructor ~Trie(){ destroy(root); } // destroy the Trie void destroy(TreeNode* &node){ if(node != NULL){ for(int i = 0; i < node->children.size(); ++i){ destroy(node->children[i]); } delete node; } } // insert a string into the Trie void insert(TreeNode* &node, string str){ // null node or empty string if(node == NULL || str == ""){ return; } // search the node for(int i = 0; i < node->children.size(); ++i){ // found it if(node->children[i]->ch == str[0]){ (node->children[i]->count)++; insert(node->children[i], str.substr(1, str.length()-1)); return; } } // a new node of the node TreeNode* child = new TreeNode(str[0]); node->children.push_back(child); insert(child, str.substr(1, str.length()-1)); } // get longest common prefix vector getLongest(TreeNode* &node, int count){ vector res; // null node if(node == NULL || count <= 0){ return res; } if(node != root && node->count < count){ return res; } // search in its children if(node->children.size() > 0){ // generate head char tmp[2]; tmp[0] = node->ch; tmp[1] = '\0'; string head(tmp); for(int i = 0; i < node->children.size(); ++i){ vector r = getLongest(node->children[i], count); for(int j = 0; j < r.size(); ++j){ res.push_back(head + r[j]); } } // there is no common prefix in it children, add itself if(res.size() <= 0){ res.push_back(head); } } else{ char tmp[2]; tmp[0] = node->ch; tmp[1] = '\0'; res.push_back(tmp); } return res; } // print Trie void print(TreeNode* &node){ // null node if(node == NULL){ return; } // print itself cout << "[" << node->ch << "," << node->count << "] "; // print its children for(int i = 0; i < node->children.size(); ++i){ print(node->children[i]); } // print a endl if(node == root){ cout << endl; } } }; // longest common prefix string longestCommonPrefix(vector &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function // empty input if(strs.size() <= 0){ return ""; } // create Trie Trie* trie = new Trie(); for(int i = 0; i < strs.size(); ++i){ trie->insert(trie->root, strs[i]); } // print Trie // trie->print(trie->root); // get longest common prefix string longest = ""; vector res = trie->getLongest(trie->root, strs.size()); for(int i = 0; i < res.size(); ++i){ // NOTE: remove the root node '$' if(res[i].length() - 1 > longest.length()){ longest = res[i].substr(1, res[i].length()-1); } } // delete Trie delete trie; return longest; }};
0 0
- LeetCode: Longest Common Prefix
- LeetCode Longest Common Prefix
- LeetCode : Longest Common Prefix
- [Leetcode] Longest Common Prefix
- leetcode6:Longest Common Prefix
- leetcode22: longest common prefix
- [LeetCode] Longest Common Prefix
- Leetcode14: Longest Common Prefix
- Longest Common Prefix
- Longest Common Prefix
- Longest Common Prefix
- [LeetCode]Longest Common Prefix
- Longest Common Prefix
- LeetCode-Longest Common Prefix
- Longest Common Prefix
- [LeetCode] Longest Common Prefix
- Longest Common Prefix
- LeetCode - Longest Common Prefix
- 从简单实例开始,学会写Makefile(二)
- java的selector用法
- 求二叉树的任意两个节点的最近公共祖先
- 【LeetCode】-Remove Duplicates from Sorted List
- HahaMil数据库(数据库操作组件)
- Longest Common Prefix
- 查找优化
- Android中文API(122) —— AudioRecord
- hdu 4569 TSP
- linux进程间通信-信号通信
- AudioRecord
- Oracel 常用函数
- 极客Web前端开发资源集锦
- Roman to Integer