Skip to content

Latest commit

 

History

History

0014

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Description

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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"] Output: "fl" 

Example 2:

Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. 

Note:

All given inputs are in lowercase letters a-z.

Tags: String

思路

题意是让你从字符串数组中找出公共前缀,我的想法是找出最短的那个字符串的长度 minLen,然后在 0...minLen 的范围比较所有字符串,如果比较到有不同的字符,那么直接返回当前索引长度的字符串即可,否则最后返回最短的字符串即可。

classSolution { publicStringlongestCommonPrefix(String[] strs) { intlen = strs.length; if (len == 0) return""; intminLen = 0x7fffffff; for (Stringstr : strs) minLen = Math.min(minLen, str.length()); for (intj = 0; j < minLen; ++j) for (inti = 1; i < len; ++i) if (strs[0].charAt(j) != strs[i].charAt(j)) returnstrs[0].substring(0, j); returnstrs[0].substring(0, minLen); } }

结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:awesome-java-leetcode

close