LeetCode记录之14——Longest Common Prefix
本题虽然是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的更多相关文章
- leetCode练题——14. Longest Common Prefix
		
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
 - [LeetCode][Python]14: Longest Common Prefix
		
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
 - 14. Longest Common Prefix【leetcode】
		
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
 - Leetcode  14.    Longest Common Prefix(水)
		
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
 - 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 ...
 - C# 写 LeetCode easy #14 Longest Common Prefix
		
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
 - [LeetCode] 14. Longest Common Prefix 最长共同前缀
		
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
 - 【一天一道LeetCode】#14 Longest Common Prefix
		
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
 - LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
		
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
 
随机推荐
- Bootstrap 中的 aria-label 和 aria-labelledby 属性
			
这两个属性是为特殊网页阅读器设置的属性,在一些特殊设备上,当浏览到这样的内容设备会将内容读出来.是为了一些有视力障碍的人能够同样”浏览”网页而准备的. 转自http://blog.csdn.net/l ...
 - Java IO简介
			
-------------siwuxie095 Java IO简介: IO 也写作"I/O",可理解为 In 和 Out,即 输入 与 输出 ...
 - linux内核被加载的过程
			
二,linux内核被加载的过程 一,linux安装时遇到的概念解析 内核必须模块vmlinz(5M左右)不认识硬盘,原本是需要写跟loader中一样的内容,来加载非必要模块. 内核非必要的功能被编译为 ...
 - grid search
			
sklearn.metrics.make_scorer(score_func, greater_is_better=True, needs_proba=False, needs_threshold=F ...
 - logistic growth model . 求解方法 (高斯牛顿法)
			
https://www.stat.tamu.edu/~jnewton/604/chap4.pdf http://www.metla.fi/silvafennica/full/sf33/sf334327 ...
 - Entity Framework 6.0 Tutorials(2):Async query and Save
			
Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...
 - Shiro——MD5加密
			
一.shiro默认密码的比对 通过 AuthenticatingRealm 的 credentialsMatcher 属性来进行的密码的比对 /**源码org.apache.shiro.realm.A ...
 - Tarjan算法求出强连通分量(包含若干个节点)
			
[功能] Tarjan算法的用途之一是,求一个有向图G=(V,E)里极大强连通分量.强连通分量是指有向图G里顶点间能互相到达的子图.而如果一个强连通分量已经没有被其它强通分量完全包含的话,那么这个强连 ...
 - 编写高质量代码改善C#程序的157个建议——建议15: 使用dynamic来简化反射实现
			
建议15: 使用dynamic来简化反射实现 dynamic是Framework 4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译器默认dy ...
 - OkHttp3的简单使用(二)
			
OkHttp3的简单封装 public class OkHttpUtil { public static final String TAG="OkHttpUtil"; privat ...