14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
寻找一个数组中最长的公共前缀
例如["baaa","caaabbb","aaaa"]输出“aaa”
结题思路:
  1. 判断非空的情况在进行计算
  2. 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中
  3. 用第一个串进行逐个对比出最长的串
 
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】的更多相关文章

  1. 78. Longest Common Prefix【medium】

    Given k strings, find the longest common prefix (LCP).   Example For strings "ABCD", " ...

  2. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  3. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  6. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  7. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  8. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

  9. 【LeetCode】14. Longest Common Prefix (2 solutions)

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

随机推荐

  1. laydate时间插件更换皮肤

    <script> ;!function(){ laydate.skin('molv'); laydate({ elem: '#demo' }) }();</script>

  2. 【Android Developers Training】 14. 序言:管理Activity生命周期

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. Redux-Saga学习心得

    # Redux Saga ## 简述- Reducers负责处理action的state更新:- Sagas负责协调那些复杂或异步的操作. ## 安装 npm install --save redux ...

  4. 【原创】源码角度分析Android的消息机制系列(六)——Handler的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 先看Handler的定义: /** * A Handler allows you to send and process {@link Mes ...

  5. SQL Server 文件结构 与 全局变量,函数

    SQL Server 文件结构与全局变量 数据库和表 文件类型 主数据文件 .mdf 次要数据文件 .ndf 日志文件 .ldf 系统数据库 master 数据库 记录所有的登陆账户和系统配置设置 记 ...

  6. maven编译常见错误解决方法整理

    程序包com.sun.xml.internal.ws.spi不存在 当maven项目里面有用到JDK内部的一些类,接口(如:com.sun.xml.internal.ws.spi.ProviderIm ...

  7. Python3中文件处理

    1.txt,xls,doc等文件的使用 f=open("filename","w")   打开一个用于写入的文件,要写入内容时使用f.write("内 ...

  8. BufferedWriter

    package JBJADV003;import java.io.*;public class BufferedWriterTest { public static void main(String[ ...

  9. Vue 实际项目中你可能会遇见的坑

    纸上得来终觉浅,绝知此事要躬行! Vue的文档和教程看的太多,小的demo做的多,也不如自己实际的进行一个完整项目的开发.只有做了才知道原来问题这么多,这里列举了一些你做demo教程可能不会遇见的坑. ...

  10. 如何共享数据?- 每天5分钟玩转 Docker 容器技术(41)

    数据共享是 volume 的关键特性,本节我们详细讨论通过 volume 如何在容器与 host 之间,容器与容器之间共享数据. 容器与 host 共享数据 我们有两种类型的 data volume, ...