public class Solution {
public String get(String a,String b)
{ if(a==""||b=="") return "";
int len1=a.length();
int len2=b.length();
int len=len1;
if(len>=len2) len=len2;
String s="";
for(int i=0;i<len;i++)
{
if(a.charAt(i)==b.charAt(i))
{
s+=a.charAt(i);
}
else break; }
return s;
} public String longestCommonPrefix(String[] strs) {
int len=strs.length;
if(len==0) return "";
if(len==1) return strs[0];
String ans=get(strs[0],strs[1]);
if(ans=="") return "";
else
{
for(String s:strs)
{
ans=get(s,ans);
if(ans=="") return "";
} }
return ans; }
}

leetcode Longest Common Prefix 多个字符串的最长字串的更多相关文章

  1. [LeetCode] Longest Common Prefix 字符串公有前序

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

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

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

  3. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  4. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  5. Leetcode Longest Common Prefix

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

  6. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  7. 14. Longest Common Prefix (截取字符串)

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

  8. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  9. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

随机推荐

  1. Reporting Services 2: 参数化报表

    http://www.cnblogs.com/waxdoll/archive/2006/07/16/452467.html

  2. OC基础-day06

    #pragma mark - Day06_01_点语法 1. 点语法. 1). 如果要访问对象的属性,还要去调用属性对应的setter getter方法.好烦躁好烦躁. 2). 点语法的作用: 快速调 ...

  3. UIimageView GIF动画

    1.代码如下 (注释都有) - (void)viewDidLoad { [super viewDidLoad]; UIImageView * bigImageView = [[UIImageView ...

  4. EA UML 建模——类图

    Enterprise Architect(EA) 是一个功能比较强悍的建模工具,本篇文章仅使用其 UML 建模功能,其他更多功能,可以Google. 一.简单梳理C#中类与类.类与接口.接口与接口的关 ...

  5. React初探

    经过几天根据官方文档和博园中一些大牛的文章,在了解过基础的语法和组件后,总结一下: 1.第一件事就是分析界面,理想状态下是让每个组件只做一件事情,也就是单一职责,相互嵌套.我认为: 构建组件树,整体的 ...

  6. Binary Tree Level Order Traversal 解题思路 ×

    要求: 树的层级遍历 思路: 1.两个队列,q1 q2 ,root放到q1 2.q1首元素出列,判断是否有左右孩子,有的话,放入q2.(循环此步骤值得q1为空) 3.q1 = q2,重复2,直到q1为 ...

  7. CSS3圆角(border-radius)

    CSS3中的border-radius支持IE9+,chrome,firefox 2.3+,以及safari3.2+浏览器. border-radius可直接使用,无需加前缀(注意:firefox13 ...

  8. IS脚本学习

    OnFirstUIBefore:函数块用于第一安装应用时安装部件前所要完成的任务.一般在这里进行下列设: 1. 设置屏蔽 2. 显示欢迎信息,软件协议书或关于软件安装的其他说明信息 3. 从用户处获取 ...

  9. 帝国cms 列表页分页样式修改美化【2】

    上一篇(帝国cms 列表页分页样式修改美化[1])中我们已经对分页说了一个大概,下面我们就自己动手弄一个分页把: 第一步:进入帝国cms后台,点击系统设置->系统参数设置->信息设置:里面 ...

  10. [转]IoC框架

    1 IoC理论的背景     我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑.   图1:软件系统中耦合的对象 如果 ...