题目:

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. win32贪吃蛇实现

    写程序是一个循序渐进的过程,一开始都是加加减减,修修补补,这和我们做企业做创新的原理都是一样的,没有一蹴而就的成功,最近看了周鸿祎的<我的互联网方法论>蛮有启发,分享给大家几句摘抄: 1. ...

  2. 基于CAS实现单点登录(SSO):工作原理

    工作中使用到了SSO,网上看到了这个博客的一系列文章感觉不错,转载收藏 源地址http://blog.csdn.net/tch918/article/details/19930037 系列文章的第一篇 ...

  3. Android初级教程短信防火墙

    如果你有女神,而且有情敌的话,你看到这篇文章会有一种窃喜的感觉. 需求:对情敌的号码进行拦截,让女神手机永远收不到它的号码. 首先定义一个广播接收者类: package com.example.sms ...

  4. 【一天一道LeetCode】#237. Delete Node in a Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  5. JAVA之旅(二十七)——字节流的缓冲区,拷贝mp3,自定义字节流缓冲区,读取键盘录入,转换流InputStreamReader,写入转换流,流操作的规律

    JAVA之旅(二十七)--字节流的缓冲区,拷贝mp3,自定义字节流缓冲区,读取键盘录入,转换流InputStreamReader,写入转换流,流操作的规律 我们继续来聊聊I/O 一.字节流的缓冲区 这 ...

  6. DB2数据库常用命令

    --创建数据库 CREATE DATABASE example AUTOMATIC STORAGE YES --自动存储 ON 'D:\' DBPATH ON'D:\' --指定数据库控制文件的存储路 ...

  7. python跨行 print:多用(),换行符\要小心,少用+或者不用(其它程序代码跨行用\就行,不能用括号)

    这两种是错的 # print '11' #       'tset3'#error # print '12' #       +'tset4'#error python跨行用()和\都能实现.+只是连 ...

  8. MyEclipse9安装Checkstyle5.5插件(图解)

    ①首先下载Eclipse Checkstyle Plug-in     官方首页:http://sourceforge.net/projects/eclipse-cs/files/     最新版为: ...

  9. Android学习之Animation(一)

    3.0以前,android支持两种动画模式,Tween Animation,Frame Animation,在android3.0中又引入了一个新的动画系统:Property Animation,这三 ...

  10. Android性能优化之被忽视的优化点

    对于性能优化这个知识点来说,实在是太广了,博主本人也一直非常关注这方面的学习,而对于性能优化来说它包括了非常非常非常多方面,比如:I/O的优化.网络操作的优化.内存的优化.数据结构的优化.代码层次的优 ...