LeetCode 14. Longest Common Prefix 找字符串数组最长相同前缀

来源:互联网 发布:中国突破第一岛链 知乎 编辑:程序博客网 时间:2024/06/08 15:05

题目:

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

Subscribe to see which companies asked this question.

public class Solution {    public String longestCommonPrefix(String[] strs) {        if(strs==null||strs.length==0)        return "";        String result = strs[0];        int i=1;        while(i<strs.length){        while(!strs[i].startsWith(result)){        result = result.substring(0, result.length()-1);        }        i++;        }        return result;    }}


0 0
原创粉丝点击