Leetcode 14——Longest Common Prefix
题目: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的更多相关文章
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- [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 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. public class ...
- 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. If there is n ...
- [LeetCode] 14. Longest Common Prefix ☆
Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...
- [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——14. Longest Common Prefix
一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...
随机推荐
- css属性详解
一.字体属性 字体 font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. body { font-famil ...
- 拥抱.NET Core系列:MemoryCache 缓存选项
在上一篇 "拥抱.NET Core系列:MemoryCache 缓存过期" 中我们详细的了解了缓存过期相关的内容,今天我们来介绍一下 MSCache 中的 Options,由此来介 ...
- Codeforces Round #454 D. Seating of Students
分三类 1 1: 一个就好了 3 3:特殊讨论下 或 : 第一行奇序号的数放前面,偶序号的数放后面,第二行奇序号的数放前面,偶序号的数放后面,第二行依次类推 有点难写,真的菜 #include< ...
- JNDI在本项目中的应用
一,在tomcat的context文件中做如下配置 <?xml version="1.0" encoding="UTF-8"?> <Conte ...
- pycharm short-cut
Ctrl + the left mouse button Ctrl + Alt + Left/Right if invalid, system setting
- 初识Windows服务 C#
1.新建Windows服务 2.切换到代码视图,拷入如下代码 该服务以10S的间隔创建 d:/1.txt 文件 using System; using System.Collections.Gen ...
- [Luogu2991][USACO10OPEN]水滑梯Water Slides
题面戳我 题面描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪. 超级轨道包含 E (1 <= E &l ...
- Oracle定时任务小案例
需求简述 一个数据表中包含此数据的录入时间,此数据的初始状态是有效,五天后系统自动置该数据的状态为无效. 方案 写一个存储过程,用于更新字段(改状态): 写一个job,用于定时执行存储过程: 方案逻辑 ...
- 谷歌chrome 插件(扩展)开发——进阶篇(c#本地程序和插件交互)下
在上一篇中,我提出了总任务.接下来去实现. 获取网页内容等其它信息,这是content.js 擅长做的事情: chrome.extension.onMessage.addListener( funct ...
- openlayers渲染mapbox gl的vector tile
准备条件 https://openlayers.org/en/v4.6.5/build/ol.js https://cdn.polyfill.io/v2/polyfill.min.js DEMO &l ...