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. hadoop搭建的前期准备

    这个hadoop的搭建是以比赛前的练习为目的的,所以我直接以root用户来搭建hadoop,主要也是方便我自己以后复习用的 需要的软件:vmware15.5,xshell6,xftp6,jdk Lin ...

  2. Googleplaystore数据分析

    本次所用到的数据分析工具:numpy.pandas.matplotlib.seaborn 一.分析目的 假如接下来需要开发一款APP,想了解开发什么类型的APP会更受欢迎,此次分析可以对下一步计划进行 ...

  3. emojy表情的小问题

    报错内容:在emojy表情入库或者更新库的时候,会报这个错:java.sql.SQLException:Incorrect string value: '\xF0\x9F\x99\x8F\..' fo ...

  4. 选择企业架构实践公开课的指导?TOGAF+ArchiMate+BangEA,EA工作者必学一门公开课

    我发现身边越来越多人谈数字化.谈企业架构,但是感觉谈的总是IT?知道好像不对,但是又好像也无法告诉别人这和传统的IT架构有什么不一样?网上找资料,看了很多也摸不到门路,也不知道别人讲的对不对.对了还好 ...

  5. Spring高级注解

    目录: 1.使用限定注解:2.自定义限定注解:3.自定义bean的生命周期: 开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8新建一个名称为demo的Spr ...

  6. Spark中持久化和序列化学习

    一.cache和persisit的对比 -rw-r--r--@ 1 hadoop staff 68M 5 17 07:04 access.log    cache/persitence是 laz ...

  7. Shell—实现DDOS攻击自动封禁IP

    需求:请根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP. 防火墙命令为:iptables-I INPUT -s IP地址 -j DR ...

  8. 《Linux/UNIX系统编程手册》第56章 SOCKET:介绍

    关键词: 1. socket基础 一个典型的客户端/服务器场景中,应用程序使用socket进行通信的方式如下: 各个应用程序创建一个socket.socket是一个允许通信的设备,两个应用程序都需要用 ...

  9. 004.MongoDB数据库基础使用

    一 数据库管理 1.1 创建数据库 [root@uhost ~]# mongo --host 172.24.9.225 --port 27017 -u useradmin -p useradmin & ...

  10. python模块下载备份

    https://pypi.org/ https://pypi.doubanio.com/simple/