小二好久没有更新博客了,真是罪过,最近在看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. WPF中Image的Stretch属性

    有时候我们在WPF程序中设置了图片的Width和Height,但图片显示出来的宽和高并不是我们预期的效果,这实际上是由于Image的默认Stretch属性导致的 Image的Stretch属性默认为U ...

  2. Microsoft Office Excel 不能访问文件

    问题描述: Microsoft Office Excel 不能访问文件“XX.xls”.可能的原因有: 1 文件名称或路径不存在.2 文件正被其他程序使用.3 您正要保存的工作簿与当前打开的工作簿同名 ...

  3. JS禁用浏览器退格键

    我们在真实的项目开发中经常会使用JS 对键盘上的一些按键进行禁用,常见的比如说退格键(backspace/ 后退键),我在一个项目中就遇到过在页面编辑的时候禁用掉退格键,因为退格键会发生页面后退,代码 ...

  4. IPHONE开发知识

    IPHONE开发知识http://www.cnblogs.com/valensoft/archive/2010/06/09/1754836.htmlhttp://www.cocoachina.com/ ...

  5. GUIText的淡入淡出

    单击按键“A”(随意改变),可以控制GUIText马上显示出来,然后淡出:按住按键“A”,可以使GUIText淡入,如果抬起按键则淡出. FadeInOut.cs using UnityEngine; ...

  6. Unity3d 协程、调用函数、委托

    (一)协程 开启方法:StartCoroutine("函数名"): 结束方法StopCoroutine("函数名"),StopAllCoroutines(); ...

  7. custom activities

    Useful Sharepoint Designer Custom Workflow Activities http://spdactivities.codeplex.com/ http://stac ...

  8. js高级技巧笔记(一)

    安全的类型检测 Js的类型检测机制并非完全可靠,发生错误否定及错误肯定的情况也不少: 在safari 在对正则表达式应用typeof操作符时返回"function",因此很难确定某 ...

  9. OPTICS光学算法

    package com.my.optics; public class DataPoint { private String name;//样本的名字 private double dimensioi ...

  10. 1031: [JSOI2007]字符加密Cipher - BZOJ

    Description喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作: ...