14. Longest Common Prefix

  • Total Accepted: 112204
  • Total Submissions: 385070
  • Difficulty: Easy

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

思路:

  题目很简单,求字符串数组的最长的共同前缀。也没什么思路,诸位比较呗,代码如下:

 public class No_014 {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "" ;
}
StringBuilder res = new StringBuilder() ;
//通过flag标志位来判断是否结束循环,以及是否应该加入返回的结果中
boolean flag = true ;
int index = 0 ;
while(flag){
char ch ;
if(index < strs[0].length()){
ch = strs[0].charAt(index) ;
}else{
flag = false ;
continue ;
}
for(int i = 1 ; i < strs.length ; i++){
if(index < strs[i].length() && strs[i].charAt(index) == ch){
continue ;
}else{
flag = false ;
break ;
}
}
//通过判断flag的值来判断是否是满足所有条件的
if(flag){
res.append(ch) ;
index++ ;
}
}
return res.toString() ;
}
}

LeetCode--No.014 Longest Common Prefix的更多相关文章

  1. 【LeetCode】014. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...

  2. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

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

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

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

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

  6. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

  7. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  8. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  9. No.014 Longest Common Prefix

    14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...

  10. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

随机推荐

  1. A除以B

    1017 A除以B (20)(20 分) 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中 ...

  2. QUARTZ系列之一-基础概念(Scheduler/Job/JobDetail/Trigger)

    摘抄自quartz官方文档: The key interfaces of the Quartz API are: Scheduler - the main API for interacting wi ...

  3. EnterpriseLibrary

    收藏一下: http://www.cnblogs.com/huangcong/archive/2010/06/01/1748672.html

  4. python调用webservice接口

    使用suds这个第三方模块 from suds.client import Clienturl = 'http://ip:port/?wsdl'cilent=Client(url)print cile ...

  5. 华为NB-IOT报告

    转 https://blog.csdn.net/np4rHI455vg29y2/article/details/78958137 [NB-IoT]华为NB-IoT网络报告(完整版) 2018年01月0 ...

  6. 命令实现linux和客户端文件上传下载

    1.rz/sz命令 linux端使用rz/sz实现和windows客户端 linux服务器需要装了rz,sz yum install lrzsz 当然你的本地windows主机也通过ssh连接了lin ...

  7. input type="file"多图片上传 原生html传递的数组集合

    单个的input type="file"表单也是可以实现多图片上传的 代码如下: <form action="manypic.php" method=&q ...

  8. [转]ArcGIS for Silverlight:关于尝试连接到REST端点时发生安全异常的解决方案

    Silverlight跨域策略: 要从远程服务器访问数据,远程服务器需要在 web 服务器的根目录下放置一个 clientaccesspolicy.xml 文件(例如 c:\inetpub\wwwro ...

  9. Postman入门使用

    Postman 是一个很强大的 API调试.Http请求的工具,方便易用,毋庸置疑. 1.Postman安装 a. 打开谷歌浏览器 b. 进入设置界面 c. 选择扩展程序 d. 选择chrome网上应 ...

  10. MVC+EF(CODEFIRST)+EASYUI医药MIS系统

    https://www.cnblogs.com/chenlinzhi/p/4332628.html