本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制。导致我一点点排坑,浪费了较多时间。

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

  编写一个函数来查找字符串数组中最长的公共前缀字符串。


 class Solution {
public String longestCommonPrefix(String[] strs) {
int length =strs.length,strLength=0;
StringBuffer sBuffer=new StringBuffer();
boolean isSame=false;
if(strs.length!=0)//考虑字符串数组可能为空情况
strLength=strs[0].length();
if (strs.length==1) {//考虑字符串数组为一个的情况
return strs[0];
}
else{
for (int i = 0; i < length - 1; i++) {
if (strs[i].length() == 0)//考虑某个字符串为空的情况
return "";
if (strLength > strs[i + 1].length())//求出最短字符串的长度
strLength = strs[i + 1].length(); }
for (int i = 0; i < strLength; i++) {
for (int j = 0; j < length - 1; j++) {
if (strs[j].charAt(i) == strs[j + 1].charAt(i)) {
isSame = true;
}
else{
isSame=false;
}
}
if (isSame)
sBuffer.append(strs[0].charAt(i));
else
break;
}
return sBuffer.toString();
}
}

LeetCode记录之14——Longest Common Prefix的更多相关文章

  1. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  2. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  3. 14. Longest Common Prefix【leetcode】

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

  4. Leetcode 14. Longest Common Prefix(水)

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

  5. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

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

  6. C# 写 LeetCode easy #14 Longest Common Prefix

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

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

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

  8. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

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

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

随机推荐

  1. elasticsearch2.x插件之一:kopf

    kopf kopf is a simple web administration tool for elasticsearch written in JavaScript + AngularJS + ...

  2. 读excle

    1.OleDbConnection读取Excel ///<summary>///上传文件到临时目录中 ///</ummary>private void Upload(){ Ht ...

  3. Solidity智能合约升级解决方案

    https://blog.zeppelin.solutions/proxy-libraries-in-solidity-79fbe4b970fd

  4. 模板模式和Comparable类

    模板模式中,父类规定好了一些算法的流程,并且空出一些步骤(方法)留给子类填充 Java的数组类中静态方法sort()就是一个模板,它空出了一个compareTo的方法,留给子类填充,用来规定什么是大于 ...

  5. Apr编程

    一.简介 http://www.xuebuyuan.com/2195578.html   二.教程 http://dev.ariel-networks.com/apr/

  6. Luogu 3698 [CQOI2017]小Q的棋盘

    BZOJ 4813 虽然数据范围很迷人,但是想树形$dp$没有前途. 先发现一个事情,就是我们可以先选择一条链,最后要走到这一条链上不回来,走到链上的点每一个只需要一步,而如果要走这条链之外的点,一个 ...

  7. Django rest_framework----认证,权限,频率组件

    认证 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions impor ...

  8. Orace开源的异步IO编程库,特点是接口非常简单

    官网:https://oss.oracle.com/projects/libaio-oracle/,正如标题所说,非常简单了,不用多解释,请直接看头文件,其中aio_poll类似于poll,重要的结构 ...

  9. Java 扫描器类 Scanner类

    1.Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.Scanner reader=new Scanner(System.in); 2.reader对象调用下列方法(函数),读取用户 ...

  10. Deep Visual-Semantic Alignments for Generating Image Descriptions(深度视觉-语义对应对于生成图像描述)

    https://cs.stanford.edu/people/karpathy/deepimagesent/ Abstract We present a model that generates na ...