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

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

下面的代码是求 字符串数组种最大的公共子串
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的代码

public class Solution {
    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) {
String s =  strs[index].substring(0, i);
int flag = 0;
for (int k = 0; k < length; k++) {
if (k != index) {
if (!strs[k].contains(s) || !strs[k].startsWith(s)) {
break;
} else {
flag++;
    if (flag == length - 1) {
if (result.length() < s.length()) {
result = s;
}
    break;
   }
     }
     }
     }
}
return result;
}
}
 
(0)

相关推荐

  • ​LeetCode刷题实战14: 最长公共前缀

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战38: 外观数列

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战242:有效的字母异位词

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战212:单词搜索 II

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战186:翻转字符串里的单词 II

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • Leetcode 3. Longest Substring Without Repeating Characters

    Given a string s, find the length of the longest substring without repeating characters. 题解: 初步想法是用一 ...

  • ​LeetCode刷题实战260:只出现一次的数字 III

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • Common Dental Infections in the Primary Care Setti...

    Family physicians commonly encounter patients with dental infections, such as dental caries and peri ...

  • ​LeetCode刷题实战258:各位相加

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战257:二叉树的所有路径

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战256:粉刷房子

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • ​LeetCode刷题实战262:行程和用户

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

  • leetcode刷题笔记-234. 回文链表(java实现)

    题目描述 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 来源:力扣(LeetCo ...

  • ​LeetCode刷题实战255:验证前序遍历序列二叉搜索树

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...