问题:寻找最长公共前缀

思路:就是逐一检查每个string中的每一位,碰到不相等的时候,结束;每个string中这一位都相等,加入到common prefix中~

public String longestCommonPrefix(String[] strs) {

        int len = strs.length;
if(len == 0) return ""; String commonPrefix = ""; for(int j = 0; ; j++){
if(j >= strs[0].length()) break;
int i;
for(i = 1 ; i < len ; i++){
if(j >= strs[i].length() || strs[i].charAt(j) != strs[0].charAt(j)) break;
}
if(i == len) commonPrefix += strs[0].charAt(j);
else break;
} return commonPrefix;
}

[leetcode]_Longest Common Prefix的更多相关文章

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

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. Leetcode Longest Common Prefix

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

  3. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  4. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  5. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  6. [LeetCode] Longest Common Prefix 字符串公有前序

    Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...

  7. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  8. [LeetCode]-011-Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. []=>" ...

  9. LeetCode OJ-- Longest Common Prefix

    https://oj.leetcode.com/problems/longest-common-prefix/ 在多个string的集合中,找出所有string的最长公共前缀. 从头开始 index ...

随机推荐

  1. Linux 下绑定域名与IP地址

    在 Linux 下,hosts 文件的路径是 /etc/hosts,此文件需要有root权限才可编辑,条目也是通过“IP 域名”的格式将域名与IP进行绑定. 对 Linux 的 hosts 配置文件的 ...

  2. zookeeper 监控 —— 阿里taokeeper

    TaoKeeper是一个围绕ZooKeeper做的监控与报表系统. 主要功能如下: 能够统计ZK集群连接数,Watcher数目 ,节点数等系列信息,并按一定规则进行一些聚合操作; 能够通过设置一些阈值 ...

  3. esriSRGeoCSType Constants

    ArcGIS Developer Help  (Geometry)     esriSRGeoCSType Constants See Also esriSRGeoCS2Type Constants ...

  4. 使用jQuery解析xml时command节点解析失败

    jQuery版本1.8.3 待解析的xml为: <message><user><command>Login</command></message& ...

  5. POJ 3270 【组合数学】

    题意: 给长度为N的学列,然后让你通过置换来使其递增.原序列没有相同的数字. 1 ≤ N ≤ 10,000 ai<=100000 思路: 先找到循环,然后根据贪心只有两种比较好的情况,让循环里边 ...

  6. bash 学习笔记

    shell:能够操作应用程序的接口就称为shell. Linux由C编写的. TAB键的使用 alias:设置别名

  7. java边界布局管理器

    public class BorderLayoutPosition extends JFrame{        String[] border={BorderLayout.CENTER,Border ...

  8. 动态代理实现AOP【转】

    http://blog.csdn.net/beijiguangyong/article/details/8624016 根据前面介绍的Proxy和InvocationHandler,实在很难看出这种动 ...

  9. 转 UITabBarController简单介绍

    文顶顶 iOS开发UI篇—UITabBarController简单介绍 iOS开发UI篇—UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavi ...

  10. HDU 4738 Caocao's Bridges

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...