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. 思路: 题意:找出 ...
随机推荐
- MySQL解惑——GROUP BY隐式排序
MySQL中GROUP BY隐式排序是什么概念呢? 主要是其它RDBMS没有这样的概念,如果没有认真了解过概念,对这个概念会感觉有点困惑,我们先来看看官方文档的介绍: 官方文档MySQL 5.7 Re ...
- TCP/IP网络协议初识
目录 一.什么是协议? 二.什么是TCP/IP协议? 三.TCP/IP为什么这么多协议? 四.TCP/IP协议为什么分层? 五.TCP/IP协议如何入门? 六.TCP/IP 的分层: 七.各协议层打包 ...
- uiautomatorviewer 报错 Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!
在进行自动化时经常需要使用到 uiautomatorviewer获取控件的各个属性,然后在脚本中通过各个控件的属性来操作. 如果使用的是uiautomator2的话,一般都是使用weditor这个来查 ...
- Security+学习笔记
第二章 风险分析 风险管理 评估:确定并评估系统中存在的风险 分析:分析风险对系统产生的潜在影响 响应:规划如何响应风险的策略 缓解: 缓解风险对未来安全造成的不良影响 风险分析流程 资产确定 漏洞确 ...
- 六、接上一个博客-ITK例子运行结果
一.程序介绍 该程序的主要思路如下: 二.程序参数 1-程序自己创建三维图像的时候 我自己计算得到的参数如下: 三维图像参数: 旋转参数: 光线投射法参数: 当我们输入参数: -v 得到程序的输出 ...
- C++ std::vector 基本用法
#include <iostream> #include <vector> using namespace std; int main() { // 初始化的方式 std::v ...
- Mybatis----resultMap类型详解
Mybatis----resultMap类型详解 这篇文章主要给大家介绍了关于Mybatis中强大的resultMap功能的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Mybatis具 ...
- python爬取网页数据
一.利用webbrowser.open()打开一个网站: ? 1 2 3 >>> import webbrowser >>> webbrowser.open('ht ...
- Redis的List的删除
Redis的List命令里没有根据index删除元素的命令,但有的时候业务会需要这个功能. 先上命令: LSET ListKey index "__deleted__"LREM L ...
- React 从入门到进阶之路(二)
在之前的文章中我们介绍了 React 开发的环境搭建及目录介绍和整理,本篇文章将介绍 React 创建组件.JSX 语法.绑定数据和绑定对象. 之前我们已经将项目运行了起来,我们再来看一下目录结构: ...