No.14 Longest Common Prefix

No.14 Longest Common Prefix

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

求一组string的最长公共前缀
想法:找到最短的那个,然后依次对比

典型的字符串操作,从前向后比较即可

 1 #include "stdafx.h"
 2 #include <string>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 class Solution
 9 {
10 public:
11     string longestCommonPrefix(vector<string> &strs)
12     {//求一组string的最长公共前缀
13      //想法:找到最短的那个,然后依次对比
14         int size = strs.size();
15         string res("");
16         if(size == 0)
17             return res;
18
19         int shortestCount = strs[0].size();//记录最短的
20         for(int i=1; i<size; i++)
21         {
22             if(strs[i].size() < shortestCount)
23                 shortestCount = strs[i].size();
24         }
25
26         //从前向后比较shortestCount位
27         for(int i=0; i<shortestCount; i++)
28         {
29             auto tmp = strs[0][i];
30             for(int j=1; j<size; j++)
31             {
32                 if(strs[j][i] != tmp)
33                     return res;
34             }
35             res += tmp;
36         }
37         return res;
38     }
39 };
40
41 int main()
42 {
43     Solution sol;
44     string data[] = {"a","abc","ad"};
45     vector<string> test(data,data+3);
46     cout << sol.longestCommonPrefix(test)<<endl;
47
48     string data1[] = {"ad","abc","ad",""};
49     vector<string> test1(data1,data1+4);
50     cout << sol.longestCommonPrefix(test1)<<endl;
51 }
时间: 2024-10-08 09:47:51

No.14 Longest Common Prefix的相关文章

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)

14. Longest Common Prefix【leetcode】

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 寻找一个数组中最长的公共前缀 例如["baaa","caaabbb","aaaa"]输出"aaa" 结题思路: 判断非空的情况在进行计算 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中 用第一

[LeetCode][Python]14: Longest Common Prefix

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-common-prefix/14: Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.===Comments by Dabay===注意边

leedCode练题——14. Longest Common Prefix

1.题目 14. Longest Common Prefix 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&qu

LeetCode记录之14——Longest Common Prefix

本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest common prefix string amongst an array of strings. 编写一个函数来查找字符串数组中最长的公共前缀字符串. 1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3

【LeetCode】14 - Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. Solution: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { //runtime:4ms 4 string str=""; 5 if(strs.empty())return

Java [leetcode 14] Longest Common Prefix

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

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个问题是关于如何寻找字符串数组的公共前缀的,从第一个字母开始,只要共有的都要输出. 思路: 1.首先,如何得到字符串共有前缀,既然共有,那么很容易先想到最短字符串. 2.最短字符串和其余进行比较,这样就省下了大于最短字符串长度的比较. 3.关于字符串的操作,length和null是很必要的判断输入操作,千万别忘记! 4.关

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

一.Description: Write a function to find the longest common prefix string amongst an array of strings. public class Solution { public String longestCommonPrefix(String[] strs) { } } 二.Solutions: 1.思路: 开始想的是数组中每两个相邻字符串进行比较,并取比较的最短的前缀作为整个数组的最短前缀.时间复杂度为