LeetCode & Q14-Longest Common Prefix-Easy
String
Description:
Write a function to find the longest common prefix string amongst an array of strings.
Example
For strings
"ABCD","ABEF"and"ACEF", the LCP is"A"For strings
"ABCDEFG","ABCEFG"and"ABCEFA", the LCP is"ABC"
测试用例想全面:
- 数组为空或为null
- 只有一个元素
- 全部完全匹配
- 匹配部分
my Solution:
public class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix = "";
if (strs.length == 1) return strs[0];
else if (strs.length > 1) {
prefix = strs[0];
int index = 0;
Integer[] temp = new Integer[strs.length - 1];
for (int i = 1; i < strs.length; i++) {
for (index = 0; index < prefix.length() && index < strs[i].length(); index++) {
if (prefix.charAt(index) != strs[i].charAt(index)) {
break;
}
}
temp[i - 1] = index;
}
index = Collections.min(Arrays.asList(temp));
return prefix.substring(0, index);
}
return prefix;
}
}
如何从数组中获得最大/最小数:
Collections.min(Arrays.asList(array));
如何跳出多层嵌套循环:
boolean flag = false;
for (; !flag; ) {
for (; ;) {
if (condition) {
flag = true;
break;
}
}
}
我的解法还是太复杂,没有考虑到indexOf的利用。
Best Solution:
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) return "";
String pre = strs[0];
int i = 1;
while(i < strs.length){
while(strs[i].indexOf(pre) != 0)
pre = pre.substring(0,pre.length()-1);
i++;
}
return pre;
}
LeetCode & Q14-Longest Common Prefix-Easy的更多相关文章
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- leetcode 题解 || Longest Common Prefix 问题
problem: Write a function to find the longest common prefix string amongst an array of strings. 寻找 0 ...
- [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. 解题思路: 老实遍历即可, ...
- 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. 很简单的一个描述,最 ...
随机推荐
- 24时区,GMT,UTC,DST,CST时间详解
全球24个时区的划分 相较于两地时间表,可以显示世界各时区时间和地名的世界时区表(World Time),就显得精密与复杂多了,通常世界时区表的表盘上会标示着全球24个时区的城市名称,但究竟 ...
- 【noip模拟】2048
Time limit: 1000ms Memory limits: 256MB Description 2048曾经是一款风靡全球的小游戏.今天,我们换一种方式来玩这个小游戏.现在, ...
- jsp学习第一弹
早期动态网站开发技术主要使用cgi技术,cgi的基本原理是,将浏览器提交至web服务器的数据通过环境变量传递给其他外部程序,经外部程序处理后,再由cgi把处理结果传送给web服务器,最后由web服务器 ...
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
写在前面 最近对文档库的知识点进行了整理,也就有了这篇文章,当时查找这些接口,并用在实践中,确实废了一些功夫,也为了让更多的人走更少的弯路. 系列文章 sharepoint环境安装过程中几点需要注意的 ...
- 设计模式——抽象工厂模式(C++实现)
#include <iostream> #include <string> using namespace std; class STAbstractProductA { pu ...
- 初探JodaTime
在学习java之初时就使用过jdk自带的java.util.Calendar ,近期的项目中需要达到类似功能的时候使用了JodaTime. Joda-Time 令时间和日期值变得易于管理.操作和理解. ...
- Mysql使用规范文档 20180223版
强制:不允许在跳板机上/生产服务器上手工连接,查询或更改线上数据 强制:所有上线脚本必须先在测试环境执行,验证通过以后方可在生产环境执行. 强制:上线脚本的编码格式统一为UTF-8 强制:访问数据库需 ...
- 1-6 hibernate映射集合属性
1.集合类框架 以Tree开头都是按顺序,默认情况下是升序排列. 以Linked 开头的都是按插入顺序排列的. 2.在hibernate中要持久化集合属性时必须将其声明为接口,如 private Se ...
- Http最常见的错误代码
1XX 表示消息 2XX 表示成功 3XX 表示重定向 4XX 表示请求错误 5XX 表示服务器端错误 我们最常见的就是: 404(页面找不到),这个错误代码是由于我们输入的网址不对造成的,浏览器找不 ...
- WEBLOGIC 11G (10.3.6) windows PSU 升级10.3.6.0.171017(Java 反序列化漏洞升级)
10.3.6版本的weblogic需要补丁到10.3.6.0.171017(2017年10月份的补丁,Java 反序列化漏洞升级),oracle官方建议至少打上2017年10月份补丁. 一.查看版本 ...