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. html5移动端查找

    用form包裹住input,修改input的类型为seach,然后给input绑定seach事件,当输入状态是输入键盘上会出现搜索,点击搜索就可以查找了 <form action="& ...

  2. 使用::befor和::after伪元素在网站中添加图标

    css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:focus. 常见伪元素——::first-letter,: ...

  3. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  4. abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”

    在测试项目的ServiceCollectionRegistrar类提前注册.

  5. Django model进阶

    Django-model进阶   QuerySet 可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Ent ...

  6. android 平台 java和javascript 通信问题 A WebView method was called on thread 'JavaBridge'.

      java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge ...

  7. FT_ND_API.dll

    ePass1000ND https://blog.csdn.net/li34442779/article/details/44276989 https://www.cnblogs.com/lidabo ...

  8. Calendar类常用需求方法

    经常处理一些日期相关的信息,Calendar类是处理日期的常用类,写下几个方法,不用重复造轮子了. 1.求上一天,下一天的日期 Date now = new Date();Calendar c = C ...

  9. SQLite3命令操作大全

    SQLite3命令操作大全 SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.ql ...

  10. Codeforces 835C-Star sky

    题目链接:http://codeforces.com/problemset/problem/835/C 题意:天上有很多星星,每个星星有他自己的坐标和初始亮度,然后每个星星的亮度在一秒内会加一如果大于 ...