[LC] 14. Longest Common Prefix
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的更多相关文章
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- 14. Longest Common Prefix 最长的公共字符串开头
[抄题]: Write a function to find the longest common prefix string amongst an array of strings. 在 " ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
随机推荐
- JavaScript—飞机大战2版
前面的思路对了 BUG 出在了计时器和没有加判断页面是否存在元素 <!DOCTYPE html> <html lang="en"> <head> ...
- Django2.0——实现简易登陆、注册
思路: 实现简易的登陆.注册,我们至少需要三个HTML页面,一个主页面.一个登陆界面.一个注册界面.为了存储和校验用户的账号和密码,我们需要写一个模型类(用于映射到数据库).两个form类(一个登陆. ...
- tomcat配置配置文件和war包进行分离
应用部署 war包.配置文件分离 部署主机路径规划以及tomcat中间件改造 1.新建存放war包路径 /appsystems/apps 将war包放置其中 2.新建存放配置文件路径 /apps ...
- h5-边框图片
1.边框图片详解 <style> *{ ; ; } div{ width: 900px; height: 900px; margin: 100px auto; border: 133px ...
- Log4Net 使用及组合公共类
好记性不如烂笔头,这次是由衷的感受到了! log4net 是一个很好用的日志记录工具,引用入项目中,如何查看项目内部运行情况,如何快速定位异常信息,好的日志记录能帮很大的忙: log4net 很好用, ...
- psmisc
https://packages.ubuntu.com/xenial/psmisc Package: psmisc (22.21-2.1build1) Other Packages Related t ...
- 关于gc中对象回收算法的认识
之前学习java时,笔者看到很多学习资料说,gc判断object存活与否的算法是:给对象添加一个引用计数器,每当有一处地方引用它时,计数器值就加1,当引用失效时,计数器值就减1,当对象计数清零时,对象 ...
- 吴裕雄--天生自然 PHP开发学习:数据库 ODBC
<html> <body> <?php $conn=odbc_connect('northwind','',''); if (!$conn) { exit("连 ...
- Mariadb-10.2.25 多实例
Mariadb-10.2.25 多实例 定义目录 mkdir -p /mysql/{3306,3307,3308}/{bin,data,etc,log,pid,socket} 生成数据库文件 /app ...
- 我是如何从Java转型为Go区块链工程师
我是如何从Java转型为Go区块链工程师 本文来自于一个比原链核心开发的陈述 前言 IT部落在加入比原链之前一直是做Java开发的,当初看到Go还有点犹豫,还怕过不了面试,结果是否掌握一门语言的考量确 ...