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 ...
随机推荐
- Pandas处理丢失数据
1.创建含NaN的矩阵 >>> dates = pd.date_range(', periods=6) >>> df = pd.DataFrame(np.arang ...
- Java 日志体系
Java 日志体系 <java 日志和 SLF4J 随想>:http://ifeve.com/java-slf4j-think/ 一.常用的日志组件 名称 jar 描述 log4j log ...
- 2019.02.21 bzoj2739: 最远点(决策单调性+分治)
传送门 题意简述:给一个N个点的凸多边形,求离每一个点最远的点. 思路:先根据初中数学知识证明决策是满足单调性的,然后上分治优化即可. 才不是因为博主懒得写二分+栈优化呢 代码: #include&l ...
- ABP框架系列之三十九:(NLayer-Architecture-多层架构)
Introduction Layering of an application's codebase is a widely accepted technique to help reduce com ...
- Oracle partition by 使用说明
--用法详解 0.select * from wmg_test; ---测试数据 1.select v1,v2,sum(v2) over(order by v2) as sum --按 ...
- 02-jQuery的选择器
我们以前在CSS中学习的选择器有: 今天来学习一下jQuery 选择器. jQuery选择器是jQuery强大的体现,它提供了一组方法,让我们更加方便的获取到页面中的元素. 1.jQuery 的基本选 ...
- WPF 依赖属性&附加属性
依赖属性 暂无 附加属性 1.在没有控件源码的前提下增加控件的属性 2.多个控件需要用到同一种属性 使用附加属性可以减少代码量,不必为每一个控件都增加依赖属性 3.属性不确定是否需要使用 在某些上下文 ...
- 设计模式,Let's “Go”! (上)
code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...
- 2019-4-25 html学习笔记
一.概念 文本 用于储存和记录文字信息的载体 html 超文本标记语言(本质就是给文本增加语义 如<h1></h1>就是给文字添加一级标题的语义) 注:互联网三大基石 有 ...
- Java基础--枚举
1.枚举简介 枚举是由一组固定的常量组成的类型,自定义数据类型. 枚举的常量值一定是可列举的有限值.常量值的类型都是public static final. 下面代码中的Gender 是一种自定义的数 ...