【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/
题目:Write a function to find the longest common prefix string amongst an array of strings.
解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀。依次与后面的元素进行比較。取其公共部分,比較结束后。剩下的就是该字符串数组的最长公共前缀。演示样例代码例如以下:
public class Solution
{
public String longestCommonPrefix(String[] strs)
{
if (strs == null || strs.length == 0)
{
return "";
}
String prefix = strs[0];
for (int i = 1; i < strs.length; i++)
{
int j = 0;
while (j < strs[i].length() && j < prefix.length() && strs[i].charAt(j) == prefix.charAt(j))
{
j++;
}
if (j == 0)
{
return "";
}
prefix = prefix.substring(0, j);
}
return prefix;
}
}
【LeetCode OJ 14】Longest Common Prefix的更多相关文章
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- leetcode第14题--Longest Common Prefix
Problems: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. 分析 该题目是求一个 ...
- 【牛客网】Longest Common Subsequence
[牛客网]Longest Common Subsequence 发现只有d数组最格路 于是我们把前三个数组中相同的数记成一个三维坐标,同一个数坐标不会超过8个 从前往后枚举d,每次最多只会更新不超过8 ...
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- 【LeetCode每天一题】Longest Common Prefix(最长前缀)
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode】 Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
随机推荐
- 前端学习之路——scss篇
一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 二.安装和使用 Sass依赖于ruby环境,所以装sass之前先 ...
- 页面加载完成触发input[type="file"]控件问题
由于浏览器厂家的限制,不同的浏览器不开放页面加载完成就允许触发input[type="file"]控件 测试 Chrome .火狐 .IE .微信客户端QQ =>桌面端: C ...
- js去掉字符串得第一个字符 、去掉字符串的最后一个字符
a1 = aa[0].slice(1); //去掉第一个字符串 a2 = a2.substr(0, a2.length - 1);
- Jtester使用
1.在Jtester中使用DataMap 为什么要使用DataMap? 早先的jTester中提供了dbFit方式来准备和验证数据库数据,应该来说,这个工具解决了很多问题.实际使用过程中,开发同学反映 ...
- docker私有仓库yum安装 docker-registry
[root@riyimei-node1:/root]> yum install docker-registryLoaded plugins: fastestmirror, langpacksLo ...
- Java 学习(11): 面向对象编程—继承(super,this)
Java 继承 what: 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为.子类从它的父类中继承可访问的数据域和方法,也 ...
- securefx连接linux后文件夹中文乱码问题解决
首先在选项中设置字符编码为UTF-8 然后在全局选项中找到Securefx的配置文件 进入到该目录中,选择“Sessions”: 在“Sessions”中找到链接地址的ini文件,并用文本编辑器打开: ...
- layDate1.0正式公布,您一直在寻找的的js日期控件
你是时候换一款日期控件了,而layDate很愿意和您成为工作伙伴.她正致力于成为全球最高大上的web日期支撑,为国内外全部从事web应用开发的同仁提供力所能及的动力.她基于原生JavaScript精心 ...
- hadoop-09-安装资源上传
hadoop-09-安装资源上传 在/software/www/html 下面上传 ambari HDP HDP-UTILS-1.1.0.21 文件,之后解压:
- SWT自定义选项卡CTabFolder
SWT自定义选项卡CTabFolder 学习了:http://blog.csdn.net/dreajay/article/details/17391731 package com.swt; impor ...