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. 很简单的一个描述,最 ...
随机推荐
- Python魔法方法(转发整合)
如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而这一切都是自动发生的. __new__: 是一个对象实例化时调用的第一 ...
- 微信小程序之实现页面缩放式侧滑效果
效果图: 实现原理:点击按钮,往需要动画的div中添加或移除拥有动画效果的class. 由于微信小程序中不能操作page这个根节点,所以,只有用一个div(view)来模仿page根节点. 1.结构 ...
- .net framework 4.5 +steeltoe+ springcloud 实现服务注册功能
首先得先了解并熟悉一下springcloud,并手动去搭建一个服务中心,具体可度娘教程. 如果是.net core的话,实现注册也是没有问题的,网上教程很多,可自行度娘. 最难的就是基于Framewo ...
- Hadoop3.0完全分布式集群安装部署
1. 配置为1个namenode(master主机),2个datanode(slave1主机+slave2主机)的hadoop集群模式, 在VMWare中构建3台运行Ubuntu的机器作为服务器: 关 ...
- 洛谷 P1146 【硬币翻转】题解
很久很久之前做过的一道题 翻n-1枚硬币,就是有一枚不翻,也可以理解为翻一枚 直接上程序,看程序说话 #include<iostream> using namespace std; ; b ...
- 在Vue.js2.0中组件模板子元素数量问题
在Vue中当利用组件进行开发时候,组件所使用的模板只可以应用于一个根实例,当你需要添加多个子元素的时候,可以用一个div将它们包裹起来,代码如下: <template id="task ...
- 数据定义: CREATE、DROP、ALTER
CREATE DATABASE 句法 CREATE DATABASE [IF NOT EXISTS] db_name 数据库.表.索引.列和别名 中被给出. 如果数据库已经存在,并且你没有指定 IF ...
- python3.6.4 tkinter安装
在pyenv虚拟环境中 sudo yum -y install tkinter tcl-devel tk-devel 重新安装python pyenv install -v 3.6.4
- Python基础-week04
本节内容摘要:#Author:http://www.cnblogs.com/Jame-mei 装饰器 迭代器&生成器 Json & pickle 数据序列化 软件目录结构规范 作业:A ...
- IPFS的竞争对手们(一)
IPFS的竞争对手 IPFS这个项目真的开发很慢,相比其它区块链项目,IPFS的进度可真是让小编捉急,恨铁不成钢啊.然而小编仍然对他们充满信心,来,借用一句盗梦空间里的经典台词: 既然做梦,那就做大点 ...