小二好久没有更新博客了,真是罪过,最近在看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. C++数据类型总结

    关键字:C++, 数据类型, VS2015. OS:Windows 10. ANSI C/C++基本数据类型: Type Size 数值范围 无值型void 0 byte 无值域 布尔型bool 1 ...

  2. GDataXMLNode创建和解析XML

    GDataXMLNode创建xml: #import <Foundation/Foundation.h> 2 #import "GDataXMLNode.h" 3 4 ...

  3. ios App 加急审核

    下面进入正题.提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i wo ...

  4. iOS的view翻转动画实现--代码老,供参考

    新建一个view-based模板工程,在ViewController文件中添加下面的代码,即可实现翻转效果: - (void)viewDidLoad { [super viewDidLoad]; // ...

  5. 论文阅读(2014-1)----a new collaborative filtering-based recommender system for manufacturing appstore: which applications would be useful to your busines?

    这篇论文讲的东西并不深,讲的是appstore上的app个性化推荐问题,简单做个笔记. 简单介绍: 推荐系统可以降低没有卖任何app就离开的用户的概率.当用户买了某个app后,可以推荐配套的app.增 ...

  6. 2002: [Hnoi2010]Bounce 弹飞绵羊 - BZOJ

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...

  7. 1200: [HNOI2005]木梳 - BZOJ

    Description   Input 第一行为整数L,其中4<=L<=100000,且有50%的数据满足L<=104,表示木板下侧直线段的长.第二行为L个正整数A1,A2,…,AL ...

  8. httpmime-session 会话保持

    sesion在浏览器和web服务器直接是通过一个叫做name为sessionid的cookie来传递的,所以只要在每次数据请求时保持sessionid是同一个不变就可以用到web的session了,做 ...

  9. js原生removeclass方法

    //如果列表中有存在给定的值就删除 // function removeClass(ele,txt){ // var str = ele.className, // ary = str.split(/ ...

  10. [转载]LINQ 中的 select

    下面通过一些例子来说明怎样使用select,参考自:LINQ Samples 1.  可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B"开头的名字,然后全部转成小写输出 ...