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. vue1与vue2的路由 以及vue2项目大概了解

    vue1的路由 1.设置根组件  Vue.extend() 2.设置局部组件  Vue.extend({template:"/home"}) 3.实例化路由   var route ...

  2. 【LeetCode】225. Implement Stack using Queues

    题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...

  3. 【Android Developers Training】 70. 使用ViewPager实现屏幕滑动

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

  4. 4.Smarty模板之间调用

    {include file="header.tpl" name="cai"}

  5. C++基础之引用与指针的区别与联系、常引用使用时应注意的问题

    什么是引用? 引用就是对变量起一个别名,而变量还是原来的变量,并没有重新定义一个变量.例如下面的例子:   #include<iostream> using namespace std; ...

  6. Linux之定时任务补充

    定时任务两实例 例1: 每分钟打印一次自己的名字拼音全拼到“/server/log/自己的名字命名的文件”中. [root@chengliang log]# mkdir -p /server/log/ ...

  7. 网络请求工具类WebServiceUtils

    如果对WebService一无所知的话,建议先看看这两篇博客,对你WebService很有帮助. http://blog.csdn.NET/eyu8874521/article/details/912 ...

  8. Tomcat7安装(linux环境)

    1.获取安装包 如果没有tomcat,则创建之,并下载二进制文件到该目录,如下: mkdir /opt/tomcat cd /opt/tomcat wget http://mirrors.hust.e ...

  9. JavaScript+svg绘制的一个饼状图

    结果: svg参考:https://www.w3.org/TR/SVG/<body onload='document.body.appendChild( pieChart([12,23,34,4 ...

  10. 分享一个废弃已久的插件架构 (.Net)

    框架介绍 1:将插件暴露的页面数据接口复用到任何 WebForm和Mvc 架构的系统. 2:插件可在线卸载,发布,更新. 3:插件可分布式 独立 部署. 4:插件之间完全解耦,通过Url跳转 相互不需 ...