【LeetCode】 Longest Common Prefix
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
题目的意图
It seems that it is not to check between pair of strings but on all the strings in the array.
For example:
{"a","a","b"} should give "" as there is nothing common in all the 3 strings.
{"a", "a"} should give "a" as a is longest common prefix in all the strings.
{"abca", "abc"} as abc
{"ac", "ac", "a", "a"} as a.
Logic goes something like this:
Pick a character at i=0th location and compare it with the character at that location in every string.
If anyone doesn't have that just return ""
Else append that character in to the result.
Increment i and do steps 1-3 till the length of that string.
return result.
Make sure proper checks are maintained to avoid index out of bounds error.
代码
public class Solution {
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == )
return "";
String pre = strs[];
int i = ;
while (i < strs.length) {
System.out.println(strs[i].indexOf(pre) );
while (strs[i].indexOf(pre) != )
pre = pre.substring(, pre.length() - );
i++;
}
return pre;
}
}
【LeetCode】 Longest Common Prefix的更多相关文章
- 【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(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- 【Leetcode-easy】Longest Common Prefix
思路:每次从字符数组中读取两个字符串比较.需要注意输入字符串为空,等细节. public String longestCommonPrefix(String[] strs) { if(strs==nu ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- 【SPOJ】Longest Common Substring
[SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- LeetCode Range Addition II
原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/ 题目: Given an m * n matrix M ini ...
- ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...
- 忘记Oracle System和Sys密码的解决方法
忘记Oracle System和Sys密码的方法 :Oracle提供两种验证方式,一种是OS验证,另一种密码文件验证方式,如果是第一种方式用以下方法修改密码: sqlplus /nolog; conn ...
- mysql之 MySQL 主从基于position复制原理概述
1 .主从复制简介MySQL 主从复制就是将一个 MySQL 实例(Master)中的数据实时复制到另一个 MySQL 实例(slave)中,而且这个复制是一个异步复制的过程.实现整个复制操作主要由三 ...
- laravel 导出导入excel和csv文件的 使用
在项目中用到的常用功能数据导入导出 在laravel有插件可以直接使用 方便快捷 学习源头: https://www.cnblogs.com/martianShu/p/5869270.html htt ...
- IDEA运行Java的项目出现页面空白
问题效果: 解决方案: 在发布的时候不应该把Tomcat的jar打包入内.
- springboot或者jetty等启动服务器后,如何去停止这个服务
首先在win7下找到运行,但是win7的运行不像XP那么好找,win7运行的位置在:开始→所有程序→附件→运行. 然后在对话框中,输入cmd(大小写均可). 然后是如何查看80端口的方法,一般 ...
- Codeforces Problem - 38E - Let's Go Rolling!(DP)
E. Let's Go Rolling! time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- JavaScript 中对小数取整的常用函数
常见的js截取小数的方法 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math. ...
- win10/server2019 系统安装 详解
https://www.microsoft.com/zh-cn/software-download/windows10 https://go.microsoft.com/fwlink/?LinkId= ...