68. Longest Common Prefix
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
思路:两两字符串挨着找。水题。
string commonPrefix(string s1, string s2){
if(s1 == "" || s2 == "") return "";
int k = 0, len1 = s1.length();
while(k < len1 && s1[k] == s2[k]) ++k;
return s1.substr(0, k);
}
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int len = strs.size();
if(len == 0) return "";
string s(strs[0]);
for(int i = 1; i < len; ++i){
if(s == "") return s;
s = commonPrefix(s, strs[i]);
}
return s;
}
};
68. Longest Common Prefix的更多相关文章
- [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 ...
- No.014:Longest Common Prefix
问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...
- No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
随机推荐
- Rhel6-cacti+nagios+ganglia(nginx)配置文档
(lnmp平台) 系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.185 server85.example.com 1 ...
- 人脸识别SDK小结
Face++人脸识别 进入官网 Face++ 致力于研发世界最好的人脸技术,提供免费的API和SDK供企业和开发者调用,更有灵活的定制化服务满足不同需求.已有多家公司使用Face++技术服务,完成包括 ...
- python3+任务计划实现的人人影视网站自动签到
python3+任务计划实现的人人影视网站自动签到 这是一个自动化程度较高的程序,运行本程序后会从chrome中读取cookies用于登录人人影视签到, 并且会自动添加一个windows 任务计划,这 ...
- 关于QGraphicsScene 和 QGraphicsView 和 QDialog 的杂乱笔记【或说指针复习。。】
LtCalibrateDlg::~LtCalibrateDlg() { if (m_pIplImageGray) cvReleaseImage(&m_pIplImageGray); MYDEL ...
- mysql插入数据后返回自增ID的方法
mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得到这个自增id的值呢? 方法一是使用la ...
- Qt + FFmpeg 本地音频播放器
http://pan.baidu.com/s/1hqoYXrI
- [Java Basics] Reflection
For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.C ...
- 百度之星热身赛-1001(dfs拓扑排序)
题意:作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机.这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的).有一天,这台计算机得到了n个任务要处理, ...
- 关于asp的运行环境配置
xp系统的配置方法http://jingyan.baidu.com/article/4f7d571289ac441a201927da.html win7系统的配置方法http://jingyan.ba ...
- Context的使用(转)
1.Context概念 Context,相信不管是第一天开发Android,还是开发Android的各种老鸟,对于Context的使用一定不陌生~~你在加载资源.启动一个新的Activity.获取系统 ...