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 ...
随机推荐
- forms
http://www.cnblogs.com/bomo/p/3309766.html http://www.cnblogs.com/leonwang/archive/2013/03/05/294457 ...
- 非阻塞同步机制与CAS操作
锁的劣势 Java在JDK1.5之前都是靠synchronized关键字保证同步的,这种通过使用一致的锁定协议来协调对共享状态的访问,可以确保无论哪个线程 持有守护变量的锁,都采用独占的方式来访问这些 ...
- WEB数据挖掘(十六)——Aperture数据抽取(9):数据源
One of the central concepts of Aperture is the notion of a DataSource. A DataSource contains all inf ...
- opencv直方图均衡化
#include <iostream> #include "highgui.h" #include "cv.h" #include "cx ...
- Routed Events【pluralsight】
Routing Strategies: Direct Bubbling Tunneling WHy use them? Any UIElement can be a listener Common h ...
- InnoDB存储引擎
[InnoDB和MyISAM区别][ http://jeck2046.blog.51cto.com/184478/90499] InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型, ...
- WCF 客户端调用几种方式
http://www..com/html/blogs/20130413/2116.htm CSDN123 http://developer.51cto.com/art/200911/161465.ht ...
- Oracle用户、权限、角色管理
Oracle 权限设置一.权限分类:系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对表或视图而言的). 二.系统权 ...
- Jquery 提示
1 文字提示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- Tomcat创建虚拟目录和程序热部署
虚拟目录的设置 方法一:在${tomcat安装目录}/conf/Catalina/localhost目录下添加与web应用同名的xml配置文件,这里站点名称为test为例子. test.xml内容:& ...