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 Studio插件
在使用Android Studio开发的时候,大部分人都会使用一些插件来提高开发效率,例如我们所熟知的butternife,selector,,GsonFormat等,这些分别从不同的原理来帮助我们提 ...
- Oracle启用scott用户
先查询一下目前数据库是否有scott用户 select username,account_status from dba_users where username like '%SCOTT%'; 如果 ...
- 文件操作:fseek函数和ftell函数
1.fseek函数: int fseek(FILE * _File, long _Offset, int _Origin); 函数设置文件指针stream的位置.如果执行成功,stream将指向以fr ...
- SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式
这对时间在学习SSH中Spring架构,Spring的事务配置做了详细总结,在此之间对Spring的事务配置只是停留在听说的阶段,总结一下,整体把控,通过这次的学习发觉Spring的事务配置只要把思路 ...
- Java项目源码为什么要做代码混淆(解释的很好)
代码混淆,是将计算机程序的代码转换成一种功能上等价,但是难于阅读和理解的形式的行为.代码混淆可以用于程序源代码,也可以用于程序编译而成的中间代码.执行代码混淆的程序被称作代码混淆器.目前已经存在许多种 ...
- [Error]Can't install RMagick 2.13.4. You must have ImageMagick 6.4.9 or later.
gem 安装ruby插件的时候 出现了一个错误 Installing rmagick 2.13.4 with native extensions Gem::Installer::ExtensionBu ...
- 使用js动态添加组件
在文章开始之前,我想说两点 1 自己初学js,文章的内容在大神看来可能就是不值一提,但是谁都是从hello world来的,望高 手不吝指教# 2 我知道这个标题起的比较蛋疼,大家看图就能说明问题 ...
- Android ToggleButton 实践
在android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来说一下,这个组件的两种使用方式. 第一种是简单的使用,利用Toast的方式弹出提示语句 需要注意的是要想自 ...
- Linux之使用网络
Linux有好多命令可以让你方便的使用网络,常见的有ssh,rsync,wget,curl等等,但是telnet等方式并不适用于网络交互的使用,因为它会暴露你的用户名密码等.所以一般使用安全的命令来进 ...
- 敏捷测试(1)--TDD概念
题记 本系列笔记将从测试人员的角度,总结在百度两年来的测试经验,记录一个完整的基于敏捷流程的验收测试全过程,分享在测试过程中的一些知识和经验,以及自己的一些理念.总结自己,也希望对大家有益. 概念 验 ...