14. Longest Common Prefix【leetcode】
- 判断非空的情况在进行计算
- 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中
- 用第一个串进行逐个对比出最长的串
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs==null || strs.length==0)
return "";
String pr= strs[0];
for(int i =1;i<strs.length;i++){
//用j计算最长串的长度
int j=0;
while(j<pr.length()&&j<strs[i].length()&&pr.charAt(j)==strs[i].charAt(j)){
j++;
}
if(j==0){
return "";
}
pr =pr.substring(0,j);
}
return pr;
}
}
注意:看到网上有帖子说进行排序,然后对比第一个和最后一个的公共序列,这样是不对的,因为如果中间的串没有公共序列时,返回结果错误
14. Longest Common Prefix【leetcode】的更多相关文章
- 78. Longest Common Prefix【medium】
Given k strings, find the longest common prefix (LCP). Example For strings "ABCD", " ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 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
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【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. Solution: cla ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
随机推荐
- 【LeetCode】89. Gray Code
题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...
- static方法和非static方法的区别
●生命周期(Lifecycle):静态方法(Static Method)与静态成员变量一样,属于类本身,在类装载的时候被装载到内存(Memory),不自动进行销毁,会一直存在于内存中,直到JVM关闭. ...
- 再谈AbstractQueuedSynchronizer:共享模式与基于Condition的等待/通知机制实现
共享模式acquire实现流程 上文我们讲解了AbstractQueuedSynchronizer独占模式的acquire实现流程,本文趁热打铁继续看一下AbstractQueuedSynchroni ...
- touchmover手机移动端的拖动
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name= ...
- js实现类似iphone的秒表-添加平均数功能
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Photoshop制作雪碧图技巧
雪碧图,就是将网页制作中使用的多个小图片合并成一个图片,使用css技术将这张合成的图片应用在网页不同的地方. 雪碧图可以减少网页加载时的http请求数,优化网页性能. 步骤: a.使用Photosho ...
- VB6之GIF分解
原文链接:http://hi.baidu.com/coo_boi/item/1264a64172fe8dec1f19bc08 还是找了个C++的翻译下,原文链接:http://www.360doc.c ...
- webpack教程(三)——热刷新
现在我们如果修改代码,需要重新打包,再一次在命令行下输入webpack命令,很麻烦. 热刷新是什么呢?就是我们该完代码保存之后webpack会自动打包引起浏览器自动刷新,你只需要把精力都专注在代码研发 ...
- gulp静态资源构建、压缩、版本号添加
公司移动端商城使用前后分离方案,前台nginx静态文件,js使用requirejs模式,使用gulp压缩添加版本号时发现问题, 问题1.在公共的js配置中,引用的路径是写死的,缓存会一直存在. 解决方 ...
- Java试题
1.不使用循环,等比数列输出整型 n.2n.4n.8n--当大于max时,反向输出8n.4n.2n.n. 例如 n=10,max=100. 输出: 10 20 40 80 80 40 20 10 解题 ...