[转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

题记:

这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描。

横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串。

纵向扫描:对所有串,从字符串第0位开始比较,全部相等则继续比较第1,2...n位,直到发生不全部相等的情况,则得出最长公共前缀串。

横向扫描算法实现:

//LeetCode_Longest Common Prefix
//Written by zhou
//2013.11.22

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.

        if (strs.size() == 0)
          return "";

        string prefix = strs[0];
        for (int i = 1; i < strs.size(); ++i)
        {
            if (prefix.length() == 0 || strs[i].length() == 0)
               return "";

            int len = prefix.length() < strs[i].length() ? prefix.length() : strs[i].length();

            int j;

            for (j = 0; j < len; ++j)
            {
                if (prefix[j] != strs[i][j])
                    break;
            }

            prefix = prefix.substr(0,j);

        }

        return prefix;
    }
};

纵向扫描代码实现:

function longestCommonPrefix(strs:Array):String{
    if(strs == null || strs.length == 0)return;

    for(i:int = 0; i < str[0].length - 1; i++)
    {
        for(j:int = 0; j < str.length - 1; j++)
        {
            if(str[i].charAt(j) != str[0].charAt(j))return strs[0].substr(0,j);
        }

    }
    return strs[0];
}
时间: 2024-08-06 09:30:06

[转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀的相关文章

求字符串数组最长公共前缀

public class LongestCommonPrefix { public String longestCommonPrefix(String[] strs) { if(strs == null) return null; if(strs.length == 0) return ""; String s = strs[0]; for(int i = 0; i < s.length(); i ++) { for(int j = 1; j < strs.length;

leetcode Longest Common Prefix 多个字符串的最长字串

1 public class Solution { 2 public String get(String a,String b) 3 { 4 5 if(a==""||b=="") return ""; 6 int len1=a.length(); 7 int len2=b.length(); 8 int len=len1; 9 if(len>=len2) len=len2; 10 String s=""; 11 for(

LeetCode——Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中的最长共现前缀字符串. 思路:共现,即要求数组中的所有元素的前缀中都要出现.所以所得的结果肯定是最短字符串的部分或全部或都不是,总之要以最短字符串为基准与其他字符串比较. public static String longestCommonPrefix(String[] strs){ int len

leetcode——Longest Common Prefix 最长公共前缀(AC)

Write a function to find the longest common prefix string amongst an array of strings. 其实做起来会感觉很简单,需要注意的是要考虑效率的问题,毕竟可能是很长的字符串数组,所以可以考虑选取所有字符串中最短的那个来首先进行比较,因为最长公共子串肯定不会大于其长度,这样避免了字符串之间长度差异很大造成的效率损失,然后每次比较之后最长公共子串的长度也永远不会大于最短的那个字符串,只会不变或减小,只要遍历字符串数组,挨个

LeetCode: Longest Common Prefix 题解

Write a function to find the longest common prefix string amongst an array of strings. 题解: 寻找一组字符串的最长公共前缀.  最简单的方法,用一个字符串记录当前最长的公共前缀,然后依次比较.时间复杂度: O(N). 1 class Solution { 2 public: 3 string getPrefix(string a,string b) // 辅助函数用于获取两个字符串的公共前缀 4 { 5 st

Leetcode::Longest Common Prefix &amp;&amp; Search for a Range

一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an array of strings. 分析: 这道题目最重要的知道什么叫prefix前缀, 否则一不小心就做成了最长子序列.前缀就是两个序列的前多少位 都要是一样的,不能跳着对齐,这样就比较简单了,也可以求很多个序列的公共前缀. 1 class Solution { 2 public: 3 string

LeetCode Longest Common Prefix 最长公共前缀

题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短,所以前缀最长也只是最短字符串的长度. 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 string ans=""; 5 if(strs.empty()

[LeetCode] Longest Common Prefix 字符串公有前序

Write a function to find the longest common prefix string amongst an array of strings. Hide Tags String 这是一道很简单的题目,判断输入的多个字符串的公有前序,简单的逻辑遍历查找就好. 算法流程: 判断输入的字符串数量,小于2时候做出相应返回. 获取最短字符串的长度. 设定标记flag,控制跳出循环,与长度返回的长度len. 在最短字符串长度的范围内循环. 循环中每次遍历全部字符串len 位的字

[leetcode] Longest Common Prefix @ Python

Source: https://oj.leetcode.com/problems/longest-common-prefix/ Write a function to find the longest common prefix string amongst an array of strings. Hint:   strs=['abc','ab','a'] strs=[ 'abc', 'ab', 'a', ] We can think of strs as a column length va