Leetcode练习题Longest Common Prefix
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的更多相关文章
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- [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. 解题思路: 老实遍历即可, ...
- 【leetcode】Longest Common Prefix (easy)
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. 很简单的一个描述,最 ...
- LeetCode(54)-Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...
随机推荐
- Cocos2d-x的坐标系统
推荐转至此处阅读<Cocos2d-x的坐标系统> Cocos2d-x的坐标系统 一.坐标系 二.Cocos2d-x的坐标系统 1.类别 2.定义 三.屏幕坐标系 & OpenGL坐 ...
- Ubuntu16.04重装NVIDIA驱动
Ubuntu系统 $ sudo apt update $ sudo apt upgrade 之后出现显卡驱动出现故障,nvidia-smi输出有错,检测不到相应的驱动.只好重装,记录一下,太多的教程根 ...
- 5面终于拿到了字节跳动offer! 鬼知道我经历了啥...
坐标北京,某211本科毕业生,之前学校活动有去过字节跳动公司总部参观,所以一直以来就蛮想进入字节工作的,被字节的企业文化和工作氛围所影响.字节作为发展速度最快的互联网公司,旗下的很多产品的用户都比肩B ...
- linux配置LAMP(CentOS7.4 Apache2.4 PHP5.6)
1.安装Apache 这个就不手动安装了,直接上脚本执行 bash apache.sh 以下为脚本的内容: #!/bin/bashversion=`lsb_release -a|grep Releas ...
- Docker 零碎
Delete none tag docker image: $ docker stop $(docker ps -a | grep "Exited" | awk '{print $ ...
- Java 添加Word文本水印、图片水印
水印是一种常用于各种文档的声明.防伪手段,一般可设置文字水印或者加载图片作为水印.以下内容将分享通过Java编程给Word文档添加水印效果的方法,即 文本水印 图片水印 使用工具:Free Spire ...
- C#时间戳与时间相互转换
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- javaWeb核心技术第十四篇之easyui
网站是分为网站的前台和网站的后台. 前台--给用户看的 例如:商城 后台--给管理员看的 例如:商城后台 目的:用来添加维护数据 BootStrap:jsp 页面显示,效果好,美观,适合作为用户界面. ...
- 松软科技web课堂:SQLServer之NOW() 函数
NOW() 函数 NOW 函数返回当前的日期和时间. 提示:如果您在使用 Sql Server 数据库,请使用 getdate() 函数来获得当前的日期时间. SQL NOW() 语法 SELECT ...
- 关于如何获取项目所部署的本机IP和端口的问题
关于如何获取项目所部署的本机IP和端口的问题 今天在写一个需求的时候碰到一个不常见的问题,在没有继承或者实现服务器提供的接口或者实现类的时候,比如说部署在tomacat上,某个类不去继承servelt ...