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的更多相关文章

  1. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  3. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  4. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

  5. 14. Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...

  6. Leetcode Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  7. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  8. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...

  9. No.014 Longest Common Prefix

    14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...

随机推荐

  1. ES6 基础版迭代器

    ES6中引入了generator function* get() { var result1 = yield c; var result2 = yield b; var result3 = yield ...

  2. Java正则匹配数字

    包括5种形式,如测试结果 import java.util.Scanner; import java.util.regex.*; public class Com { public static vo ...

  3. mysql命令行创建存储过程命令行定时执行sql语句

    mysql -uroot -p show databases; use scm; show tables; show procedure status; 其他命令: SHOW VARIABLES LI ...

  4. CentOS6.4安装Hadoop2.0.5 alpha - Single Node Cluster

    1.安装JDK7 rpm到/usr/java/jdk1.7.0_40,并建立软链接/usr/java/default到/usr/java/jdk1.7.0_40 [root@server-308 ~] ...

  5. Libgdx 开发指南(1.3) 应用框架——查询、日志

    查询 Application接口提过多种方法查询运行时环境属性. 获得应用类型 有时候根据运行平台需要处理一些具体的逻辑,可以使用 Application.getType() 方法来返回应用所运行的平 ...

  6. springboot系列之-log

    配置文件以application.yml为例说明: Spring Boot默认的日志组件为Logback. 一. 日志配置参数: logging: file: #日志文件,绝对路径或相对路径 path ...

  7. redis windows

    下载 redis windows版本 redis-server  redis.windows.conf  //cmd 命令 启动成功 E:\redis\Redis-x64-3.0.500>red ...

  8. 论文笔记之:Hybrid computing using a neural network with dynamic external memory

    Hybrid computing using a neural network with dynamic external memory Nature  2016 原文链接:http://www.na ...

  9. 盯盯拍Android App 3.0指导

    http://www.ddpai.com/bbs/thread-233-1-1.html 视频介绍:http://v.17173.com/v_102_604/MzA0OTAwMDg.html

  10. jquery选择器之内容选择器

    HTML示例代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...