首页 > 其他 > 详细

LeetCode # Longest Common Prefix #

时间:2015-04-15 17:12:56      阅读:153      评论:0      收藏:0      [点我收藏+]

技术分享




我的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 # Longest Common Prefix #

原文:http://blog.csdn.net/cinmyheart/article/details/45058549

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!