LeetCode14 Longest Common Prefix
题意:
Write a function to find the longest common prefix string amongst an array of strings. (Easy)
这两天实验室项目太忙了, 老板各种活,只能挑着先水几道easy题,这两个题是昨天做的没来得及写总结。
分析:
暴力的想法遍历比较一下就行,注意遍历的始末位置。优化的话改天再看看discuss
代码:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string result;
if (strs.size() == ) {
return result;
}
int minLength = 0x7FFFFFFF;
for (int i = ; i < strs.size(); ++i) {
int si = strs[i].size();
minLength = min(minLength, si);
}
for (int i = ; i < minLength; ++i) {
int flag = ;
for (int j = ; j < strs.size() - ; ++j) {
if (strs[j][i] != strs[j + ][i]) {
flag = ;
break;
}
}
if (flag == ) {
result += strs[][i];
}
if (flag == ) {
break;
}
}
return result;
}
};
8.6更新:
上面的还是不删除了,算法上这个简单题应该没啥优化的了,无非是另一种方法拿第一个字符串以此与后续求公共前缀;
但是!当初这个代码写的真是太长太丑了...有一些可以优化代码的地方,比如
1. flag其实没用,不满足情况就直接返回结果就行了;
2. 不要多走一遍求最小来作为遍历范围了,中间加一个判断语句就行;
3. 不用sting的+=,改用一个length最后再substr感觉效率上会高一点;(在leetcode跑的并没变化...)
更新后代码:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int length = ;
if (strs.size() == ) {
return "";
}
for (int i = ; i < strs[].size(); ++i) {
for (int j = ; j < strs.size() - ; ++j) {
if (strs[j].size() == i|| strs[j][i] != strs[j + ][i]) {
return strs[].substr(,length);
}
}
length++;
}
return strs[].substr(,length);
}
};
LeetCode14 Longest Common Prefix的更多相关文章
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- [Swift]LeetCode14. 最长公共前缀 | 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 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
随机推荐
- Cocos2d-JS v3.0 alpha 导入 cocostudio的ui配置
1.在新项目的根文件夹下打开project.json文件,修改: "modules" : ["cocos2d", "extensions", ...
- 第二百零七天 how can I坚持
都这么一大把年纪了,还没正事,哎.. mysql ifnull(expr,expr),oracle nvl();去null函数. 每天也没什么事. 哎,每天 也总有那么点事. 刘松.李承杰,都是些什么 ...
- cocos2d-x 3.2读取xml和json练习
读取和生成xml文件: #include "tinyxml2/tinyxml2.h" using namespace tinyxml2; void HelloWorld::make ...
- 【转】Java中只有按值传递,没有按引用传递!
原文链接:http://guhanjie.iteye.com/blog/1683637 今天,我在一本面试书上看到了关于java的一个参数传递的问题: 写道 java中对象作为参数传递给一个方法,到底 ...
- <转>linux进程间通信<一>
这篇文章真心不错,只是代码比较久,有些地方需求大家自行修改.先全文转载,以备复习只用.原文链接为:http://www.ibm.com/developerworks/cn/linux/l-ipc/pa ...
- LC并联谐振回路
- HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))
http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树 ...
- HDU 5884 Sort (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...
- Remove Duplicates from Sorted List @LeetCode
/** * Remove Duplicates from Sorted List * * Given a sorted linked list, delete all duplicates such ...
- Java数据类型简单认识
Java是一种强类型编程语言,因而在声明变量的时候必须声明数据类型,java语言有基本数据类型和引用数据类型这两大数据类型,基本数据类型有8种分别是4种整型.2种浮点类型.1种用于Unicode表示字 ...