leet_14 Longest Common Prefix

package com.mingxin.leetcode.leet_14;

/**
 * Created by Administrator on 2016/1/25.
 */
public class LongestCommonPrefix {

    public static void main(String[] args){

        String[] strs = {"adfdgdf", "adferef", "adfeergf", "adf"};
        String[] strs1 = {"a", "b"};
        String result = longestCommonPrefix(strs1);
        System.out.println(result);
    }

    public static String longestCommonPrefix(String[] strs) {

        if(null == strs){
            return "";
        }
        int strLength = strs.length;
        if(strLength == 0){
            return "";
        }

        int minLength = strs[0].length();

        for(String str:strs){
            int length = str.length();
            if(length < minLength){
                minLength = length;
            }
        }

        if(minLength == 0){
            return "";
        }
        //不要用char[]会有坑
        StringBuilder sb = new StringBuilder();
        int endFlag = 0;//解决break跳不出第二重循环的问题
        for(int i = 0; i < minLength; i++){

            if(endFlag == 1){
                break;
            }

            char c = strs[0].charAt(i);
            for(int j = 0; j < strLength; j++){
                if (c != strs[j].charAt(i)) {
                    endFlag = 1;
                    break;
                }
                if(j == strLength-1){
                    sb.append(c);
                }
            }
        }

        return sb.toString();
    }
}
时间: 2024-10-06 03:31:56

leet_14 Longest Common Prefix的相关文章

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个问题是关于如何寻找字符串数组的公共前缀的,从第一个字母开始,只要共有的都要输出. 思路: 1.首先,如何得到字符串共有前缀,既然共有,那么很容易先想到最短字符串. 2.最短字符串和其余进行比较,这样就省下了大于最短字符串长度的比较. 3.关于字符串的操作,length和null是很必要的判断输入操作,千万别忘记! 4.关

Longest Common Prefix

https://leetcode.com/problems/longest-common-prefix/ Write a function to find the longest common prefix string amongst an array of strings 1 public class Solution { 2 public static String longestCommonPrefix(String[] strs) { 3 if(strs.length==0){retu

14. Longest Common Prefix (最长公共前缀)

一.Description: Write a function to find the longest common prefix string amongst an array of strings. public class Solution { public String longestCommonPrefix(String[] strs) { } } 二.Solutions: 1.思路: 开始想的是数组中每两个相邻字符串进行比较,并取比较的最短的前缀作为整个数组的最短前缀.时间复杂度为

14. 字符串数组的最长公共前缀 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. public class Solution { public string LongestCommonPrefix(string[] strs) { StringBuilder result = new StringBuilder(); if (strs != null && strs.Length > 0) {

LeetCode 14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 让我们在一个字符串中找出所有字符串的共同前缀 class Solution { public: string longestCommonPrefix(vector<string>& strs) { string result = ""; if (!strs.size())return resul

leetCode 14. Longest Common Prefix 字符串

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 题目大意:求一组字符串的最长前缀. 代码如下: class Solution { public:     string longestCommonPrefix(vector<string>& strs) {         if(strs.size() == 0)

LeetCode14 Longest Common Prefix

题意: Write a function to find the longest common prefix string amongst an array of strings. (Easy) 这两天实验室项目太忙了, 老板各种活,只能挑着先水几道easy题,这两个题是昨天做的没来得及写总结. 分析: 暴力的想法遍历比较一下就行,注意遍历的始末位置.优化的话改天再看看discuss 代码: 1 class Solution { 2 public: 3 string longestCommonP

LeetCode #14 Longest Common Prefix (E)

[Problem] Write a function to find the longest common prefix string amongst an array of strings. [Analysis] 思路非常简单,循环验证每一个字符串就可以通过OJ,代码也没有优化. [Solution] public class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0)

LeetCode之LCP(Longest Common Prefix)问题

这个也是简单题目,但是关键在于题意的理解. 题目原文就一句话:Write a function to find the longest common prefix string amongst an array of strings. 题意是给一个字符串数组,找出这个字符串数组中所有字符串的最长公共前缀. 注意是限定的前缀,而不是子串. 所以,直接的解法就是以第一个字符串为基准,逐个比较每个字符.算法复杂度也显然是O(M*N),M是字符串的个数,N是最长前缀的长度. 代码如下: class So