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. 十二 logging模块

    一 日志级别 CRITICAL = 50 #FATAL = CRITICAL ERROR = 40 WARNING = 30 #WARN = WARNING INFO = 20 DEBUG = 10 ...

  2. C#Winform的DEV下拉下拉控件介绍

    LookupEdit 下拉单选,可搜索,下拉展示为一个table: ComboxEdit 下拉单选,可当做text使用,输入数据源中没有的项,只有显示值: CheckcomboxEdit 下拉多选,可 ...

  3. H5滑条(input type=range)

    input[type=range] { -webkit-appearance: none; width: 230px; border-radius: 10px; /*这个属性设置使填充进度条时的图形为 ...

  4. Linux-目录结构及文件系统

    1.Linux 系统的顶层目录结构 /              根目录 ├── bin     存放用户二进制文件 ├── boot    存放内核引导配置文件 ├── dev     存放设备文件 ...

  5. vue.$nextTick 解决了哪些问题

    转载:https://www.cnblogs.com/xuewuhen/p/7860989.html $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用 $next ...

  6. Windows7 VS2015 下编译 PythonQt3.2

    本文在使用vs2015编译python3.6.7源代码后,编译的PythonQt3.2.如果使用python二进制文件进行安装,注意python的路径即可 本机环境: 1.win7 64 旗舰版 2. ...

  7. java项目中VO和DTO以及Entity,各自是在什么情况下应用

    1.entity里的每一个字段,与数据库相对应, 2.dto里的每一个字段,是和你前台页面相对应, 3.VO,这是用来转换从entity到dto,或者从dto到entity的中间的东西.   举个例子 ...

  8. 杨其菊201771010134《面向对象程序设计(Java)》第三周学习总结

    <面向对象程序设计(Java)>第三周学习总结 第一部分:理论知识 这周课程没有新进度,由于感觉对基础语法的不熟悉,复习了一遍前三章的细碎知识,学到一些之前不知道的原理: 1.计算机高级语 ...

  9. NCUAP 利用java自带方法实现导入excel取数据

    final JComponent parent = getModel().getContext().getEntranceUI(); JFileChooser chooser = new JFileC ...

  10. 命令方式联网与界面network-manager方式联网

    命令方式联网: sudo vi /etc/NetworkManager/NetworkManager.conf [main]plugins=ifupdown,keyfile,ofonodns=dnsm ...