题目:

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. Linux网络和进程管理

     1) 计算机网络是通过外围的设备和连接,将分布在相同或不同区域的多台计算机 连接在一起所形成的集合.网络中的计算机实现彼此间互相通信,并且可以共 同使用硬件.软件和数据资源,实现资源共享.Lin ...

  2. Centos7下Redis3.2的安装配置与JReid测试

    环境 Centos7 Redis版本 3.2.0 安装目录 /usr/local/redis/redis-3.2.0 Redis的介绍 参见官网 安装 1 安装gcc与tcl # yum instal ...

  3. Cocos2D iOS之旅:如何写一个敲地鼠游戏(九):创建动画

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

  4. Java EE 之 过滤器入门学习与总结(2)

    今天就对使用Filter技术能做什么来个小小的归纳.也为了方便今后的复习. 控制浏览器不进行对jsp页面的缓存 //在doFilter方法中添加这样的代码 HttpServletRequest req ...

  5. 新手学python(3):yield与序列化

    1 Yield生成器 Yield是我在其他语言中没有见过的一个属性,算是python的一大特色,用好之后可以使代码更简洁.考虑一个简单的例子,文件的遍历.要遍历一个目录下的所有文件需要递归的操作.如果 ...

  6. 如何在Cocos2D 1.0 中掩饰一个精灵(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...

  7. Android官方命令深入分析之虚拟机

    Android SDK包含了一个运行在计算机上的移动设备虚拟机.这个虚拟机可以允许你在没有物理设备的情况下开发和测试Android应用. 键盘命令 虚拟设备按键 对应键盘按键 Home HOME 菜单 ...

  8. EBS DBA指南笔记(二)

    第三章 监控和诊断   本章涵盖以下几个主题:监测的方法,数据库的监测,apache的监测,forms的监测,并发管理器的监测,服务器的监测,网络的监测,其它的一些监测和诊断方法. 1.监测的方法:主 ...

  9. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十)

    打开Arm.h,在其接口中添加一个新方法: -(void)armShoot; 接下来在Arm.m中实现该方法: -(void)armShoot{ CGPoint startPoint = [self ...

  10. Chipmunk僵尸物理对象的出现和解决(一)

    最近在写的BrickHit游戏App中出现了一个比较头疼的问题. 该问题很难用常规手段调试,因为其发生看起来貌似是随机的. 我想在这里将这个问题的现象和解决过程详细的记录下来,一来避免其他童鞋走弯路, ...