[LeetCode]-011-Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
[]
=>""
["abcweed","htgdabc","sabcrf"]
=>""
["abcweed","abhtgdc","abacrf"]
=>"ab"
题目大意:求字符串数组的最长子前缀
public class Solution{
public String longestCommonPrefix(String[] strs) {
//System.out.println(Arrays.toString(strs));
if(strs.length == 0)
return "";
int min = Integer.MAX_VALUE;
for(String s : strs){
if(min>s.length())
min = s.length();
}
if(min==0)
return "";
//System.out.println(min);
String prefix = "", tmp = "";
int i=0;
for( ; i<min; i++){
prefix = strs[0].substring(0,(i+1));
//System.out.println("prefix=" + prefix);
for(int j=1; j<strs.length; j++){
tmp = strs[j].substring(0,(i+1));
//System.out.println("tmp=" + tmp);
if(!prefix.equals(tmp))
return strs[0].substring(0,(i));
}
}
System.out.println("i=" + strs[0].substring(0,(i)));
return "";
}
public static void main(String[] args){
String[] arr = {"","asdffg","aswd"};
if(args.length!=0)
arr = args;
Solution solution = new Solution();
String res = solution.longestCommonPrefix(arr);
System.out.println(res);
}
}
[LeetCode]-011-Longest Common Prefix的更多相关文章
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...
- Leetcode 14——Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...
- LeetCode(54)-Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...
- [leetcode]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- PythonDay10
第十章函数进阶 今日内容 函数的参数 动态参数 动态接收位置参数 动态接收关键字参数 函数的注释 名称空间 函数的嵌套 global.nonlocal global的宗旨 nonlocal宗旨 1.函 ...
- [.net core]11.异常页
.net core中的异常页很重要 因为可以查看异常的堆栈信息, 请求的参数(如果有),cookie, http头 帮助我们快速的定位问题 .net core web app 默认开启了异常页,但是 ...
- TypeError: esri.layers.WMSLayer is not a constructor
最近加载wms地图后,总是报这个错误,因为错误,导致后续的代码无法加载,导致无法功能使用. 原因是,由于方法公用,有的新功能在使用时,引用依赖包时,未引用完整,导致加载此处加载wms图层的时候, 报此 ...
- linux 配置环境变量三种方式
一:用于当前终端: export PATH=$PATH:<你的要加入的路径> 此方式仅用于当前终端,一旦该终端关闭,则配置失效 二:用于当前用户: vi ~/.bashrc 然后加入:ex ...
- Linux上安装JDK1.8,tomcat9,以及mysql8的步骤
(该篇是在centos7上安装JDK1.8.0_201 tomcat9.0.16 和 mysql8.0.15) 一.安装JDK 方式一 1.首先,下载JDK(链接http://www.oracle. ...
- 一道有关#define的题
题目是:查看以下代码,问结果是什么? 结果是打印出“array:16345678910”吗? #include "stdafx.h" #include <iostream&g ...
- Windows PyCharm QtDesigner/pyuic5配置
QtDesigner 配置成功截图如下: C:\ProgramData\Anaconda3\Library\bin\designer.exe $FileDir$ pyuic5 配置成功截图如下: C: ...
- install - 复制文件并设置属性
SYNOPSIS[总览] install [options] [-s] [--strip] source dest install [options] [-s] [--strip] source... ...
- MongoDB入门_shell基本操作
使用shell客户端连接mongoDB数据库 [root@localhost mongodb_simple]# ./bin/mongo /admin mongoDB的简单基本操作 1. mongoDB ...
- h5 实现页面上拉加载更多数据
您好,您的上拉加载更多的代码来喽: html:(style部分:html部分:js部分--js部分主要就是监控上拉,调接口) //上拉加载更多css <style> .wait-loadi ...