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. hadoop cmd

    一.hadoop文件操作 1.Ls hadoop fs -ls / 2.Put hadoop fs -put xx /path 3.Mkdir hadoop fs -mkdir 4.要从HDFS中删除 ...

  2. STM32重映射

  3. C++概要简介

    从C到C++ 新类型 bool类型 新的输入输出方式 con cout 新的内存存储方式 new delete 引用& 用于传参 函数 内敛函数inline 通过代码区膨胀 减少函数的跳转时间 ...

  4. ZJNU 1160 - 不要62——中级

    取模判断,数组模拟 /* Written By StelaYuri */ #include<stdio.h> ]; int main(){ int n,m,i,s,t; ;i<;i+ ...

  5. \_\_del\_\_

    __del__ 一.__del__ __del__也称之为析构方法 __del__会在对象被删除之前自动触发 print('主')class People: def __init__(self, na ...

  6. JS 判断移动端与PC端

    js判断移动端与pc端   这里介绍下使用device.js插件来判断移动端设备 地址:https://github.com/matthewhudson/device.js 示例: 1 2 3 4 5 ...

  7. 学习spring第四天

    Spring第四天讲义 今日内容 Spring的事务管理 Spring和MyBatis框架的集成 1. Spring的事务管理 1.1. 事务是什么? 在操作数据库时(增删改),如果同时操作多次数据, ...

  8. python 3.6

    安装了最新版anaconda3-4.3 发现jupyter-notebook 少了一些东西.需要手工安装 https://github.com/Anaconda-Platform/nbpresent

  9. matlab设置mex失败

    更新 使用matlab2017b时,又报错, >> mex -setup 警告: Xcode is installed, but its license has not been acce ...

  10. Python字符串与列表