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 ...
随机推荐
- 微信第三方 授权方账户信息 API文档错误
获取授权方的账户信息 该API用于获取授权方的公众号基本信息,包括头像.昵称.帐号类型.认证类型.微信号.原始ID和二维码图片URL. 需要特别记录授权方的帐号类型,在消息及事件推送时,对于不具备客服 ...
- 一些sql语句的常用总结(重要)
select primary_flag from tc_contact where primary_flag !=0 select dept_id,dept_name,tree_level,tree_ ...
- Xcode 设置 ARC&MRC混用
如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签.如果你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标 ...
- Linux线程-互斥锁pthread_mutex_t
在线程实际运行过程中,我们经常需要多个线程保持同步.这时可以用互斥锁来完成任务:互斥锁的使用过程中,主要有pthread_mutex_init,pthread_mutex_destory,pthrea ...
- Day19_IO第一天
1.异常 1.概念 程序出现不正常的情况 2.异常体系(掌握) Throwable |-Error ...
- Day17_集合第三天
1.HashSet类(掌握) 1.哈希值概念 哈希值:哈希值就是调用对象的hashCode()方法后返回的一个int型数字 哈希桶:简单点理解就是存储相同哈希值对象的一个容器 1. ...
- java 读写JSON(一)
算是第一次正式接触Json,没有深入研究,先贴上java的代码,日后才说! package priv.chenhy.datehandle; import java.io.BufferedReader; ...
- LintCode First Position of Target
找指定target的最左位置. class Solution { /** * @param nums: The integer array. * @param target: Target to fi ...
- Extjs控制面板组件
(1)aoolyTo:(id) renderTo:(id)呈现在哪个html里面,同上 id最好用"" contentEI:() 呈现哪个html元素里面,把eI内的内容呈现 ( ...
- LeetCode 【46. Permutations】
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...