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 ...
随机推荐
- ssh传输文件
在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地 ...
- curl请求的时候总是提示400
今天用curl测试一个接口,一直提示400 最后发现是url的问题,如下处理就可以了 $url = str_replace(' ', '+', $url);
- ExtJs中gridpanel分组后组名排序
/** * 定义降序的groupingStore */ var DescGroupingStore = Ext.extend(Ext.data.GroupingStore, { groupDir : ...
- Android的学习——ubuntu下android5.1源码的make编译
在repo sync下载源码后,经历了漫长的时间,终于可以进行下一步了. 在进行make之前还需要三个步骤. 1> source build/envsetup.sh:加载命令 ...
- SpringMVC进阶
1.springmvc(注解版本) 注解扫描 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- jdk6提供的加密算法
SUN:SHA1PRNG____sun.security.provider.SecureRandomSUN:SHA1withDSA____sun.security.provider.DSA$SHA1w ...
- enmo_day_08
性能监视 管理内存组件 自动内存管理(AMM) : 指定分配给实例的总内存(SGA, PGA) 自动共享内存管理(ASMM) : 指定SGA, 管理分配给共享池, java池, 动态性能视图 :v$( ...
- WebView WebViewClient WebChromeClient
在android中,浏览器的功能分成几个部分,每个部分分工明确,互相协作.其中: 1. WebView :专门负责网页数据解析和渲染: 2. WebViewClient :帮助WebView处理各种请 ...
- C# .net windows服务启动多个服务 ServiceBase
在windows服务中想要启动多个服务 ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { // new SyncServ ...
- C++指针比较的问题
在C++里面,指针的比较是要保障type-safe的,也就是说,这两个指针必须是convertible的:从一个指针能够直接转换到另一个指针(有中间路径不算,不然都往void*转没完没了),顺序不限 ...