14. Longest Common Prefix 最长的公共字符串开头
[抄题]:
Write a function to find the longest common prefix string amongst an array of strings.
在 "ABCD" "ABEF" 和 "ACEF" 中, LCP 为 "A"
在 "ABCDEFG", "ABCEFG", "ABCEFA" 中, LCP 为 "ABC"
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
字符串数组为空、长度为0的情况都需要考虑
[思维问题]:
知道要从最短的前缀开始找,还以为要排序
[一句话思路]:
每个单词都用.indexof()逐个压缩前缀
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
对前缀字符串进行压缩
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
int indexOf(String str): 返回指定字符串在字符串中第一次出现处的索引,如果此字符串中没有这样的字符串,则返回 -1。
若strs[i].indexOf(pre) == ,则有此前缀。这是判断前缀的新方法。
[关键模板化代码]:
每个单词都要做前缀压缩
for (int i = 1; i < n; i++) {
while (strs[i].indexOf(pre) != 0) {
pre = pre.substring(0, pre.length() - 1);
}
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public String longestCommonPrefix(String[] strs) {
//corner case
if (strs == null) {
return "";
}
if (strs.length == 0) {
return "";
}
//define pre
String pre = strs[0];
int n = strs.length;
//shorten pre
for (int i = 1; i < n; i++) {
while (strs[i].indexOf(pre) != 0) {
pre = pre.substring(0, pre.length() - 1);
}
}
//return
return pre;
}
}
14. Longest Common Prefix 最长的公共字符串开头的更多相关文章
- [LeetCode]14. Longest Common Prefix最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)
1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- [leetcode]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
随机推荐
- BZOJ3261:最大异或和
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...
- java入门很简单之各种循环
1.if结构的语法: <1> 简单的if :if (条件){ //代码块 ...
- xsd解析
1. 解释: <xsd:element name="interceptors"> <xsd:annotation> 这一块是对第一行的注解(解释) < ...
- 关于硬件实现FFT逆运算
前面的文章我们介绍了关于FFT的硬件实现.关于FFT的逆运算IFFT,其实就是将实现FFT的过程反过来执行就可以了. 在实现过程中要注意很多问题. 同 FFT一样,效率问题.以2048点为例,根据理论 ...
- app的apk 安装的方法--adb--命令安装 (含把apk放某个文件夹,每次启动自己安装)
adb安装 1.在app自动化之前,首先手机上有要被测试的app,如何把电脑本地上的app安装到手机上呢?可以在运行自动化代码前,在cmd输入adb指令,把电脑app安装到手机上 adb instal ...
- HBase之二:Hbase优化
1. 预先分区 默认情况下,在创建 HBase 表的时候会自动创建一个 Region 分区,当导入数据的时候,所有的 HBase 客户端都向这一个 Region 写数据,直到这个 Region ...
- Docker Rest API使用入门
Docker Rest API使用入门 系统:Centos7.2, Docker版本信息如下: [python] view plain copy Client: Version: 17.03 ...
- Error 20002 (severity 9):Adaptive Server connection failed
环境: Ubuntu12.10_x64 问题: 用tsql访问SQL Server >> tsql -H U sa Error (severity ): Adaptive Server c ...
- (转)Docker volume plugin - enabled create local volume on docker host
原文地址:https://hub.docker.com/r/cwspear/docker-local-persist-volume-plugin/ Short Description Create n ...
- .NET中使用unity实现aop
Unity是一款知名的依赖注入容器,其支持通过自定义扩展来扩充功能.在Unity软件包内默认包含了一个对象拦截(Interception)扩展定义.本篇文章将介绍如何使用对象拦截功能来帮助你分离横切关 ...