题目:

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

题意:

写出一个函数。找到一组数组中的最长公共子串。

算法分析:

须要构建两重循环。第一层是最短子串的长度,还有一层是遍历字符串数组中的每一个成员。

brute force的想法,以第一个字符串为标准。对于每一个字符串从第一个字符開始,看看是不是和标准一致,假设不同,则跳出循环返回当前结果。否则继续下一个字符。

AC代码:

public String longestCommonPrefix(String[] strs)
{
if(strs == null || strs.length == 0)
return ""; int minLen=Integer.MAX_VALUE;
for(String str: strs)
{
if(minLen > str.length())
minLen = str.length();//找到这些字符串中的最短子串
}
if(minLen == 0) return ""; for(int j=0; j<minLen; j++)
{
char prev='0';
for(int i=0; i<strs.length ;i++)
{
if(i==0)
{
prev = strs[i].charAt(j);//每次仅仅取第一个字符串的头一个字符測试
continue;
} if(strs[i].charAt(j) != prev)//若全部的字符串的这个字符都相等,就取下一个字符,若不相等,就返回这个字符前面的那个些公共前缀
{
return strs[i].substring(0, j);
}
}
} return strs[0].substring(0,minLen);//全部字符串在最短字符串长度下前缀都相等
}

[LeetCode][Java] Longest Common Prefix的更多相关文章

  1. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  2. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

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

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  4. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  5. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  6. [LeetCode] 14. Longest Common Prefix

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

  7. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  8. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  9. LeetCode(54)-Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...

随机推荐

  1. Android兼容性测试GTS-环境搭建、测试执行、结果分析

    GTS的全称是Google Mobile Services Test Suite,所谓的Google Mobile Services即谷歌移动服务,是谷歌开发并推动Android的动力,也是Andro ...

  2. Experimental considerations

    先知 重金属颗粒与气孔大小 气孔位置.密度与开合时间 角质层厚度 意外 叶子第二天掉落 样本没有放冰箱 高光谱仪器损坏 天气下雨/雪 仪器预约 楼顶/实验室门卡提前一天预约 光合仪提前一天预约 ASD ...

  3. vue 的 scroller 使用

    一 安装 使用npm 安装npm install vue-scroller -d 二 引入 import VueScroller from "vue-scroller" Vue.u ...

  4. Django之单表的增删改查

      books/urls.py   """books URL Configuration The `urlpatterns` list routes URLs to vi ...

  5. Configure Always On Availability Group for SQL Server on Ubuntu

    下面简单介绍一下如何在Ubuntu上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的填充方法. 目前在Linux上可以搭 ...

  6. Silverlight客户端加载DWG图纸方案

    前段时间一直再研究怎么才能在Silverlight客户端加载 DWG图纸,ArcGIS API For Silverlight中可以加载Shp文件,但是不能加载DWG,最后想出了一个方法步骤如下: 1 ...

  7. 【转载】linux之sed用法

    linux之sed用法 原文地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html   sed是一个很好的文件处理工具 ...

  8. 刷题总结——奇怪的游戏(scoi2012)

    题目: 题目描述 Blinker 最近喜欢上一个奇怪的游戏.这个游戏在一个 N*M  的棋盘上玩,每个格子有一个数.每次 Blinker  会选择两个相邻的格子,并使这两个数都加上 1.现在 Blin ...

  9. 【THUSC2016】成绩单(bzoj4897)

    $f(i,j,x,y)$ 表示区间 $[i,j]$中,第 $j$ 个数在最后一次操作中才消去,最后一次操作的最大值为 $x$,最小值为 $y$ 时的最小代价: $g(i,j)$ 表示区间 $[i,j] ...

  10. cf660E Different Subsets For All Tuples

    For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct sub ...