Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)
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++实现)的更多相关文章
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
 - [LeetCode]14. Longest Common Prefix最长公共前缀
		
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
 - [leetcode]14. Longest Common Prefix 最长公共前缀
		
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
 - [LeetCode] 14. Longest Common Prefix 最长共同前缀
		
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
 - # Leetcode 14:Longest Common Prefix 最长公共前缀
		
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
 - 【LeetCode】14. Longest Common Prefix 最长前缀子串
		
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
 - 【LeetCode】Longest Common Prefix(最长公共前缀)
		
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
 - LeetCode Longest Common Prefix 最长公共前缀
		
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
 - Longest Common Prefix -最长公共前缀
		
问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...
 
随机推荐
- TensorFlow csv读取文件数据(代码实现)
			
TensorFlow csv读取文件数据(代码实现) 大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 Tens ...
 - HAL库|神器cubemx的正确打开方式
			
前言 工欲善其事,必先利其器.HAL库的开发不一定必须使用cubemx,但是使用了cubemx,你绝对不会后悔.基于一些小伙伴对cubemx的使用还有一些疑问,本次小飞哥从新建工程到生成工程,编写应用 ...
 - Python“九九乘法表”
			
用Python语言编程,使用双重循环语句输出"九九乘法表". for i in range(1, 10): # 控制行 for j in range(1, i+1): # 控制列 ...
 - Python爬虫入门:Urllib parse库使用详解(二)
			
文字转载:https://www.jianshu.com/p/e4a9e64082ef,转载内容仅供学习 如有侵权,请联系删除 获取url参数 urlparse 和 parse_qs ParseRes ...
 - 【NX二次开发】拉伸面、拉伸封闭曲线成片体UF_MODL_create_extrusion
			
用那几个拉伸,不能将封闭的曲线拉伸成片体,用UF_MODL_create_extrusion函数是可以的,下面是例子. 帮助上说拉伸片体不能有拔模角度,应该是错误的,下面是一个封闭曲线带拔模角度拉伸片 ...
 - CDQ分治(三维偏序集)
			
排序,三关键字 去重 归并排序+树状数组 #include<bits/stdc++.h> using namespace std; #define re register int cons ...
 - CRM系统个性化定制的对企业的优势作用
			
伴随着科学技术的不断发展,企业信息化建设也在持续地开展.企业管理模式已经开始由传统模式向信息化转变,并且越来越多的企业开始使用互联网软件来进行辅助管理,这一趋势也让CRM客户管理系统得到快速的发展.市 ...
 - Docker | Docker常用命令学习笔记
			
@ 目录 前言 1. 帮助命令: version.info.help 2. 镜像命令: images.search pull.rmi 3. 容器命令: pull.run ps.exit .ctrl+P ...
 - XML:No operation was found with the name报错解决办法
			
当我们使用CXF动态客户端调用WebService接口容易出现如下问题:命名空间问题 Exception in thread "main" org.apache.cxf.commo ...
 - hsdis反汇编java源码工具的使用方法
			
下载地址: hsdis linux下将hsdis.so拷贝到 /usr/lib/jvm/java-11-openjdk-11.0.7.10-4.el7_8.x86_64/lib/server 目录下( ...