LeetCode # Longest Common Prefix #






我的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;
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值