1. 原题链接

https://leetcode.com/problems/longest-common-prefix/description/

2. 题目要求

给定一个字符串数组,让你求出该数组中所有字符串的最大公共前缀。例如{"qqwwee", "qqww", "qqfds"}的最大公共前缀为"qq",{"qqwwee", "qqww", "qqfds", "wea"}的最大公共前缀为""(空)。

3. 解题思路

(1)首先判断该字符串数组长度是否为零,为零直接返回最大公共前缀为空;

(2)假设该字符串数组中第一个元素strs[0]为最大公共前缀,然后与第二个元素进行比较,判定第二个元素是否包含假定的最大公共前缀,且假定的最大公共前缀的起始位置为0。满足以上要求,则证明该元素与之前的元素共同拥有该前缀。如果不包含,则证明假定的公共前缀不符合要求,将假定的公共前缀缩短一位,然后重新比较。

4. 代码实现

 public class LongestCommenPrefix14 {

     public static void main(String[] args) {
String[] strs = {"weeer", "weer", "wer"};
System.out.println(LongestCommenPrefix14.longestCommonPrefix(strs));
} 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); //缩短假定的公共前缀
if (prefix.equals(""))
return "";
}
}
return prefix;
}
}

LeetCode:14. Longest Commen Prefix(Easy)的更多相关文章

  1. 「Leetcode」14. Longest Common Prefix(Java)

    分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...

  2. 【leetcode】Longest Common Prefix (easy)

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

  3. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  4. LeetCode:5. Longest Palindromic Substring(Medium)

    原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...

  5. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  6. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  7. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  8. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  9. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

随机推荐

  1. 第二次团队Scrum

    长大一条龙之登录注册 一.设计详情 本次冲刺我们团队实现了长大一条龙的登录注册功能,我们的这个项目严格遵守MVC架构,采用前后端分离的策略.我们将登录注册分为二层,DAO层:负责与数据进行交互,读写数 ...

  2. 如何证明CRM WebClient UI上的应用是有状态(Stateful)的

    随便找一个在CRM WebClient UI里会被频繁调用到的函数,在函数里创建一个隐式增强,打印当前的会话ID. 不关闭浏览器,在浏览器里不断做操作,可以观察到不管做任何操作,每次断点出发后,打印出 ...

  3. POJ 2985 名次树

    题意:1~n个猫,有合并操作,有询问操作,合并两只猫所在的集合,询问第K大的集合. 分析:合并操作用并查集,用size维护,询问操作用Treap.注意优化,不能刚开始就把所有size = 1放到名次树 ...

  4. 【[CTSC2012]熟悉的文章】

    题目 好题啊 \(SAM\)+单调队列优化\(dp\) 首先这个\(L\)满足单调性真是非常显然我们可以直接二分 二分之后套一个\(dp\)就好了 设\(dp[i]\)表示到达\(i\)位置熟悉的文章 ...

  5. 2018.12.21 如何在现有的Eclipse(4.9.0版本)中安装STS (Spring Tool Suite ) Mac环境下

    在Eclipse中安装STS正确步骤实现方式 1.简介说明       spring Tool Suite(sts)就是一个基于Eclipse的开发环境, 用于开发Spring应用程序.它提供了一个现 ...

  6. 从OC和C#中找乐趣:相同又不同的delegate

    不想说话,本来第一段打了一大堆废话,结果浏览器崩溃了...直接进入正题吧.看Demo: C#里面也有delegate,我今天的目的就是模仿着OC里面的写法来写一个网络请求模拟类.先建一个“Protoc ...

  7. idea连接sqlite

    首先下载驱动 官网链接:http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc 打开idea 第一步:右边 数据源 (如果没有显示单击这里,有 ...

  8. Trident中 FixedBatchSpout分析

    FixedBatchSpout 继承自 IBatchSpout IBatchSpout 方法 public interface IBatchSpout extends Serializable { v ...

  9. android 学习笔记 杂记1

    getIntent().getExtras().get("intent"); 这个intent是数据包装的参数. 比如: Intent intent = new Intent(th ...

  10. 课时60.CSS的固定格式(掌握)

    CSS就是用来设置样式的,美化界面的 如何验证? 打开一个京东首页 删除掉css样式 发现页面变得非常难看 由此我们验证了一个说法,css就是用来美化界面的 1.格式: <style type= ...