[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 ...
随机推荐
- ubuntu下python3虚拟环境的配置
安装相关包 sudo pip3 install virtualenv # 虚拟环境包 sudo pip3 install virtualenvwrapper # 虚拟环境管理包 创建虚拟环境目录 su ...
- PHP 模拟http 请求
php 模拟请求类 <?php /** * fangdasheng * http 模拟请求 */ class Myhttp { private $apiUrl; // 构造函数 public f ...
- day 01 常量 注释 int(整型) 用户交互input 流程控制语句if
python的编程语言分类(重点) if 3 > 2: 编译型: 将代码一次性全部编译成二进制,然后再执行. 优点:执行效率高. 缺点:开发效率低,不能跨平台. 代表语言:C 解释型: 逐行解释 ...
- linux:用户和组文件解释(/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow)
一.用户文件:/etc/passwd [root@pinfun6 ~]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash 1 2 3 4 5 6 7 | ...
- 如何使用前端分页框架bootstrap paginator
前端分页框架bootstrap paginator用于web前端页面快速实现美观大方的翻页功能.在实现交互良好的页面翻页功能时,往往还需要配合使用后端分页框架pagehelper.pagehelper ...
- numpy中的argsort()函数
在阅读<机器学习实战>一书中,发现了一个比较函数是argsort() 猜测是在numpy中出现的,手动进行了测试 >>> import numpy as np >& ...
- 配置 http 反向代理
[root@nginx ~]# cd /etc/nginx/ 1 [root@nginx nginx]# cp nginx.conf nginx.conf.bak #备份一个原配置文件 2 [root ...
- PAT Basic 1067 试密码 (20 分)
当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码(长度不超过 20 的.不包含空格. ...
- PAT Basic 1066 图像过滤 (15 分)
图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式: 输入在第一行给出一幅图像的分辨 ...
- PHP漏洞函数
1. is_numeric函数 bool is_numeric ( mixed $var ) 此函数用于判断变量是否数字或者数字字符串,不仅能接受十进制,还能接受十六进制,这样就有了绕过的方法 < ...