leetcode — longest-common-prefix
/**
* Source : https://oj.leetcode.com/problems/longest-common-prefix/
*
* Created by lverpeng on 2017/7/10.
*
* Write a function to find the longest common prefix string amongst an array of strings.
*/
public class LongestCommonPrefix {
/**
* 依次比较每个字符串的每个字符是否相同
*
*
* @param strArr
* @return
*/
public String findLongestPrefix (String[] strArr) {
String prefix = "";
for (int i = 0; i < strArr[0].length(); i++) {
boolean equal = true;
for (int j = 0; j < strArr.length; j++) {
if (i >= strArr[j].length()) {
equal = false;
break;
}
if (j == 0) {
continue;
}
if (strArr[j].charAt(i) != strArr[j - 1].charAt(i)) {
equal = false;
}
}
if (!equal) {
break;
} else {
prefix += strArr[0].charAt(i);
}
}
return prefix;
}
public static void main(String[] args) {
LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
String[] strArr = new String[]{"abc", "a", "abcd"};
System.out.println("a-------" + longestCommonPrefix.findLongestPrefix(strArr));
strArr = new String[]{"abcsdfg", "abc", "abcdasdf"};
System.out.println("abc-------" + longestCommonPrefix.findLongestPrefix(strArr));
}
}
leetcode — longest-common-prefix的更多相关文章
- [LeetCode] Longest Common Prefix 最长共同前缀
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. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- [LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings. Hide Tags Str ...
- LeetCode——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution { public String get(String a,String b) { if(a==""||b=="") ...
- leetcode Longest Common Prefix python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...
随机推荐
- 五子棋 AI(AIpha-beta算法)
博弈树 下过五子棋的人都应该知道,越厉害的人,对棋面的预测程度越深.换句话讲,就是当你下完一步棋,我就能在我的脑海里假设把我所有可能下的地方都下一遍,然后考虑我下完之后你又会下在哪里,最后我根据每次预 ...
- 记一次Java Core Dump分析过程
#背景提要 很久没有亲自动手部署代码了,命令行的亲切感越来越低.放飞了键盘,习惯了鼠标操作的windows环境.冷不丁实操部署也是不错的. 常常在部署时,运维同学对于[hs_err_pid]文件视而不 ...
- SPARK安装一:Windows下VirtualBox安装CentOS
一.虚拟机安装 重点是网络设置,参见:https://www.linuxidc.com/Linux/2018-04/151924.htm 本文用三台2核4g虚拟机做集群,虚拟机安装centos7,如下 ...
- 3.在自己的bag上运行Cartographer ROS
1.验证自己的bag cartographer ROS提供了一个工具cartographer_rosbag_validate来自动分析包中的数据.在尝试调试cartographer之前运行这个工具. ...
- 1.4eigen中的块运算
1.4 块运算 块是矩阵或数组的一个矩形部分.块表达式既可以做左值也可以作右值.和矩阵表达式一样,块分解具有零运行时间成本,对你的程序进行优化. 1.使用块运算 最常用的块运算是.block()成员函 ...
- [Bat]如何彻底关闭每个盘符默认的共享$(即使重启也有效)
Windows启动时都会默认打开admin$ ipc$ 和每个盘符的共享,对于不必要的默认共享,一般都会把它取消掉,可当又需要打开此默认共享时,又该从哪里设置呢,一般来说有两个地方,MSDOS命令和计 ...
- SimpleDateFormat的parse(String str)方法的用法
SimpleDateFormate 中的parse 方法可以将string类型的字符串转换成特定的date的特定类型.
- windows下angularJs环境搭建和遇到的问题解决
搭建本地开发环境 angular官网社区上说:你应该在自己的电脑上本地开发... 你也应该在本地环境学习 Angular. 本人也认为在本地搭建学习环境--靠谱.所以决定尝试一下. 安照中文社区给的步 ...
- nginx安装以及调优
目录: 1.安装nginx 2.配置nginx 3.调优nginx 4.性能测试 ps:为了方便,文档使用docker容器来操作的. 1.安装nginx 1.1 启动容器.download nginx ...
- _ZNote_Mac_技巧_QuickLook功能扩展
QuicLook(快速查看)是macOS一项非常方便的独有功能: 当选中一个文件,只需要按下空格键即可查看其内容,在按下空格退出QuickLook, 不需要启动再关闭任何软件. 默认支持大部分视频.音 ...