Longest Common Prefix -最长公共前缀
问题:链接
Write a function to find the longest common prefix string amongst an array of strings.
解答:
注意 当传入參数为空,即vector<string> 大小为0时。应该直接返回一个空字符串“”。而不是返回NULL。
这点须要特别注意。
代码:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.size() == 0)
return "";
int i = 0;
char a;
while(1)
{
if(i >= (*strs.begin()).size())
return strs[0].substr(0,i);
a = (*strs.begin())[i];
for(vector<string>::iterator it = strs.begin()+1; it != strs.end(); ++it)
{
if(i >= (*it).size() || a != (*it)[i] )
return strs[0].substr(0,i);
}
++i;
}
}
};
Longest Common Prefix -最长公共前缀的更多相关文章
- [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 No.14 Longest Common Prefix最长公共前缀(c++实现)
1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- [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】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- VS2015 解决方案 或者项目 卡 正在加载 的解决办法
导致项目无法打开以及VS无法关闭. 解决方法: 1.关闭VS: 2.去C:\Users\<your users name>\AppData\Local\Microsoft\VisualSt ...
- VS代码注释插件GhostDoc
http://community.submain.com/blogs/tutorials/archive/2013/03/28/how-to-access-ghostdoc-pro-features. ...
- Centos 7 部署FTP服务简单版
第三方教程推荐与参考: http://blog.csdn.net/somehow1002/article/details/70232791 先安装成功了,有信心了.再进一步扩展配置. 1.安装vsft ...
- Linux操作系统桌面环境GNOME和KDE的切换
一.设置GNOME或者KDE为默认的启动桌面环境 方法1:修改/etc/sysconfig/desktop,根据需要将“DESKTOP”后面的内容改为KDE或GNOME. 方法2:在当前用户目录下建立 ...
- Codeforces Round #277 (Div. 2)---A. Calculating Function (规律)
Calculating Function time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 深入浅出--iOS的TCP/IP协议族剖析&&Socket
深入浅出--iOS的TCP/IP协议族剖析&&Socket 简介 该篇文章主要回顾--TCP/IP协议族中的TCP/UDP.HTTP:还有Socket.(--该文很干,酝酿了许久! ...
- python SimpleHTTPServer源码学习
SimpleHTTPServer.SimpleHTTPRequestHandler继承了BaseHTTPServer.BaseHTTPRequestHandler. 源码中主要实现了BaseHTTPS ...
- deque双端队列用法
#include <iostream> #include <cstdio> #include <deque> #include <algorithm> ...
- java String去除两端的空格和空字符
java中String有个trim()能够去掉一个字符串的前后空格.但是trim()只能去掉字符串中前后的半角空格,而无法去掉全角空格.去掉全角空格需要在trim()方法的基础上加上一些判断.Stri ...
- hdu 4704(费马小定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704 思路:一道整数划分题目,不难推出公式:2^(n-1),根据费马小定理:(2,MOD)互质,则2^ ...