思路:每次从字符数组中读取两个字符串比较。需要注意输入字符串为空,等细节。

     public String longestCommonPrefix(String[] strs) {
if(strs==null||strs.length==0){
return "";
}
int count=strs[0].length();
for(int i=1;i<strs.length;i++){
String str1=strs[i-1];
String str2=strs[i];
int len=Math.min(str1.length(),str2.length());
if(len<count){
count=len;
}
int comNum=0;
for(int j=0;j<count;j++){
if(str1.charAt(j)==str2.charAt(j)){
comNum++;
}else{
break;
}
}
if(comNum<count){
count=comNum;
} }
if(count==0){
return "";
}
return strs[0].substring(0, count);
}

【Leetcode-easy】Longest Common Prefix的更多相关文章

  1. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  2. C# 写 LeetCode easy #14 Longest Common Prefix

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

  3. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  4. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  5. 【leetcode】Longest Common Prefix

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

  6. 【LeetCode算法-14】Longest Common Prefix

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

  7. 【LeetCode每天一题】Longest Common Prefix(最长前缀)

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

  8. 【LeetCode】 Longest Common Prefix

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

  9. 【leetcode刷题笔记】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...

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

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

随机推荐

  1. 【共享单车】—— React后台管理系统开发手记:主页面架构设计

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  2. java thin方式连接oracle数据库

    本文主要描述通过thin方式连接oracle数据库 1.创建web project ,将D:\oracle\product\10.2.0\db_1\jdbc\lib(oracle安装目录)下的ojdb ...

  3. element的el-tabs控制,以及el-select 多选默认值

    一.el-tabs 1.element自己已经封装好了,当切换时v-model的值自动切换为el-tabs-pane的name对应的值. 如下: <el-tabs v-model='active ...

  4. uboot移植rtc

    uboot中可能会有需求提供rtc的支持目的达到uboot启动也能够进行墙上时间的显示和后面推断.大部分rtc支持的一个必要条件就是已经有i2c的支持.由于非常多的rtc是i2c接口控制的.uboot ...

  5. same-tree——比较两个二叉树是否相同

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. 设计模式之MVC设计模式初阶

    MVC M:Model(数据) V:View(界面) C:Control(控制) 1⃣️Control可以直接访问View和Model 2⃣️View不可以拥有Control和Model属性,降低耦合 ...

  7. oracle select into相关

    自定义参数输出: declare v_test integer :=0 ;beginselect count(*) into v_test  from tf_estate_card t ;dbms_o ...

  8. AMD和Intel的CPU对比

    http://www.lotpc.com/yjzs/5825.html 推荐文章:小白看AMD与intel的cpu架构,AMD慢的原因 CPU核心的发展方向是更低的电压.更低的功耗.更先进的制造工艺. ...

  9. linux中下载JDK 1.7

    今天想linux下安装java,然后就使用wget来下载jdk1.7,结果老是报错,大概意思是cookie有问题.如下图: 然后网上看了一下,下面的地址可以下载: wget --no-cookies ...

  10. poj1206(dp)

    题目链接:http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...