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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

Time: O(M * N)

class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int len = strs[0].length();
for (int i = 0; i < len; i++) {
char cur = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (i == strs[j].length() || strs[j].charAt(i) != cur) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}

[LC] 14. Longest Common Prefix的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 14. Longest Common Prefix【leetcode】

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  3. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. 14. Longest Common Prefix 最长的公共字符串开头

    [抄题]: Write a function to find the longest common prefix string amongst an array of strings. 在 " ...

  6. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

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

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  8. 14. Longest Common Prefix

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

  9. [LeetCode] 14. Longest Common Prefix

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

随机推荐

  1. Python说文解字_看起来有点儿像字典的元组(命名元祖)

    1. 需要一个库 namedtuple: 所谓命名元组就是对于元组的每一个元素进行起名,看起来很像访问字典一样. 实例 from collections import namedtuple Stock ...

  2. 《C++语言导学》小记

    我看的这本是Bjarne Stroustrup写的,南开大学的杨巨峰和王刚译的.这本书不适合初学者看,我就是大概翻了翻其中感兴趣的章节. 这本书第14章的标题是“历史和兼容性”,这节内容我看了收获很深 ...

  3. excel 导出长数据 变成科学计数 解决办法

    加 “\t”

  4. MyBatis从入门到精通(第4章):MyBatis动态SQL【foreach、bind、OGNL用法】

    (第4章):MyBatis动态SQL[foreach.bind.OGNL用法] 4.4 foreach 用法 SQL 语句中有时会使用 IN 关键字,例如 id in (1,2,3).可以使用 ${i ...

  5. i++ 和 ++i 的区别和实现

    ++i 和 i++ ++i 和 i++ 的区别 1)i++ 返回的是 i 的值,++i 返回的是 i+1 的值 2)i++ 不能用作左值,++i 可以用作左值 左值和右值的区别是什么? 根本区别是:能 ...

  6. 【模式分解】无损连接&保持函数依赖

    首先引入定义 无损分解指的是对关系模式分解时,原关系模型下任一合法的关系值在分解之后应能通过自然联接运算恢复起来.反之,则称为有损分解. 保持函数依赖的分解指的是对关系分解时,原关系的闭包与分解后关系 ...

  7. 初识API网关,API-gateway

    1.API-gateway(含义) 所有API的调用统一接入API网关层,由网关层负责接入和输出. API Gateway是一个服务器,也可以说是进入系统的唯一节点.这跟面向对象设计模式中的Facad ...

  8. 实践一次有趣的sql优化

    课程表 #课程表 create table Course( c_id int PRIMARY KEY, name varchar(10) ) 增加 100 条数据 #增加课程表100条数据 DROP ...

  9. 上传本地项目到GIT码云

    1.下载GIT 下载地址:https://git-scm.com/downloads 我这里下载的64位 2.安装GIT 双击下载的Git-2.18.0-64-bit.exe文件,选择自己的安装目录, ...

  10. js字符串相关要点

    不要创建string对象,它会拖慢执行速度,并可能产生其他副作用. var x = "John"; var y = new String("John"); (x ...