Question:

Longest Common Prefix

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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""

Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

我的解法思路是:

首先比较第一个和第二个字符的第i字母,如果第一个和第二个字符的第i个字母相同,则比较第二个和第三个的第i个字母,如果都相等,则将第i个字母加入最后的str中,i的大小从0开始,直到循环最后。

存在的问题是:

class Solution {
public String longestCommonPrefix(String[] strs) { int Strslength = strs.length; if(Strslength>1)
{ int minLength = strs[0].length();
//O(n)
for(String str:strs)
{
int strLength = str.length();
if(minLength > strLength)
{
minLength = strLength;
}
} //Store output ;
String str = ""; label:for(int i=0;i<minLength;i++)
{ for(int j=1;j<strs.length;j++)
{ if((strs[j-1].charAt(i))!=(strs[j].charAt(i)))
{ break label; } } str+= strs[0].charAt(i);
}
return str; }
else if(Strslength==1)
{
return strs[0];
}
else
{
return "";
} }
}

其他人的解法:

在这个解法中,作者主要通过找到两个string中相等的那个值,然后返回结果作为字串继续寻找使用indexOf函数,找到第一个相等的位置。

    public static String longestCommonPrefix(String[] strs) {
if(strs.length==0){return "";}
String prefix = strs[0];
for(int i=1;i<strs.length;i++)
{
while (strs[i].indexOf(prefix)!=0)
{
prefix= prefix.substring(0,prefix.length()-1);
}
} return prefix;
}

Leetcode练习题Longest Common Prefix的更多相关文章

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

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

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

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

  3. 【leetcode】Longest Common Prefix

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

  4. [LeetCode] 14. Longest Common Prefix

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

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

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

  6. 【leetcode】Longest Common Prefix (easy)

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

  7. Java [leetcode 14] Longest Common Prefix

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

  8. Leetcode 14——Longest Common Prefix

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

  9. LeetCode(54)-Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...

随机推荐

  1. C编程小结1

    1. ‘\0’表示字符串结束符 2. 变量之间互相赋值一定要考虑他们的数据类型,要强制转换匹配上了或者进行一些处理才能赋值,同时读程序的时候也要注意这一点,否则可能看不懂.如: sData[0]=wD ...

  2. 原创【cocos2d-x】CCMenuItemToggle 在lua中的使用

    说明:1,所使用的cocos2dx版本为2.1.3 ;09:48:05 2,本人仍是在学习中的小菜鸟,此博客只是为了记录我学习过程中的点滴,同时也希望同样lua开发的童鞋,一起交流: 3,本人whj0 ...

  3. SpringBoot条件注解的总结

    https://blog.csdn.net/qq_31142553/article/details/86439950

  4. LeetCode 219: 存在重复元素 II Contains Duplicate II

    题目: ​ 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. ​ Given an ...

  5. 织女星开发板RISC-V内核实现微秒级精确延时

    前言 收到VEGA织女星开发板也有一段时间了,好久没玩了,想驱动个OLED屏,但是首先要实现IIC协议,而实现IIC协议,最基本的就是需要一个精确的延时函数,所以研究了一下如何来写一个精确的延时函数. ...

  6. NFS文件系统及搭建NFS共享服务

    一.什么是文件系统? 文件系统是对一个存储设备上的数据和元数据进行组织的一种机制.文件系统是在一个磁盘(包括光盘.软盘.闪盘及其它存储设备)或分区上组织文件方式方法,常见文件系统如ext2.ext3. ...

  7. 为什么 Go 标准库中有些函数只有签名,没有函数体?

    如果你看过 Go 语言标准库,应该有见到过,有一些函数只有签名,没有函数体.你有没有感觉到很奇怪?这到底是怎么回事?我们自己可以这么做吗?本文就来解密它. 首先,函数肯定得有实现,没有函数体,一定是在 ...

  8. C#深入浅出之数据类型

    基本数据类型        C#支持完整的BCL(基类库)名字,但是最好都统一使用关键字进行使用与开发,比如使用int而不是System.Int32,以及使用string类型时候应当使用string而 ...

  9. 爬虫最新的库requests-html库总结

    requests-html是比较新的爬虫库,作者和requests是同一个作者 一.安装依赖 pip install requests-html 我们可以在安装的时候看到他安装了lxml,reuqes ...

  10. MyBatis框架之第二篇

    1.高级参数映射和返回值映射(重点) a)Pojo包装pojo的参数映射 b)当结果集列名与pojo属性名不一致的返回值映射 2.动态sql(重点) 3.关联查询结果(重点) a)一对一关联结果 b) ...