LeetCode # Longest Common Prefix #
2015-04-15 15:07
267 查看
我的Python 解答:
""" Programmer : EOF Date : 2015.04.15 File : lcp.py E-mail : jasonleaster@gmail.com """ """ Varible Description: @ret_string : We store the string to be returned into this varible @length : Find the min length of the inputed string in the list """ class Solution: def longestCommonPrefix(self, strs): ret_string = "" if len(strs) == 0: return ret_string else: length = len(strs[0]) for i in range(0, len(strs)): if length > len(strs[i]): length = len(strs[i]) if len(strs) != 1: for j in range(0, length): """ We check the element from back to front. If we can't get the first string in the inputed list of string, we can tell that we the comman prefix break there. """ i = len(strs) - 1 while i >= 1: if strs[i][j] != strs[i-1][j] : break i -= 1 if i == 0: ret_string += strs[i][j] else: break return ret_string #-------- just for testing ---------- s = Solution() strings = ["acb", "bca"] print s.longestCommonPrefix(strings) strings = ["a", "b"] print s.longestCommonPrefix(strings) strings = ["a"] print s.longestCommonPrefix(strings) strings = ["abcd", "ab", "abc"] print s.longestCommonPrefix(strings)
凯旋冲锋的Java解答:
package longest_substring_without_repeating_characters; import java.util.HashMap; import java.util.Map; public class Solution { public int lengthOfLongestSubstring(String s) { int maxLen = 0; int start = 0; Map<Character, Integer> record = new HashMap<>(256); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); Integer pos = record.get(ch); start = Math.max(start, pos == null ? 0 : pos + 1); maxLen = Math.max(i - start + 1, maxLen); record.put(ch, i); } return maxLen; } public static void main(String[] args) { System.out.println(new Solution().lengthOfLongestSubstring("wlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmco")); } }
皓神的C++实现:
// Source : https://oj.leetcode.com/problems/longest-common-prefix/ // Author : Hao Chen // Date : 2014-07-03 /********************************************************************************** * * Write a function to find the longest common prefix string amongst an array of strings. * * **********************************************************************************/ #include <iostream> #include <string> #include <vector> using namespace std; string longestCommonPrefix(vector<string> &strs) { string word; if (strs.size()<=0) return word; for(int i=1; i<=strs[0].size(); i++){ string w = strs[0].substr(0, i); bool match = true; int j=1; for(j=1; j<strs.size(); j++){ if (i>strs[j].size() || w!=strs[j].substr(0, i) ) { match=false; break; } } if (!match) { return word; } word = w; } return word; } int main() { const char* s[]={"abab","aba","abc"}; vector<string> v(s, s+3); cout << longestCommonPrefix(v) <<endl; }
相关文章推荐
- leetcode-14Longest Common Prefix
- (JAVA)LeetCode-14. Longest Common Prefix
- [LeetCode]14. Longest Common Prefix
- [LeetCode] Longest Common Prefix
- Longest Common Prefix --leetcode
- [Leetcode]14. Longest Common Prefix
- leetcode_Longest Common Prefix_easy
- Leetcode - 14. Longest Common Prefix
- Leetcode -- Longest Common Prefix
- Leetcode-14. Longest Common Prefix
- leetcode - Longest Common Prefix
- [Leetcode]Longest Common Prefix
- [LeetCode]Longest Common Prefix
- LeetCode-Longest Common Prefix
- Leetcode: Longest Common Prefix
- [LeetCode] 14. Longest Common Prefix
- (LeetCode)Longest Common Prefix --- 最长公共前缀
- [LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现
- leetcode:Longest Common Prefix(取最长字符串前缀)
- leetcode——Longest Common Prefix