LeetCode # Longest Common Prefix #






我的Python 解答:

"""
Programmer  :   EOF
Date        :   2015.04.15
File        :   lcp.py
E-mail      :   [email protected]
"""

"""
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;
}












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章