题目:

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的更多相关文章

  1. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  2. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  3. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  4. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  5. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  6. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  7. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  8. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  9. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

随机推荐

  1. 安卓自定义日期控件(仿QQ,IOS7)

    还记得上篇:高大上的安卓日期时间选择器,本篇是根据上篇修改而来,先看下qq中日期选择的效果: 鉴于目前还没有相似的开源日期控件,因此本人花费了一些时间修改了下之前的日期控件,效果如图: 虽说相似度不是 ...

  2. 【编程练习】poj1111

    Image Perimeters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8632   Accepted: 5168 ...

  3. SSH网上商城---使用ajax完成用户名是否存在异步校验

    小伙伴在上网的时候,需要下载或者观看某些视频资料,更或者是在逛淘宝的时候,我们都需要注册一个用户,当我们填写好各种信息,点击确定的时候,提示用户名已经存在,小编就想,为什么当我们填写完用户名的时候,她 ...

  4. 查找maven中的groupId,artifactId,version等信息的方式

    可以查看:http://search.maven.org/   输入要想找的东西 

  5. 03_NoSQL数据库之Redis数据库:list类型

     lists类型及操作 List是一个链表结构,主要功能室push,pop.获取一个范围的所有值等等,操作中key理解为链表的名字.Redis的list类型其实就是一个每个元素都是string类型 ...

  6. Windows Server2008R2、2012R2重置系统开机登陆密码

    平时用的虚拟机太多导致经常会忘记密码,这里分享两个链接,分别对应的是08R2和12R2重置密码的方法. 08R2:http://ucweb.blog.51cto.com/4042188/962284 ...

  7. mysql5.6升级到5.7后Sequel Pro无法连接解决

    因为装ntop,brew自动升级了本地的Mysql,结果升级完成之后,使用Sequel Pro连接总是报错,使用mysql 命令行工具就没有问题. OSX版本 10.11.5 Mysql版本 5.6 ...

  8. Activity, Service,Task, Process and Thread之间的关系

    Activity, Service,Task, Process and Thread之间到底是什么关系呢? 首先我们来看下Task的定义,Google是这样定义Task的:a task is what ...

  9. iOS中 UIMPMediaPickerController播放系统音乐

    布局如下: 引入框架: #import <AVFoundation/AVFoundation.h> #import <MediaPlayer/MediaPlayer.h> 遵循 ...

  10. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...