LeetCode (4) . Longest Common Prefix
2018-04-01 19:50
344 查看
题目:Write a function to find the longest common prefix string amongst an array of strings. 翻译:写一个函数(方法),找出字符串数组中的最长公共前缀 比如:"abcdejfd", "abcdjjkjk", "abcd", "abcdreoj" 那么最长的公共前缀是 "abcd"
#ifndef LEETCODE_SOLUTION_H#define LEETCODE_SOLUTION_H#include <vector>#include <string>using namespace std;class Solution{public:string longestCommonPrefix(vector<string>& strs){if (strs.empty ())return "";for (size_t i = 0; i < strs.front ().size (); ++i) // 先找一个字符串作为比较的基数{for (const auto &str : strs)if (i == str.size () || str[i] != strs.front ().at (i))return strs.front ().substr (0, i);}}};#endif //LEETCODE_SOLUTION_H
相关文章推荐
- 【LeetCode】014.Longest Common Prefix
- Leetcode-14. Longest Common Prefix
- leetcode--Longest Common Prefix
- [Leetcode]Longest Common Prefix
- LeetCode-Easy-Java-Longest Common Prefix
- Leetcode Longest Common Prefix
- leetcode - Longest Common Prefix
- Longest Common Prefix LeetCode :Mysolution
- leetcode第十四题(easy)——Longest Common Prefix
- [LeetCode]Longest Common Prefix
- leetcode第14题--Longest Common Prefix
- Leetcode 之Longest Common Prefix(34)
- leetcode-14-Longest Common Prefix
- LeetCode——Longest Common Prefix
- [leetcode] Longest Common Prefix
- leetcode 14. Longest Common Prefix
- LeetCode Longest Common Prefix
- 【LeetCode】Longest Common Prefix
- leetcode-14 Longest Common Prefix
- LeetCode 14 -- Longest Common Prefix