No.014:Longest Common Prefix

题目:

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

官方难度:

Easy

翻译:

写一个函数,用来寻找一个字符串数组中的最长公共前缀。

思路:

1. 长度为0的数组直接退出,长度为1的数组直接返回第一项,一般处理至少长度为2.

2. 将第一个字符串作为现有的最长公共前缀,第二个字符串开始匹配,循环的长度是当前匹配字符串和目前的最长公共前缀的最小值。

3. 逐一匹配,遇到不同的字符,更新最长公共前缀,直接退出内循环。

4. 若最长公共前缀为空字符串"",直接返回,没必要做下面的循环。

解题中可能遇到的困难:

1. 养成好习惯,比较字符串用String.equals()方法,不要用"=="操作符。

解题代码:

 1     private static String method(String[] array) {
 2         // 特殊处理
 3         if (array == null || array.length == 0) {
 4             return "";
 5         } else if (array.length == 1) {
 6             return array[0];
 7         } else {
 8             String currentPrefix = array[0];
 9             // 从数组第二项开始遍历
10             for (int i = 1; i < array.length; i++) {
11                 // 比较长度取较小者
12                 int length = Math.min(currentPrefix.length(), array[i].length());
13                 // 出现长度为0的空字符串,不作处理,直接返回
14                 if (length == 0) {
15                     return "";
16                 }
17                 StringBuffer sb = new StringBuffer();
18                 for (int j = 0; j < length; j++) {
19                     // 记得用String.equals()方法
20                     if (currentPrefix.substring(j, j + 1).equals(array[i].substring(j, j + 1))) {
21                         sb.append(currentPrefix.substring(j, j + 1));
22                     } else {
23                         // 遇到不一样,赋值退出
24                         currentPrefix = sb.toString();
25                         // 匹配长度为0,直接返回
26                         if (currentPrefix.length() == 0) {
27                             return "";
28                         }
29                         break;
30                     }
31                 }
32             }
33             return currentPrefix;
34         }
35     }

测试代码地址:

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q014.java

LeetCode题目地址:

https://leetcode.com/problems/longest-common-prefix/

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

时间: 2024-08-10 17:01:51

No.014:Longest Common Prefix的相关文章

leetcode笔记:Longest Common Prefix

一. 题目描述 Write a function to find the longest common prefix string amongst an array of strings. 二. 题目分析 题目的大意是,给定一组字符串,找出所有字符串的最长公共前缀. 对比两个字符串的最长公共前缀,其前缀的长度肯定不会超过两个字符串中较短的长度,设最短的字符串长度为n,那么只要比较这两个字符串的前n个字符即可. 使用变量prefix保存两个字符串的最长公共前缀,再将prefix作为一个新的字符串与

【LeetCode】LeetCode——第14题:Longest Common Prefix

14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Submissions: 345681 Difficulty: Easy Write a function to find the longest common prefix string amongst an array of strings. Subscribe to see which compan

LeetCode 14: Longest Common Prefix

Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 求最长公共前缀. 代码例如以下: class Solution { public: string longestCommonPrefix(vector<string>& strs) { int length = strs.size(); if (length <=

Leetcode题目:Longest Common Prefix

题目: Write a function to find the longest common prefix string amongst an array of strings. 题目解答: 求一组字符串的最长公共前缀.使用迭代器数组实现.思路异常的简单. 代码如下: class Solution {public:    string longestCommonPrefix(vector<string>& strs) {        string res;        int s

LeetCode第[14]题(Java): Longest Common Prefix

题目:最长公共前缀 难度:EASY 题目内容: 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: ["flow

# Leetcode 14:Longest Common Prefix 最长公共前缀

公众号:爱写bug 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"

LeetCode12~14 Integer to Roman/Roman to Integer/Longest Common Prefix

一:Integer to Roman 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 链接:https://leetcode.com/problems/integer-to-roman/ 分析:此题关键是确定罗马数字如何表示,比如4并不是IIII而是IV,9并不是VIIII而是IX, 通过不断的取出最高位和余下的得到结果

[LeetCode] 14. Longest Common Prefix 最长共同前缀

Write a function to find the longest common prefix string amongst an array of strings. 这题有好几种解法,个人认为会1,2的解法就可以了,但这种多方法解题的思路可以好好学习一下.具体可参考:Longest Common Prefix 1. 一个一个字符串取比较word by word matching: 先拿前2个,从第一位开始比较,直到发现有不同的字符,此时前面一样的字符串在去和后面的字符串比较,直到结束.可

【LeetCode】014 Longest Common Prefix

题目:LeetCode 014 Longest Common Prefix 题意:给出一组字符串求公共前缀 思路:很多个字符串的公共前缀应该不会很高,所以直接暴力解决就好 但是又有个特判,即当只有一个字符串的时候,直接返回即可.另外,一定要注意每次利用下标访问字符串的时候,一定要判断是否在有效范围内. 代码如下: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 int