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

如果不存在公共前缀,返回空字符串 ""。

示例 1:

输入: ["flower","flow","flight"]
输出: "fl"

示例 2:

输入: ["dog","racecar","car"]
输出: ""

解释: 输入不存在公共前缀。
说明:

所有输入只包含小写字母 a-z 。

思路:

最后的公共前缀肯定不会超过任何一个字符串的长度;

暴力法:直接依次比较第一个字符串和其他字符串的同一位置的字符。

#include<iostream>
#include<vector>
#include<string>
using namespace std; string longestCommonPrefix(vector<string>& strs) { if (strs.size() == )
{
return "";
}
if (strs.size() == )
{
return strs[];
}
int len = strs.size();
for (int i = ; i < strs[].length(); i++)
{
for (int j = ; j < len; j++)
{
if ((i >= strs[j].length()) || strs[j][i] != strs[][i])
{
return strs[].substr(, i);
}
}
}
return strs[];
} int main(){
vector<string> a = { "jieiuye", "jian", "j" };
string ans = longestCommonPrefix(a);
cout <<ans << endl;
return ;
}

#leetcode刷题之路14-最长公共前缀的更多相关文章

  1. leecode刷题(19)-- 最长公共前缀

    leecode刷题(19)-- 最长公共前缀 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: [&quo ...

  2. 【Leetcode】【简单】【14最长公共前缀】【JavaScript】

    题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

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

    14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...

  4. Java实现 LeetCode 14 最长公共前缀

    14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...

  5. python刷LeetCode:14. 最长公共前缀

    难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  6. Leetcode题库——14.最长公共前缀

    @author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...

  7. [LeetCode]14.最长公共前缀(Java)

    原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...

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

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  9. 【LeetCode】14. 最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1:输入: ["flower","flow&quo ...

随机推荐

  1. for 循环的时候 append() 是移动不是复制

    使用for 的时候,append() 不是复制,而是移动,只有最后一个元素才真正的append() 到了 解决办法: 1. 使用字符串: 2.使用clone();

  2. Python基础-月考

    1. 8<<2等于? # 解释:将8按位左移2位 # 8 0 0 0 0 1 0 0 0 # 32 0 0 1 0 0 0 0 0 2. 通过内置函数计算5除以2的余数 print(div ...

  3. SSRS 2008R2 执行Log 查询

    1. 可以参考ExecutionLog3试图,此为系统安装Reporting Service自带的试图. 2. 可以使用以下语句查询: SELECT els.LogEntryId, els.Insta ...

  4. 解决Maven 报 Return code is: 400 , ReasonPhrase:Repository version policy: SNAPSHOT does not allow version: 2.1.0.RELEASE. 的错误

    最近在搭建公司的基础框架,业务需求用到elasticsearch,所以需要整合到基础框架里,供各业务线使用同时也便于管理,但在整合的过程中,出现了莫名的问题,同时maven的提示也不够明确. 我的版本 ...

  5. maven升级遇到的疑惑

    今天在解决一个问题的时候,由于需要修改maven的client包,按照之前的办法,修改完之后,没有修改版本号,而是直接修改client的代码,之后直接 mvn deploy -e 打包上去了,然后奇怪 ...

  6. ArcGIS 10.1 如何连接数据库(转)

    原文地址:http://blog.csdn.net/arcgis_cs/article/details/7750893 ArcGIS 10.1如何连接数据库 最近在使用ArcGIS 10.1的数据库, ...

  7. c# 依赖注入之---setterInjection(转)

    图3.1 Setter注入示意 定义一个接口: namespace SetterInjection { internal interface IServiceClass { string Servic ...

  8. c# 控制台输出txt文件

    string tempFileName = "DETAIL_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ...

  9. Linux 内核超时导致虚拟机无法正常启动

    问题描述 当 Linux 虚拟机启动时,通过串口输出或者启动日志, 观察到超时的报错.导致虚拟机无法正常启动和连接. 问题分析 常见的超时报错范例如下: 复制 INFO: task swapper:1 ...

  10. C++中虚继承的作用及底层实现原理

    http://blog.csdn.net/bxw1992/article/details/77726390