题目:

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. Eclipse编写ExtJS卡死问题 eclise js验证取消

    1. Eclipse编写ExtJS卡死问题 eclise js验证取消 近期项目用到了extjs,发现项目编译的时候特别的卡,浪费很多时间而且保存的时候还要编译,因此打算看下如何取消验证extjs.最 ...

  2. Runtime系列(二)--Runtime的使用场景

    Runtime 理解介绍的文章非常多,我只想讲讲Runtime 可以用在哪里,而我在项目里哪些地方用到了runtime.多以实际使用过程为主,来介绍runtime的使用. * 那么runtime 怎么 ...

  3. IT女孩特不烦恼---九月实习总结

    对着岁月落笔,画出一场清风,那是最真的笑容 一溜烟的功夫,小编来实习Android已经四个月了,从刚开始的电商项目到现在的车段子项目,小编渐渐对这个曾经陌生的名字慢慢扭转变成熟悉的面孔,四个月的时间, ...

  4. 内存管理单元--MMU

    现代操作系统普遍采用虚拟内存管理(Virtual Memory Management)机制,这需要处理器中的MMU(Memory Management Unit,内存管理单元)提供支持,本节简要介绍M ...

  5. Android 实现图片加水印

    加水印代码 public Bitmap addWaterMark(Bitmap src, String water, Context context){ Bitmap tarBitmap = src. ...

  6. Centos中git的安装

     CentOS的yum源中没有git,只能自己编译安装,现在记录下编译安装的内容,留给自己备忘. 确保已安装了依赖的包 yum install curl yum install curl-deve ...

  7. shell入门之函数应用

    最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...

  8. 4、Android UI测试

    为你的APP进行UI测试是为了确保不出现意料之外的结果,提升用户的体验.如果你需要验证你的APP UI的正确性,你需要养成创建UI测试的习惯. Espresso测试框架是由Android Testin ...

  9. iOS中 最新支付宝支付(AliPay) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 现在的支付方式一般有三种, 支付宝, 微信, 网银. 个人觉得最简单易用的还是支付宝, 微信虽然看起来币支付宝要简单 ...

  10. vbox centos安装增强工具

    就是虚拟机识别不了宿主机的usb接口,这个虚拟机有没有图形界面,看看怎么装. 一个是依赖包问题,另一个就是挂了安装包,但是我怎么找到它并安装上去的问题. 虚拟机是centos6.6哈 vbox4.3. ...