题目:

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. jvm库对nio的处理

    JVM的IO选择 查JVM源码时刚好看到JVM库的一段代码: public static SelectorProvider create() { String osname = AccessContr ...

  2. Android源码分析—深入认识AsyncTask内部机制

    本文转载http://blog.csdn.net/singwhatiwanna/article/details/17596225该博主博文,谢谢该博主提供的好文章! 前言 什么是AsyncTask,相 ...

  3. 财务模块多组织,GL, SLA, SOB, COA, BSV, CCID, LE 概念的简单介绍

     GL=  General Ledger 指的是Oracle 的总帐系统. application_id = 101. 在uk似乎居然还有不同的解释(In the UK, it was refer ...

  4. Android多点触摸缩放图片-android学习之旅(四)

    获取多触摸点 核心代码: 获取触摸点的个数和位置 public boolean onTouch(View v, MotionEvent event) { switch (event.getAction ...

  5. ROS_Kinetic_20 ROS基础补充

    ROS_Kinetic_20 ROS基础补充 1 手动创建ROS功能包 参考官网:http://wiki.ros.org/cn/ROS/Tutorials/Creating%20a%20Package ...

  6. A*寻路算法入门(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  7. 【linux学习笔记】Sublime Text3支持GB2312和GBK编码以及中文输入法

    几天在ubuntu15.10下使用Sublime Text3发现中文乱码,以及不能使用中文输入法(搜狗输入法linux版)的问题,捣鼓了半天,终于完善了,下面po一下我的解决方案. 一.支持GB231 ...

  8. Hadoop-1.0.4伪分布安装与配置

    1.采用伪分布模式安装      将hadoop-****.tar.gz复制到linux的/usr/local目录下. 2.解压,重命名      #tar -xzvf hadoop-1.0.4.ta ...

  9. UI设计——最后一根稻草

    WindowsLive提供的本地地图服务看起来挺简单的,但其实非常难用: 跟其他人一样,每当我接触到一个新的地图服务,我做的第一件事总是拿当前的地址去试一试.我在上面输入的是我工作的地方.但是,当我敲 ...

  10. Socket编程实践(7) --Socket-Class封装(改进版v2)

    本篇博客定义一套用于TCP通信比较实用/好用Socket类库(运用C++封装的思想,将socket API尽量封装的好用与实用), 从开发出Socket库的第一个版本以来, 作者不知道做了多少改进,  ...