leetcode 14

14. Longest Common Prefix

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

找出字符串集合的最长公共前缀。

思路:先计算出前两个字符串的最长公共前缀s,然后用s去和剩下的字符串依次计算公共前缀,若计算结果的长度小于s的长度时替换s,

循环执行,直到所有字符串参与计算,此时s即为整个字符串集合的最长公共前缀。

需要注意当字符串集合为空时的处理。

代码:

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {
 4         string longest = "";
 5         int n = 0;
 6         if(strs.size() == 1)
 7         {
 8             return strs[0];
 9         }
10         if(strs.size() == 0)
11         {
12             return "";
13         }
14         strs[0].length() > strs[1].length() ? n = strs[1].length() : n = strs[0].length();
15         for(int i = 0; i < n; ++i)
16         {
17             if(strs[0][i] == strs[1][i])
18             {
19                 longest += strs[0][i];
20             }
21             else
22             {
23                 break;
24             }
25         }
26         for(int i = 2; i < strs.size(); ++i)
27         {
28             string s = "";
29             for(int j = 0; j < longest.length(); ++j)
30             {
31                 if(j >= strs[i].length() && strs[i][0] == longest[0])
32                 {
33                     s = strs[i];
34                 }
35                 if(longest[j] == strs[i][j])
36                 {
37                     s += longest[j];
38                 }
39                 else
40                 {
41                     break;
42                 }
43             }
44             longest = s;
45         }
46         return longest;
47     }
48 };
时间: 2024-08-03 16:59:10

leetcode 14的相关文章

LeetCode 14. 最长公共前缀(Longest Common Prefix)

14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". LeetCode14. Longest Common Prefix 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racec

leetCode 14. Longest Common Prefix 字符串

14. 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) {         if(strs.size() == 0)

Java [leetcode 14] Longest Common Prefix

小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 该问题就是找到所有数组字符串里面的最长相同前字串.所以我的思路是先找到数组中最短的那个字符串,然后每次比较的时候最多循环该长度就行,这样避免字符串下标溢出的问题.设置StringBuilder对象用于存

[leetcode] 14. Climbing Stairs

这道题leetcode上面写着是DP问题,问题是我一开始写了个简单的递归结果直接超时,所以没办法只好拿迭代来做了.题目如下: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这个基本就可以很简单的做成递归.如果楼层是

Longest Common Prefix [LeetCode 14]

1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路分析 将数组内每个字串转换为List,每次批量取出各列表对应元素存入新列表,对新列表使用set去重.若set长度为1,说明元素一样,算入前缀. 3- Python实现 1 class Solution: 2 # @param {string[]} strs 3 # @return {string}

[LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现

Q: Write a function to find the longest common prefix string amongst an array of strings. A: 这题的大概意思就是说给你一组字符串找出其中最长的哪个通用的前缀出来.这个东西不难找,但是如何找的又快又好不简单.其实这题本来就是easy题,但是却让我联想到了<数据结构与算法分析>上的一道题目,那道题目是这样的: 给一个8900个字的字典,从中间找出类似abc.bbc.abb这样单词中只有一个字母不同的单词进行

python(leetcode)-14最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀. 说明: 所有输入只包含小写字母 a-

# 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"

LeetCode (14) Plus One

题目描述 Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 使用字符串表示数字,对数字进行"加1"操作,返回结果字符串.数字的高位在左边,低位在右边,与我们通常看到的数字顺序习惯相同. 本题比