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. pytest系列(一):什么是单元测试界的高富帅?

    pytest是python语言中一款强大的单元测试框架,用来管理和组织测试用例,可应用在单元测试.自动化测试工作中. unittest也是python语言中一款单元测试框架,但是功能有限,没有pyte ...

  2. Java死锁演示

    Java死锁演示 在线程中嵌套获取锁导致死锁.思路,尽量不要嵌套获取锁. package com.mozq.demo.demo; public class DeadLockDemo { private ...

  3. Nginx安装及配置反向代理

    本片博客记录在ubuntu16下安装nginx,以及如何实现负载均衡 安装nginx 如果是新机器,安装相关依赖环境 sudo apt install build-essential sudo apt ...

  4. IdentityServer4 常见问题 - 用户拒绝授权后报错

    1.问题说明 在 IdentityServer4 Web 授权中,一般会有一个显示客户端需要获取用户的那些信息的页面,询问用户是否同意: 在这个页面如果我们点击"No, Do Not All ...

  5. SpringCloud的入门学习之概念理解、Ribbon负载均衡入门

    1.Ribbon负载均衡,Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端.负载均衡的工具. 答:简单的说,Ribbon是Netflix发布的开源项目,主要功能 ...

  6. Linux下使命令不受终端断开的影响,保持在后台运行的几种方法及原理

    摘自https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/ 记录一下Linux下使命令不受终端断开的影响,保持在后台运行的几个方法及其原理.当用 ...

  7. 一篇文章,彻底理解ReentrantLock

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  8. 关于javascript中变量及函数的提升

    javascript中变量以及函数的提升,在我们平时的项目中其实还是挺常用的,尤其是大型项目中,不知不觉就会顺手添加一些变量,而有时候自己的不小心就会酿成一些不必要错误,趁有时间整理一下自己对于js中 ...

  9. JQuery 获取元素到浏览器可视窗口边缘的距离

    获取元素到浏览器可视窗口边缘的距离 by:授客 QQ:1033553122 1.   测试环境 JQuery-3.2.1.min.js 下载地址: https://gitee.com/ishouke/ ...

  10. js-事件函数调用简化

    // 一般写法 function fn(event) { console.log(event) } div.onclick = function (event) { fn(event) } ===== ...