题目:Write a function to find the longest common prefix string amongst an array of strings.

  很简单的一个描述,最长公共前缀,首先想到的是用递归,既然是找公共前缀,那肯定要两个进行比较,所以把第一个和除了第一个之外的字符串数组看作两个进行比较,再对后面的进行递归就好了,上代码。

public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0)
return "";
if (strs.length == 1)
return strs[0]; return help1(strs);
}
//对进入的字符串数组进行“分开” 操作
public static String help1(String[] strs) {
String[] newStrs = new String[strs.length - 1];
for (int i = 0; i < strs.length - 1; i++) {
newStrs[i] = strs[i + 1];
}
return help2(strs[0], newStrs);
}
//分开之后,进入help2,迭代“分开”。
public static String help2(String str, String[] strs) {
StringBuffer returnString = new StringBuffer();
String[] newStrs = new String[strs.length - 1];
if (str != "") {
if (strs.length == 1) {
// 比较 取公共前缀
for (int i = 0; i < str.length() && i < strs[0].length(); i++) {
if (str.charAt(i) != strs[0].charAt(i)) {
break;
}
returnString.append(str.charAt(i));
}
} else {
for (int i = 0; i < strs.length - 1; i++) {
newStrs[i] = strs[i + 1];
}
return help3(str,help2(strs[0], newStrs));
}
return returnString.toString();
}
return "";
} //对迭代返回的数据进行比较
public static String help3(String str1,String str2) {
StringBuffer returnString = new StringBuffer();
for (int i = 0; i < str1.length() && i < str2.length(); i++) {
if (str1.charAt(i) != str2.charAt(i)) {
break;
}
returnString.append(str1.charAt(i));
}
return returnString.toString();
}

  之后看了官方的solution,思路大体上是差不多的,但是实现很巧妙。先比较第一第二个,再取一二的公共前缀来比较第三个,不过这里比较的方式有点特殊。先看第二个字符串里面index(第一个字符串)是不是等于0,是的话就继续用第一个字串比较后面的(因为第一个字符串是第二个字符串的前缀了,index()为0),如果不为0,也就是说第一个字符串不是第二个的前缀,那么就把第一个字符串缩短一位,再比较,直到第一个字符串为空。上代码:

  

public 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.isEmpty()) return "";
}
return prefix;
}

Leetcode 14——Longest Common Prefix的更多相关文章

  1. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  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 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  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 [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. [LeetCode] 14. Longest Common Prefix ☆

    Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...

  8. [LeetCode]14. Longest Common Prefix最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  9. LeetCode——14. Longest Common Prefix

    一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...

随机推荐

  1. C#扩展方法类库StringExtensions

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. linux 安装沙盒virtualenv 、virtualenvwrapper

    1.沙盒安装命令: 最新版本:sudo easy_install virtualenv或者sudo apt-get install virtualenv 指定版本:pip install virtua ...

  3. 网络协议笔记-网络层-ARP协议

    [2-地址解析协议ARP] [2.1-基本概念] 地址解析协议ARP的作用是根据主机的IP地址,找出该主机的硬件地址. [2.2-为什么要使用ARP] 在数据传输过程中,网络层使用的IP地址,但是在实 ...

  4. Python Web-第三周-Networks and Sockets(Using Python to Access Web Data)

    1.Networked Programs 1.Internet 我们现在学习Internet部分,即平时我们浏览器做的事情,之后再学习客服端这部分 2.TCP 传输控制协议 3.Socket HTTP ...

  5. controller层中,参数的获取方式以及作用域的问题

    package com.krry.web; import javax.servlet.http.HttpServletRequest; import org.springframework.stere ...

  6. 为Android Studio中的SettingsActivity定制任务栏

    Android Studio为开发者提供了很多内建的Activity, 其中Settings Activity是非常有用且功能强大的一种内建Activity. Setting Activity其实本质 ...

  7. IDE-IntelliJ IDEA 主题、字体、编辑区主题、文件编码修改、乱码问题

    主题修改 上图标注 1 所示为 IntelliJ IDEA 修改主题的地方,可以通过打开左上角的File -> Setting.在 Windows 系统上 IntelliJ IDEA 默认提供的 ...

  8. oracle数据库和表的操作

    一.字符函数 (1)连接符 concat --连接符 select concat('10086','-')||'1531234567' 电话号 from dual; (2)首字母大写 initcap ...

  9. c# 多线程同步之Mutex

    说起Mutex,它的中文名字叫互斥体.它是WaitHandle家族成员之一,前面有一篇介绍过WaitHandle的家族成员构成.那么Mutex有什么作用呢?它是怎么使用的? 我们先来看看它的使用场景一 ...

  10. 利用EF Core的Join进行多表查询

    背景 话说有这么一家子,老公养了一条狗,老婆养了一只猫. 数据库的设计 人表 宠物表 通过表可以知道,宠物通过Owner指向主人的Id. 问题来了,我要和故事开头一样,老公-狗,老婆-猫,对应起来,怎 ...