本题虽然是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. 【总结整理】AXURE原件

    iphone:750*1334 一般用分辨率的一半 移动的时候,按住shift拖动,可水平移动 框选的时候,箭头选择包含模式,只要不全部包含进来,就不会被选中 ctrl+'=显示背景网格 ctrl+s ...

  2. Bootstrap 中的 aria-label 和 aria-labelledby 属性

    这两个属性是为特殊网页阅读器设置的属性,在一些特殊设备上,当浏览到这样的内容设备会将内容读出来.是为了一些有视力障碍的人能够同样”浏览”网页而准备的. 转自http://blog.csdn.net/l ...

  3. 设置窗口的z-order总是在最底部

    想让窗口置顶,很简单,只需要在SetWindowPos中指定 HWND_TOPMOST就OK了, 但是如果想要窗口始终位于最底端,Windows却没有提供接口. 不过呢,Windows提供了一个消息W ...

  4. opennebula 模板参数说明

    两种模板配置方式一.光驱引导启动需要配置:disk1:磁盘类型:cdrom      驱动类型:file      磁盘标记:hd      是否只读:yesDisk2:磁盘类型:DATABLOCK驱 ...

  5. 5-有道爬虫demo(post)

    爬取有道页面,实现中文翻译成英文: #_*_ coding: utf-8 _*_ ''' Created on 2018-7-12 @author: sss 功能:爬取有道翻译 ''' import ...

  6. 17-取石子-hdu1846(巴什博奕)

    http://acm.hdu.edu.cn/showproblem.php?pid=1846 Brave Game Time Limit: 1000/1000 MS (Java/Others)     ...

  7. IntelliJ IDEA——利用maven插件构建web工程

  8. Laravel Gate 授权方式的使用指南

    参考链接:An Introduction to Laravel Authorization Gates 本文使用 Laravel 的 Gate 授权方式 实现一个基于用户角色的博客发布系统. 在系统包 ...

  9. centos 升级 python

    1. 下载python 2.7编译安装 $wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz $tar zxvf Python-. ...

  10. Listview 利用Datapager进行分页

    原文:http://lgm9128.blog.163.com/blog/static/421734292010513111851101/ <asp:ListView ID="ListV ...