LeetCode(54)-Longest Common Prefix
题目:
Write a function to find the longest common prefix string amongst an array of strings.
思路:
- 题意:找出字符串的最大的公共前缀
- 思路是写一个比较的函数,遍历
-
代码:
public class Solution {
public String longestCommonPrefix(String[] strs) {
String result = null;
if(strs == null){
return null;
}
if(strs.length == 0){
return "";
}
if(strs.length == 1){
return strs[0];
}
result = see(strs[0],strs[1]);
if(result == null){
return null;
}
for(int a= 1;a < strs.length-1;a++){
String tmp = see(strs[a],strs[a+1]);
if(tmp == null){
return null;
}else{
String tmp1 = see(tmp,result);
if(tmp1 == null){
return null;
}else{
result = tmp1;
}
}
}
return result;
}
public String see(String a,String b){
StringBuffer sb = new StringBuffer();
if(a == null || b == null){
return null;
}
int n = Math.min(a.length(),b.length());
for(int i = 0;i < n;i++){
if(a.charAt(i) == b.charAt(i)){
sb.append(a.charAt(i));
}else{
return sb.toString();
}
}
return sb.toString();
}
}
LeetCode(54)-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]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- Android必知必会-带列表的地图POI周边搜索
如果移动端访问不佳,请尝试–> Github版 2016-08-22 更新 注意:在 Activity 代码中的onPoiSearched(PoiResult result, int rCode ...
- ubuntu opengl 开发
开发环境: eclipse,需要安装C++开发插件,在自带的源中查找安装C++开发工具包即可 下载安装gl库: sudo apt-get install libgl1-mesa-dev 下载安装glu ...
- 【一天一道LeetCode】#232. Implement Queue using Stacks
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- ROS(indigo)使用Qt Creator Plug in即ros_qtc_plugin
更为详细版本请参考: http://blog.csdn.net/zhangrelay/article/details/52214411 结合看更为具体. 首先,先上原版参考: 1 http://wik ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- android-async-http详解
android-async-http开源项目可以是我们轻松的获取网络数据或者向服务器发送数据,使用起来非常简单,关于android-async-http开源项目的介绍内容来自于官方:http://lo ...
- Win7/Win8/Win10下安装Ubuntu14.04双系统 以及常见问题
整理自网络. 1. 制作镜像 将ubantu镜像刻录到优盘(我使用UltraISO刻录,镜像下载地址:链接: http://pan.baidu.com/s/1bndbcGv 密码: qsmb) 2. ...
- python 内存数据库与远程服务
python 内存数据库与远程服务 需要import的python 内存数据库代码参考下面的链接: http://blog.csdn.net/ubuntu64fan/article/details/5 ...
- LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR的区分与转化
首先在编译程序时经常会遇到这种问题: 错误 1 error C2664: "CWnd::MessageBoxW": 不能将参数 1 从"const char [3]&qu ...
- 分析比较KafkaWordCount及DierctKafkaWordCount
参考spark官方文档,Spark Streaming + Kafka Integration Guide,其中提到Spark Streaming如何从Kafka中接收数据.主要有两种方法,一种是使用 ...