本题虽然是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. MyBatis—实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  2. XP下,移动窗口产生重影的问题

    最近做了一个东西,其中有一个小窗口需要跟着主窗口一起移动,结果发现在Xp系统上总是产生重影,需要刷新桌面才能消失. 移动窗口我使用的是MoveWindow,最后一个参数bRepaint传递的是FALS ...

  3. IWebBrowser2不能复制剪切

    项目中嵌入了IE控件,近期做了一次大改版,发现网页不能进行复制和剪切了,折腾了半天,发现是com初始化有问题: 修正前的方式: CoInitialize(NULL); // do your work ...

  4. Windows Cmder

    一.简介 作为一个程序员,即使是在windows工作环境,cmd也是我们必不可少的使用工具.cmder 是为 Windows 提供的一个便携式控制台仿真器,用来替代windows的cmd,使用非常简单 ...

  5. Apache logresolve命令

    一.简介 logresolve是一个解析Apache访问日志中IP地址的后处理程序. 二.语法 logresolve [ -s filename ] [ -c ] < access_log &g ...

  6. Django----配置数据库读写分离

    Django配置数据库读写分离 https://blog.csdn.net/Ayhan_huang/article/details/78784486 https://blog.csdn.net/ayh ...

  7. 防止SQL注入方法总结

    一.参数化SQL 是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数 (Parameter) 来给值,用@来表示参数. 在使用参数化查询的情况下,数据库服务器不会将参数的内容视为 ...

  8. myeclipse 8.0 注册码

    Subscriber:accptechSubscription Code:nLR8ZC-855550-6765855429037911 Subscriber:Hello World Subscript ...

  9. [LintCode笔记了解一下]39.恢复旋转排序数组

    思路: 1.需要O(n)的事件复杂度,所以多次循环不考虑 2.四步翻转法 -第一步,找到数组里最小的那个数字,因为是旋转排序数组,所以只要找到某个位置arr[i]>arr[i+1]的话,就找到了 ...

  10. C++语法知识小结(持续更新中)

    1)在适用构造函数创建对象时,有时会创建临时对象.如 Stock::Stock(const std::string & co,long n,double pr); 在使用时,下面两条语句有根本 ...