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. 很简单的一个描述,最 ...
随机推荐
- 什么是Docker??
Docker是一个轻量级虚拟机,也是一种Linux容器,它突破了以往的沙盒技术,解放了应用部署,让PaaS的应用场景更为广泛. docker是通过内核虚拟化技术((namespaces及cgr ...
- 【转】UML的9种图例解析
UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统中, ...
- 使用MBROSTool 工具制作U盘多启动盘的方法总结
前段时间写了一个自用五合一多启动盘分享--分别用来维护娱乐,wifi密码破解,win&mac登陆密码绕过/清除,反馈的同学还是挺多,觉得大家都有这方面的需求,于是再把自己的使用经验总结一下. ...
- 深度剖析PHP序列化和反序列化
序列化 序列化格式 在PHP中,序列化用于存储或传递 PHP 的值的过程中,同时不丢失其类型和结构. 序列化函数原型如下: string serialize ( mixed $value ) 先看下面 ...
- leetCode:461 汉明距离
汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 思路: 当看到"对应二进制位不同的位置的数目"这 ...
- 前端的UI设计与交互之反馈示篇
为了帮助用户了解应用当前要做什么,也给用户的下一步行为做参考,以及了解操作后所产生的结果 ,当用户和系统需要交互时,使用不同的模式来反馈信息或结果.当设计者使用反馈或者自定义一些反馈时,请注意:为用户 ...
- Webpack执行命令参数详解
一.概述前面的章节我们讲解了webpack的安装.webpack.config.js的 基本配置.webpack执行命名以及require方法的使用,不 知道大家有没有发现,当我们每次修改或者新增一个 ...
- shiro权限框架(一)
不知不觉接触shiro安全框架都快三个月了,这中间配合项目开发踩过无数的坑.现在回想总结下,也算是一种积累,一种分享.中间有不够完美的地方或者不好的地方,希望大家指出来能一起交流.在这里谢谢开涛老师的 ...
- JWT 简介
JWT是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以Json对象的形式安全的传递信息 ...
- RabbitMQ 通信过程
Rabbit MQ的通信过程 MQ全称为Message Queue, 是一种分布式应用程序的的通信方法,是消费-生产者模型的典型的代表,producer往消息队列中不断写入消息,而另一端consume ...