LeetCode 第 14 題(Longest Common Prefix)
LeetCode 第 14 題(Longest Common Prefix)
Write a function to find the longest common prefix string amongst an array of strings.
這道題比較簡單,主要是要考慮一些特殊情況,比如這個 vector 為空如何處理。下面是我的程式碼。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ret("");
int N = strs.size();
if(N == 0) return ret;
unsigned int i = 0;
while(1)
{
char a;
if(strs[0].size() > i)
{
a = strs[0][i];
}
else
{
return ret;
}
for(int j = 1; j < N; j++)
{
if(strs[j].size() <= i || strs[j][i] != a)
{
return ret;
}
}
ret.push_back(a);
i++;
};
return ret;// 這一行是永遠執行不到的
}
};
相關文章
- Leetcode 14 Longest Common PrefixLeetCode
- LeetCode解題-#14-Longest Common PrefixLeetCode
- Leetcode Longest Common PrefixLeetCode
- [LeetCode]Longest Common PrefixLeetCode
- Leetcode-Longest Common PrefixLeetCode
- Longest Common Prefix leetcode javaLeetCodeJava
- C# 寫 LeetCode easy #14 Longest Common PrefixC#LeetCode
- leetcode學習筆記14 Longest Common PrefixLeetCode筆記
- LeetCode Longest Common Prefix(014)解法總結LeetCode
- LeetCode - 014 - 最長公共字首(longest-common-prefix)LeetCode
- LeetCode | 14. Longest Common Prefix的三種演算法LeetCode演算法
- [LeetCode] Longest Common Prefix 最長共同字首LeetCode
- Longest Common Prefix字串最長公共字首問題字串
- LintCode-Longest Common Subsequence
- LintCode-Longest Common Substring
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- LeetCode每日一題:longest palindromic substringLeetCode每日一題
- LeetCode- Implement Trie (Prefix Tree)LeetCode
- LeetCode第 146 號問題: LRU 快取機制LeetCode快取
- leetcode演算法題解(Java版)-14-第k小數問題LeetCode演算法Java
- Leetcode 32 Longest Valid ParenthesesLeetCode
- LeetCode- Longest Absolute File PathLeetCode
- LeetCode-Longest Increasing SubsequenceLeetCode
- Leetcode Longest Palindromic SubstringLeetCode
- Leetcode-Longest Valid ParenthesesLeetCode
- Leetcode-Longest Consecutive SequenceLeetCode
- leetcode 之 Longest Valid ParenthesesLeetCode
- Longest Valid Parentheses leetcode javaLeetCodeJava
- Longest Consecutive Sequence leetcode javaLeetCodeJava
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- Leetcode第1~10題LeetCode
- leetcode388. Longest Absolute File PathLeetCode
- [LeetCode] 5. Longest Palindromic SLeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- leetcode32_Longest Valid ParenthesesLeetCode
- LeetCode-Longest Increasing Path in a MatrixLeetCode
- Leetcode-Longest Palindromic SubstringLeetCode