LeetCode-Longest Common Prefix

Posted IncredibleThings

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-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) {
    if(strs==null && strs.length()==0){
      return "";
    }
    int len=strs.length();
String pre=strs[0]; for(int i=1; i<len; i++){ int j=0; while(j<pre.length() && j<strs[i].length() && strs[i].charAt(j)==pre.charAt(j)){ j++; } if(j==0){ return ""; } pre=pre.substring(0, j); } return pre; } }

 

以上是关于LeetCode-Longest Common Prefix的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-Longest Substring Without Repeating Characters

LeetCode-Longest Palindromic Subsequence

LeetCode-Longest Palindromic Substring

LeetCode-Longest Substring with At Least K Repeating Characters

spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring

BZOJ1968 [Ahoi2005]COMMON 约数研究