本题虽然是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. C++ 重载操作符- 01 简单的入门

    重载操作符的定义 这篇博客是对 重载操作符 的一个概要性的介绍. 重载操作符是C++语言的高级功能,当我们写一个类的时候,可以根据需要学一个重载操作符,如果 不需要,我们可以不写. 大量的操作符都可以 ...

  2. C调用C++接口

    在cpp头文件里面声明函数 #ifndef _HEAD_ #define _HEAD_ #ifdef __cplusplus extern "C" { #endif #define ...

  3. MCMC 破译密码 http://mlwhiz.com/blog/2015/08/21/MCMC_Algorithms_Cryptography/

    # AIM: To Decrypt a text using MCMC approach. i.e. find decryption key which we will call cipher fro ...

  4. oracle数据库登录

    在做以下操作时,要确保你的数据库环境已经正确安装完成.数据库在实际应用中是比较多的,我们测试人员经常会在前台造一些测试数据,在后台数据库进行验证,当然,不局限于此,数据库也可以作为一个专项测试来谈.反 ...

  5. jstl c

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 例子:list中有两 ...

  6. Storm的wordCounter计数器详解

    原文:http://www.maoxiangyi.cn/index.php/archives/362 拓扑 点击(此处)折叠或打开 package cn.jd.storm; import backty ...

  7. delphi声明类及其调用方法

    {type放在interface下的uses引用单元下面} 1 // 声明类 type TMyclass = class //注意这里不能加';' 因为这是个整体 data: integer; //类 ...

  8. "ServiceStack.Redis.RedisNativeClient”的方法“get_Db”没有实现。

    解决办法: 1.首先通过nuget程序包管理器将相关依赖项卸载干净 2.检查各项目模块中的package.config里还有没有redis的节点,如果已经存在就删除掉 3.去别的正常的项目中看一下re ...

  9. 为什么 kubernetes 天然适合微服务 (3)

    此文已由作者刘超授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 四.Kubernetes 本身就是微服务架构 基于上面这十个设计要点,我们再回来看 Kubernetes,会发现 ...

  10. 【连载】redis库存操作,分布式锁的四种实现方式[四]--基于Redis lua脚本机制实现分布式锁

    一.redis lua介绍 Redis 提供了非常丰富的指令集,但是用户依然不满足,希望可以自定义扩充若干指令来完成一些特定领域的问题.Redis 为这样的用户场景提供了 lua 脚本支持,用户可以向 ...