LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接
https://leetcode.com/problems/longest-common-prefix/description/
2. 题目要求
给定一个字符串数组,让你求出该数组中所有字符串的最大公共前缀。例如{"qqwwee", "qqww", "qqfds"}的最大公共前缀为"qq",{"qqwwee", "qqww", "qqfds", "wea"}的最大公共前缀为""(空)。
3. 解题思路
(1)首先判断该字符串数组长度是否为零,为零直接返回最大公共前缀为空;
(2)假设该字符串数组中第一个元素strs[0]为最大公共前缀,然后与第二个元素进行比较,判定第二个元素是否包含假定的最大公共前缀,且假定的最大公共前缀的起始位置为0。满足以上要求,则证明该元素与之前的元素共同拥有该前缀。如果不包含,则证明假定的公共前缀不符合要求,将假定的公共前缀缩短一位,然后重新比较。
4. 代码实现
public class LongestCommenPrefix14 {
public static void main(String[] args) {
String[] strs = {"weeer", "weer", "wer"};
System.out.println(LongestCommenPrefix14.longestCommonPrefix(strs));
}
public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0)
return "";
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) != 0) { //与假定的公共前缀不匹配
prefix = prefix.substring(0, prefix.length() - 1); //缩短假定的公共前缀
if (prefix.equals(""))
return "";
}
}
return prefix;
}
}
LeetCode:14. Longest Commen Prefix(Easy)的更多相关文章
- 「Leetcode」14. Longest Common Prefix(Java)
分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- LeetCode:5. Longest Palindromic Substring(Medium)
原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
随机推荐
- 【[ZJOI2008]骑士】
这道题好暴力啊 发现自己刚学\(OI\)的时候对着这道题写了一个大搜索 发现已经看不懂了 果然我现在菜到连一年半前的我都不如了 这其实是一个基环树\(dp\)啦,基环树上的最大点独立集 其实很简单,我 ...
- Hibernate的属性配置
Hibernate配置属性 hibernate.dialect Hibernate方言(Dialect)的类名 - 可以让Hibernate使用某些特定的数据库平台的特性 取值. full.class ...
- ASP.NET整体运行机制+asp.net请求管道+页面生命周期
- [luoguP4306][JSOI2010]连通数
\[Yeasion\] \[Nein\] 其实我很奇怪为什么我的正解和输出\(N \times N\)的效果是一样的.....嗯,大概是\(RP\)问题吧.... 嗯首先来看一下题目: 题目描述: 度 ...
- PAT 1063. Set Similarity
1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...
- 蓝牙实现对等网络连接 <GameKit/GameKit.h>
/* 1.设置UI界面 2.引入框架 3.点击选择照片 4.连接蓝牙设备 5.实现蓝牙的代理方法 6.发送照片 */ #import "ViewController.h" #imp ...
- udt的java版本judt项目持续升级1.2版本
修改了一些问题,努力兼容udt4版本.具体内容查看项目更新说明: 当前项目版本1.2 地址:https://github.com/jinyuttt/judt
- C#实现文件上传以及文件下载
public ActionResult Upload() { // var pathUrl = "http://" + Request.Url.Authority; var fil ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)-B-杨老师游戏
题目链接:杨老师游戏 题目分析:将9个数字分成3块,分块枚举,话句话说,9个数字的所有排列组合,如果满足N=a*b-c就是一个答案,暴力枚举Orz. 代码如下: #include<iostre ...
- string类中字符的大小写转换
今天做一道题,要用string类,涉及大小写转换,查看了C++文档,string类没有提供这样的方法,只好自己写. 之后是想到一个比较笨的方法,我把string当成一个容器,然后用迭代器一个一个来替换 ...