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 ...
随机推荐
- 团队合作之Scrum
CCSU小助手 一:开发团队简介 队名:瓜队 组员:钟文兴.周畅.吉刘磊.唐仲勋 宣言:We are a team at any time! 团队项目描述: 内容:“生活在长大”: 目标:为了方便对学 ...
- (第六场)Singing Contest 【模拟】
题目链接:https://www.nowcoder.com/acm/contest/144/A 标题:A.Singing Contest | 时间限制:1 秒 | 内存限制:256M Jigglypu ...
- stn,spatial transformer network总结
对整篇paper的一个总结:https://blog.csdn.net/xbinworld/article/details/69049680 github:1.https://github.com/D ...
- c#用链表存储并读取配置文件(代码部分)
//读取配置参数 private void loadcofig() { //List<param> camra1 = new List<param>(); List<st ...
- 根据ip确定城市
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- java读取资源文件(Properties)
四步: java代码 //new一个读取配置文件 Properties properties=new Properties(); //获取文件路径 String path=request.getSer ...
- 【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
洛谷P3627:https://www.luogu.org/problemnew/show/P3627 思路 由于有强连通分量 所以我们可以想到先把整个图缩点 缩点完之后再建一次图 把点权改为边权 并 ...
- Python—面向对象04 绑定方法
坚持把梳理的知识都给记下来....... 嗯哼哼 1.绑定方法与非绑定方法 在类内部定义的函数,分为两大类: 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_m ...
- 用c#语言编写水仙花数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 制作npm插件vue-toast-m实例练习
制作npm插件vue-toast-m实例练习(消息弹窗) 一.使用npm插件 import VueToast from 'vue-toast-demo-cc' Vue.use(VueToast) th ...