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() ;
}
}

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

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

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

  2. LeetCode--No.014 Longest Common Prefix

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

  3. 【LeetCode】014. Longest Common Prefix

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

  4. [Leetcode]014. Longest Common Prefix

    public class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.l ...

  5. 014 Longest Common Prefix 查找字符串数组中最长的公共前缀字符串

    编写一个函数来查找字符串数组中最长的公共前缀字符串. 详见:https://leetcode.com/problems/longest-common-prefix/description/ 实现语言: ...

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

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

  7. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...

  8. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  9. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

随机推荐

  1. 4、时间同步ntp服务的安装于配置(作为客户端的配置)

    yum安装ntpd服务   .yum -y install ntp ntpdate (安装时间同步ntp服务) . vi /etc/ntp.conf (修改ntpd服务的配置文件)   3.修改配置文 ...

  2. 大公司的Java面试题集

    找工作要面试,有面试就有对付面试的办法.以下一些题目来自我和我朋友痛苦的面试经历,提这些问题的公司包括IBM, E*Trade, Siebel, Motorola, SUN, 以及其它大小公司. 面试 ...

  3. HDU2222

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 注意: 1. keyword可以相同,因此计算时要累计:cur->num++. 2. 同一个keyw ...

  4. SVG 2D入门11 - 动画

    交互性      SVG拥有良好的用户交互性,例如:1. SVG能响应大部分的DOM2事件.2. SVG能通过cursor良好的捕捉用户鼠标的移动.3. 用户可以很方便的通过设置svg元素的zoomA ...

  5. 关于设置border的小技巧

    可以在需要的时候,在某个元素下面放一个长或宽为1px,或者你需要的border宽度的 div ,再在这个div 上设置border.按需要调整这个div的位置.

  6. UVA 10801 Dij最短路(改模板)

    题意:有n个电梯,目的地是第K层(起点是第0层),给出每个电梯的速度,以及每个电梯能到达的层数,如果中途需要换电梯的话,时间需要+60,求到达目的地的最短时间: 思路:Dij求最短路.如果是另一条路比 ...

  7. Linux线程-创建

    Linux的线程实现是在内核以外来实现的,内核本身并不提供线程创建.但是内核为提供线程[也就是轻量级进程]提供了两个系统调用__clone()和fork (),这两个系统调用都为准备一些参数,最终都用 ...

  8. JavaScript Emoji 表情库_js 类似于qq微信的表情库

    摘要: emoji就是表情符号,来自日语词汇“絵文字”(假名为“えもじ”,读音即emoji).emoji的创造者是日本人栗田穰崇(Shigetaka Kurita),他将目光投向儿时的各种元素以获取灵 ...

  9. Extjs各版本的下载链接

    Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www.sencha.com/pro ...

  10. C++访问托管类(C#类库)

    1.新建C# 类库项目Airth,新建类 using System; using System.Collections.Generic; using System.Linq; using System ...