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 ...
随机推荐
- SpringMVC中的Interceptor拦截器及与Filter区别
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- hp visual user generator
loadrunder 自动化测试 脚本 用例
- 借助Algorithmia网站API:用AI给黑白照片上色,复现记忆中的旧时光
先看DEMOhttps://demos.algorithmia.com/colorize-photos/ 了解ColorfulImageColorizationhttps://algorithmia. ...
- mui.init方法配置
mui框架将很多功能配置都集中在mui.init方法中,要使用某项功能,只需要在mui.init方法中完成对应参数配置即可,目前支持在mui.init方法中配置的功能包括: 创建子页面. 关闭页面. ...
- selenium之生成html测试报告--testng.xsl
自制版制作步骤: 1.首先下载一个文件名为testng.xslt-1.1.zip testng.xslt-1.1我在印象笔记里面备份了一份 打开testng.xslt中lib文件夹,找到saxon-8 ...
- JavaScript基础视频教程总结(041-050章)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- _ZNote_Qt_Tips_添加动态链接库
之前添加都是 手写添加,今天陈老师提示可以在 .pro 文件内空白处,右键弹出添加
- Alpha阶段项目复审(冲鸭队)
Alpha阶段项目复审(冲鸭队) 组名 优点 缺点 排名 天冷记得穿秋裤队 支持文件离线开源下载,没有限速 部分功能未实现 1 中午吃啥队 点餐系统用户需求较高,系统功能完善 界面可以再完善一下些 2 ...
- Js之设置日期时间 判断日期是否在范围内
var now = new Date(); var startDate = new Date(); startDate.setFullYear(2018, 08, 07); startDate.set ...
- Django _VIEW视图_源码分析
Django _VIEW视图: 1. 点击as_view方法. 第二步: as_view () 为VIEW 类里定义的,到时候我们定义业务逻辑的类就继承这个VIEW类. view方法内返回的是disp ...