1. 题目

1.1 英文题目

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

1.2 中文题目

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

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

1.3输入输出

输入 输出
strs = ["flower","flow","flight"] "fl"
strs = ["dog","racecar","car"] ""

1.4 约束条件

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lower-case English letters.

2. 分析

2.1 一般方法

看到这道题,最简单的方法就是两层for循环,最外层是对第一个字符串进行遍历(其实第几个字符串都无所谓),内层是对字符数组进行遍历,意思也就是先看所有字符串第一个元素是否都一样,再看第二个,第三个,以此类推,当遇到有不同的时候,就可以跳出外层循环。代码如下:

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans;
for (unsigned int i = 0; i < strs[0].size(); i++)
{
unsigned int count = 0;
for (unsigned int j = 0; j < strs.size(); j++)
{
char temp = strs[0][i];
if (i != strs[j].size() && strs[j][i] == temp)//
count++;
else
goto here;
if (count == strs.size())
ans += strs[0][i];
}
}
here:
return ans;
}
};

2.2 改进算法(贼好使)

上述算法使用goto跳出两层循环,而goto的使用很多人是不太推荐使用的,同时联想到for循环的第二项就是一个判断语句,因此可以将goto判断语句改到for循环里,具体代码如下:

class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans;
for (unsigned int i = 0; i < strs[0].size(); i++)
{
unsigned int count = 1;
unsigned int j = 1;
int temp = strs[0][i];
for (; j < strs.size() && i != strs[j].size() && strs[j][i] == temp; j++)
count++;
if (count == strs.size())
ans += strs[0][i];
else
break;
}
return ans;
}
};

这个算法时间消耗0ms,空间消耗9M,非常不错!

Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)的更多相关文章

  1. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

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

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

  3. [leetcode]14. Longest Common Prefix 最长公共前缀

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

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

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

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

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  6. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  7. 【LeetCode】Longest Common Prefix(最长公共前缀)

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  8. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  9. Longest Common Prefix -最长公共前缀

    问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...

随机推荐

  1. GPU虚拟化技术详解

    GPU虚拟化技术详解 GPU英文名称为Graphic Processing Unit,GPU中文全称为计算机图形处理器,1999年由NVIDIA公司提出. 一.GPU概述 GPU这一概念也是相对于计算 ...

  2. springboot——发送put、delete请求

    在springmvc中我们要发送put和delete请求,需要先配置一个过滤器HiddenHttpMethodFilter,而springboot中,已经帮我们自动配置了,所以我们可以不用配置这个过滤 ...

  3. XML文件存在中文注释报错问题( 3 字节的 UTF-8 序列的字节 3 无效)

    今天在做mybatis项目的时候,给映射文件添加了中文注释后,程序就报错.把中文注释删除后,程序又可以正常执行.解决方法在下文提到. 我的xml映射文件如下: <?xml version=&qu ...

  4. python取整函数 向上取整 向下取整 四舍五入

    向上取整 >>> import math >>> math.ceil(3.5) 4 >>> math.ceil(3.4) 4 >>&g ...

  5. 【NX二次开发】Block UI 选择对象

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...

  6. 分分钟教你Python Web开发框架Django

    Python除了爬虫.深度学习(人工智能).数据分析等外,还可以用来开发网站系统,如我们常见的知乎,豆瓣等都是用Python开发的网站系统. 今天辰哥就来教大家如何新建属于自己的Django项目,让D ...

  7. springcloud webflux

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  8. 01-ubuntu18.04安装docker脚本[含自动配置apt]

    01-ubuntu18.04安装docker脚本[含自动配置apt] 脚本一键安装docker,并配置阿里云的docker镜像加速. #!/bin/bash #更新apt源为清华源 echo &quo ...

  9. 自定义组件开发:使用v-model封装el-pagination组件

    1.前言 通过封装el-pagination组件开发自定义分页组件的类似文章网上已经有很多了,但看了一圈,总是不如意,于是决定还是自己动手搞一个. 2.背景 2.1.常规分页处理方法 利用el-pag ...

  10. js 动态设置 div 背景图片 并滚动显示

    var imgs =["../img/index/bgstyle/style1/index_top_bg2.jpg", "../img/index/bgstyle/sty ...