Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题!
问题描述:
Write a function to find the longest common prefix string amongst an array of strings.
解题思路:
该问题就是找到所有数组字符串里面的最长相同前字串。所以我的思路是先找到数组中最短的那个字符串,然后每次比较的时候最多循环该长度就行,这样避免字符串下标溢出的问题。设置StringBuilder对象用于存放相同的字符。然后开始循环,对于字符串的每个位置的字符,取该数组中第一个字符串的该位置作为参考,如果有哪个字符串该位置的字符不匹配,则直接返回已接好的StringBuilder对象,否则循环继续。最后返回接好的StringBuilder对象。
代码如下:
public class Solution {
public String longestCommonPrefix(String[] strs) {
int length = Integer.MAX_VALUE;
StringBuilder stringbuilder = new StringBuilder();
if (strs.length == 0 || strs == null)
return "";
if (strs.length == 1)
return strs[0];
for (int i = 0; i < strs.length; i++) {
length = (strs[i].length() < length) ? strs[i].length() : length;
}
if (length == 0)
return "";
for (int j = 0; j < length; j++) {
for (int i = 0; i < strs.length; i++) {
if (strs[i].charAt(j) != strs[0].charAt(j))
return stringbuilder.toString();
}
stringbuilder.append(strs[0].charAt(j));
}
return stringbuilder.toString();
}
}
Java [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 ...
- 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 ☆
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/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...
随机推荐
- Oracle回收站
回收站是删除对象使用的存储空间.可以使用实例参数recyclebin禁用回收站,默认是on,可以为某个会话或系统设置为off或on.所有模式都有一个回收站. 当表空间不足时可以自动重用回收站对象占用的 ...
- crystal report format number
ToText({#totalPrice}, 2,'.', ',') &" €" http://crystaltricks.com/wordpress/?p=149
- Controlling Site Provisioning Process with a Custom Provider
http://www.cnblogs.com/frankzye/archive/2010/09/07/1820346.html http://sujoysharepoint2010.blogspot. ...
- 使用maven 命令运行项目
安装好maven3 配置好环境变量后, 输入mvn -v 查看安装是否成功, 然后导入maven项目, 选择import 导入选择Exsting Maven Projects, 接下来就准备运行一下m ...
- Linux学习笔记(6)-工作管理
什么是工作管理 工作来自job命令的翻译,job命令可以查看后台工作的进程.举例来说什么是工作管理,当你要打包一个比较大的目录时,很耗时间,但是你同时又需要使用别的命令.你会想我可以到开几个终端进行登 ...
- FileFilter
FileFilter 下面的例子中我们创建了一个FileFilter类,此类根据文件名的扩展名是否为.png来筛选文件.创建FileFilter实例之后需要将此实例作为参数传给File的listFil ...
- PHP5 session 详解
http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越来越复杂的WEB应用,需要保存一些用户状 ...
- sublime 配置g++
资料来源: http://blog.csdn.net/leonsc/article/details/5853614 http://www.cnblogs.com/zhenglichina/archiv ...
- uva 10034
计算所有点之间的权值 然后就是最小生成树 #include<cstring> #include<string> #include<cstdio> #includ ...
- DLL 支持MFC 没有DLLMAIN函数
如果使用VC编写DLL时,需要MFC功能: 一般在源文件里就不能手动写DLLMAIN函数了 它给MFC集成了,\src\mfc\dllmodule.cpp打开它,里面有有一个DLLMAIN函数,根据源 ...