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. python操作excel常用的方法

    读操作模块安装 pip install xlrd 写操作模块安装 pip install xlwt xlrd读操作 1.打开excel xl = xlrd.open_workbook('test.xl ...

  2. 视频直播技术(七):Ijkplayer切换网络时停止播放的问题处理

    问题起因: 在进行ijkplayer播放器的测试时,发现ijkplayer播放器在切换网络时出现直播画面停止的问题. 问题分析: 抓取日志发现:tv.danmaku.ijk.media.player. ...

  3. java枚举类型举例(基础)

    enum Mycolor{红色,绿色,蓝色}; public class asd { public static void main(String[] args) { Mycolor[] allcol ...

  4. Docker(二):Dockerfile 使用介绍

    上一篇文章Docker(一):Docker入门教程介绍了 Docker 基本概念,其中镜像.容器和 Dockerfile .我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dock ...

  5. DevExpress中GridControl自定义汇总列值(有选择性的汇总)

    今天碰到有同事遇到这个方面的需求,贴一下吧. private void gvTop_CustomSummaryCalculate(object sender, CustomSummaryEventAr ...

  6. ImageButton 图像按钮

    ImageButton 类主要成员有: setINMask 属性: 数据类型:Bool, {get ,set}. 用于确定是否接受用户输入操作,它的值是传给一个指针.这个指针指向的当前图像按钮所在的窗 ...

  7. adobe media encoder cc 2015在win10中打开崩溃的解决办法(该方法同样适用于adobe其他产品)

    今天就给大家讲讲adobe media encoder cc 2015启动的时候崩溃的问题,先来看看现象.就是这样了,然后我在网上找了很多办法,有的方法已经过时了,也或者因为现在新版本的adobe m ...

  8. 挂载U盘和移动硬盘

    1, 挂载U盘和USB接口的移动硬盘一样对linux系统而言U盘也是当作SCSI设备对待的.使用方法和移动硬盘完全一样.插入U盘之前[root at pldyrouter root]# fdisk - ...

  9. python 一篇就能理解函数基础

    一,函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过 ...

  10. MySQL数据库学习二 MSQL安装和配置

    2.1 下载和安装MySQL软件 2.1.1 基于客户端/服务器(C/S)的数据库管理系统 服务器:MySQL数据库管理系统 客户端:操作MySQL服务器 2.1.2 MySQL的各种版本 社区版(C ...