14. Longest Common Prefix【leetcode】
- 判断非空的情况在进行计算
- 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中
- 用第一个串进行逐个对比出最长的串
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs==null || strs.length==0)
return "";
String pr= strs[0]; for(int i =1;i<strs.length;i++){
//用j计算最长串的长度
int j=0; while(j<pr.length()&&j<strs[i].length()&&pr.charAt(j)==strs[i].charAt(j)){
j++;
}
if(j==0){
return "";
}
pr =pr.substring(0,j);
} return pr;
}
}
注意:看到网上有帖子说进行排序,然后对比第一个和最后一个的公共序列,这样是不对的,因为如果中间的串没有公共序列时,返回结果错误
14. Longest Common Prefix【leetcode】的更多相关文章
- 78. Longest Common Prefix【medium】
Given k strings, find the longest common prefix (LCP). Example For strings "ABCD", " ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
随机推荐
- Python进阶 - 命名空间与作用域
Python进阶 - 命名空间与作用域 写在前面 如非特别说明,下文均基于Python3 命名空间与作用于跟名字的绑定相关性很大,可以结合另一篇介绍Python名字.对象及其绑定的文章. 1. 命名空 ...
- 浅谈IOC
一.引言 IOC-Invertion of Control,即控制反转,是一种程序设计思想,世上本没有路,走的人多了便有了路,本文将一步步带你了解IOC设计思想的演进之路. 在学习IOC之前我们先初步 ...
- H3C交换机删除VLAN与其绑定端口配置
在系统视图下,执行 undo int vlan 2 undo vlan 2 可以删除vlan2的配置信息. 执行 undo vlan all 可以删除所有的vlan信息. 在vlan2视图下,执行: ...
- (转载)Bonding技术指南
原文链接:http://www.wushiqin.com/?post=68 一.什么是网卡绑定及简单原理 网卡绑定也称作"网卡捆绑",就是使用多块物理网卡虚拟成为一块网卡,以提供负 ...
- VB6之阴影图层
要是能创建半透明的刷子就好了,就不必像这样以图层的方式实现透明阴影效果. 代码: 'code by lichmama@cnblogs.com '绘制阴影图层 Private Declare Funct ...
- NewsServiceImpl
package com.pb.news.service.impl; import java.util.List; import com.pb.news.dao.NewsDao;import com.p ...
- 把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理
把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理 1 编译安装httpd 把httpd编译安装在/app/httpd/目录下. 2 在/etc/rc.d/init ...
- React环境配置
现在开始配置一个基础项目. 创建项目文件夹:C:\Users\Danny\Desktop\React npm init 创建package.json文件 下面的所有安装,都是--save-dev,因为 ...
- Dev控件学习-GridControl中的BandGridview导出多层行头操作
BandGridview默认导出的是Columns的列头信息,而不是Bands的列头信息,为了实现导出多层行头.代码如下 public static void ExportExcel2(DevExpr ...
- Java语言的9个主要特性
Java作为时下很流行的一门编程语言,受到很多人的热爱,那么它有哪些特性呢?一起来看看吧. 1.Java语言是简单的 Java语言的语法与C语言和C++语言很接近,使得大多数程序员很容易学习和使用.另 ...