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"

测试用例想全面:

  1. 数组为空或为null
  2. 只有一个元素
  3. 全部完全匹配
  4. 匹配部分

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的更多相关文章

  1. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  2. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  3. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  4. leetcode 题解 || Longest Common Prefix 问题

    problem: Write a function to find the longest common prefix string amongst an array of strings. 寻找 0 ...

  5. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  6. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  7. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  8. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  9. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  10. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

随机推荐

  1. hibernate 反向生成 实体类

    1,配置数据库连接 步骤. 点击    windows   -> open perspective  - >   myeclipse datebase  Exprorer 打开了dateb ...

  2. Micropython TurnipBit 电子时钟 青少年编程入门

    电子时钟是一个很常用但是制作非常简单的小玩具了,对于Micropython初学者来说,制作一个电子时钟是非常简单又容易检验自己学习成果的实验了.TurnipBit相比于其他开发板,制作电子时钟就更加简 ...

  3. PHP 接口 返回构造函数

    我们写接口的时候,需要返回json数据,并且里面有错误码,错误信息 还有要返回的数据,这里我构造了一个函数 这里是针对TP5来写的,自己可以根据自己的框架来修改 这样就可以在返回的时候直接用info函 ...

  4. Oracle闪回恢复

    Oracle的闪回功能包括 1.闪回数据库(前提 归档模式下 启用闪回数据库) mount 下 alter database archivelog; alter database flashback ...

  5. webpack深入场景——开发环境和生产环境配置

    以前自己写一小项目时,webpack的配置基本就是一套配置,没有考虑生产环境和开发环境的区分,最近在做一个复杂的商城项目接触到了webpack的高级配置,经过两天的研究,写出了一份目前来说比叫满意的配 ...

  6. c++cout执行顺序之一个不容易注意到的一点

    二话不说,先看一个例子 #include <iostream> using namespace std; int main() { ]={,,,,,,,,,}; int *p=a; int ...

  7. jsp页面集成xhEditor文本编辑器

    经常写博客的都应该接触文本编辑器,现在大多数都是使用Markdown,Markdown是一种可以使用普通文本编辑器编写的标记语言,在文章中通过简单的语法标记就可以实现文字的不同格式,对于Markdow ...

  8. 给我一台全新的服务器,使用nginx+gunicorn+supervisor部署django

    0.准备工作 在一台全新的服务器中新建用户以及用户的工作目录,之后的操作都以这个用户的身份进行,而不是直接用root. 举个栗子: 在服务器下新建用户rinka并赋予sudo权限 1) root登陆, ...

  9. linux服务器中毒可疑进程sfewfesfs CPU80%

    我用的是wdlinux, 难免会有漏洞,不知怎么就被莫名其妙地给入侵了,而且还频繁发包.下面是我查看攻击机器的整个过程. 首先跟客户要了root密码登录看,第一个命令是就top cd /proc/25 ...

  10. java中equals与==的区别

    http://blog.csdn.net/zfrong/article/details/4290904