LeetCode # Longest Common Prefix #

來源:互聯網
上載者:User

標籤:leetcode






我的Python 解答:
"""Programmer  :   EOFDate        :   2015.04.15File        :   lcp.pyE-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;}












LeetCode # Longest Common Prefix #

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.