小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题!

问题描述:

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

解题思路:

该问题就是找到所有数组字符串里面的最长相同前字串。所以我的思路是先找到数组中最短的那个字符串,然后每次比较的时候最多循环该长度就行,这样避免字符串下标溢出的问题。设置StringBuilder对象用于存放相同的字符。然后开始循环,对于字符串的每个位置的字符,取该数组中第一个字符串的该位置作为参考,如果有哪个字符串该位置的字符不匹配,则直接返回已接好的StringBuilder对象,否则循环继续。最后返回接好的StringBuilder对象。

代码如下:

public class Solution {
public String longestCommonPrefix(String[] strs) {
int length = Integer.MAX_VALUE;
StringBuilder stringbuilder = new StringBuilder();
if (strs.length == 0 || strs == null)
return "";
if (strs.length == 1)
return strs[0];
for (int i = 0; i < strs.length; i++) {
length = (strs[i].length() < length) ? strs[i].length() : length;
}
if (length == 0)
return "";
for (int j = 0; j < length; j++) {
for (int i = 0; i < strs.length; i++) {
if (strs[i].charAt(j) != strs[0].charAt(j))
return stringbuilder.toString();
}
stringbuilder.append(strs[0].charAt(j));
}
return stringbuilder.toString();
}
}

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

  1. Leetcode 14. Longest Common Prefix(水)

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

  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 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

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

  4. [LeetCode] 14. Longest Common Prefix

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

  5. Leetcode 14——Longest Common Prefix

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

  6. [leetcode]14. Longest Common Prefix 最长公共前缀

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

  7. [LeetCode] 14. Longest Common Prefix ☆

    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. If there is n ...

  9. LeetCode——14. Longest Common Prefix

    一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...

随机推荐

  1. vs2010配备boost编程环境

    vs2010配备boost编程环境 vs2010配置boost编程环境 第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_53_0 ...

  2. PowerDesigner 非数值默认值时会自动增加单引单

    在PowerDesigner中,如果默认值是非数值型的,那么 PowerDesigner 会默认加上单引号 因此我们需要把这个默认的单引号干掉,如果是需要设置字符串默认值的时候,就手工加上 单引号 即 ...

  3. easy ui datagrid 设置冻结列

    为了冻结列,您需要定义 frozenColumns 属性.frozenColumn 属性和 columns 属性一样. $('#tt').datagrid({ title:'Frozen Column ...

  4. EXTJS4.2 控件之Grid 根据数据源某列数据不同绑定不同的控件setEditor

    Grid 根据数据源某列数据不同绑定不同的控件,例如:文本框和下拉框 主要代码写在grid的  plugins: [rowEditing],下面这是定义的rowEditing对象,这里面的要定义成 E ...

  5. 自定义UICollectionViewLayout并添加UIDynamic - scorpiozj(转)

    转载自:http://www.tuicool.com/articles/jM77Vf     自定义UICollectionViewLayout并添加UIDynamic UICollectionVie ...

  6. C# - Generic

    定义泛型类 创建泛型类,在类定义中包含尖括号语法 class MyGenericClass<T> { ... } T可以是任意标识符,只要遵循通常的C#命名规则即可.泛型类可以在其定义中包 ...

  7. PAT-乙级-1044. 火星数字(20)

    1044. 火星数字(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 火星人是以13进制计数的: 地球人的 ...

  8. textarea中限制输入字符长度

    要在textarea中限制输入字符的长度,比如在twitter中要限制字符为140个,可实现的方法有: 1. <textarea name="A" cols="45 ...

  9. Quartz任务调度快速入门(转)

    概述 了解Quartz体系结构 Quartz对任务调度的领域问题进行了高度的抽象,提出了调度器.任务和触发器这3个核心的概念,并在org.quartz通过接口和类对重要的这些核心概念进行描述: ●Jo ...

  10. [笨木头FireFly01]入门篇1·最简单的服务端和客户端连接

    原地址:http://www.9miao.com/question-15-53938.html 最近一直在写游戏,几乎没有来写教程了,打算放慢一下脚步,学学新东西.那为嘛我要学FireFly呢? 之前 ...