LeetCode之Longest Common Prefix

简介: LeetCode之Longest Common Prefix

1、题目

Write a function to find the longest common prefix string amongst an array of strings

2、代码实现

package leetcode.chenyu.test;
public class LongestCommonPrefix {
  public static void main(String[] args)  {
    String [] ss = {"12345", "3234", "234"};
    String [] ss1 = {"flower", "flow", "flight"};
    String [] ss2 = {"ca", "a"};
    String [] ss3 = {"a", "a"};
    String [] ss4 = {"aa","ab"};
    String [] ss5 = {"abca","aba","aaab"};
    String result =longestCommonPrefix(ss5);
    System.out.printf("result1111:" + result);
  }
  public static boolean isNull(String s) {
    if(s == null || s.length() == 0) {
      return true;
    }
    return false;
  } 
  public  static String longestCommonPrefix(String[] strs) {
    if(strs == null || strs.length ==0) {
      return "";
    }
    int length = strs.length;
    if (length == 1) {
      return strs[0];
    }
    int minLength = Integer.MAX_VALUE;
    int index = 0;
    for (int i = 0; i < strs.length; ++i) {
       if (isNull(strs[i])) {
         return "";
       }
       if (minLength > strs[i].length()) {
         minLength = strs[i].length();
         index = i;
       }
    }
    String result = "";
    for (int i = 1; i <= minLength; ++i) {
       int count = 0;
       for (int j = i; j <= minLength; ++j) {
          String s =  strs[index].substring(count++, j);
          System.out.print("s:" + s + "\t");
          int flag = 0;
          for (int k = 0; k < length; k++) {
            if (k != index) {
              if (!strs[k].contains(s)) {
                break;
              } else {
               flag++;
               System.out.print("flag:" + flag + "\t");
               if (flag == length - 1) {
                 if (result.length() < s.length()) {
                    int num = 0;
                    for (int h = 0; h < length; h++) {
                       if (!strs[h].startsWith(s)) {
                          break;
                       } else {
                         num++;
                       }
                    }
                    if (num == length)
                    result = s;
                 }
                 System.out.println("result:" + result + "\t");
                 break;
               }
              }
             } 
          }
       }
    }
    return result;
  }
}

特么一开始看错题目,虽然结果验证没有出问题,但是超时了,日了狗

下面的代码是求 字符串数组种最大的公共子串


下面的代码是求 字符串数组种最大的公共子串

1.package leetcode.chenyu.test;
public class LongestCommonPrefix {
  public static void main(String[] args)  {
    String [] ss = {"12345", "3234", "234"};
    String [] ss1 = {"flower", "flow", "flight"};
    String [] ss2 = {"ca", "a"};
    String [] ss3 = {"a", "a"};
    String [] ss4 = {"aa","ab"};
    String [] ss5 = {"abca","aba","aaab"};
    String result =longestCommonPrefix(ss5);
    System.out.printf("result1111:" + result);
  }
  public static boolean isNull(String s) {
    if(s == null || s.length() == 0) {
      return true;
    }
    return false;
  } 
  public  static String longestCommonPrefix(String[] strs) {
    if(strs == null || strs.length ==0) {
      return "";
    }
    int length = strs.length;
    if (length == 1) {
      return strs[0];
    }
    int minLength = Integer.MAX_VALUE;
    int index = 0;
    for (int i = 0; i < strs.length; ++i) {
       if (isNull(strs[i])) {
         return "";
       }
       if (minLength > strs[i].length()) {
         minLength = strs[i].length();
         index = i;
       }
    }
    String result = "";
    for (int i = 1; i <= minLength; ++i) {
       int count = 0;
       for (int j = i; j <= minLength; ++j) {
          String s =  strs[index].substring(count++, j);
          System.out.print("s:" + s + "\t");
          int flag = 0;
          for (int k = 0; k < length; k++) {
            if (k != index) {
              if (!strs[k].contains(s)) {
                break;
              } else {
               flag++;
               System.out.print("flag:" + flag + "\t");
               if (flag == length - 1) {
                 if (result.length() < s.length()) {
                    result = s;
                 }
                 System.out.println("result:" + result + "\t");
                 break;
               }
              }
             } 
          }
       }
    }
    return result;
  }
}
相关文章
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
47 0
|
Java
Leetcode 3. Longest Substring Without Repeating Characters
此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。
50 3
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
104 0
LeetCode 424. Longest Repeating Character Replacem
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
74 0
LeetCode 409. Longest Palindrome
LeetCode 395. Longest Substring with At Least K
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 T 的长度。
87 0
LeetCode 395. Longest Substring with At Least K
|
Windows
LeetCode 388. Longest Absolute File Path
我们致力于寻找我们文件系统中文件的最长 (按字符的数量统计) 绝对路径。例如,在上述的第二个例子中,最长路径为 "dir/subdir2/subsubdir2/file2.ext",其长度为 32 (不包含双引号)。
82 0
LeetCode 388. Longest Absolute File Path
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
69 0
LeetCode 329. Longest Increasing Path in a Matrix
|
算法
LeetCode 300. Longest Increasing Subsequence
给定一个无序的整数数组,找到其中最长上升子序列的长度。
52 0
LeetCode 300. Longest Increasing Subsequence
|
算法
LeetCode 128. Longest Consecutive Sequence
给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。
88 0
LeetCode 128. Longest Consecutive Sequence
|
Java 程序员 开发者
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
本文是《LeetCode第三题(Longest Substring Without Repeating Characters)三部曲》的第二篇,前一篇文章已经列出了完整的解题思路,今天来将此思路转化为具体的Java代码
102 0
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化