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 ...
随机推荐
- jstl_core标签库
先导入这个 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 1 & ...
- jsp_include
jsp__include指令先包含后编译 include 行为 先编译后包含 <jsp:include page="head.jsp"></jsp:include ...
- session 与 cookie 区别
一.Session的概念 Session 是存放在服务器端的,类似于Session结构来存放用户数据,当浏览器 第一次发送请求时,服务器自动生成了一个Session和一个Session ID用来唯一标 ...
- error: device not found
C:\Users\Administrator>adb shell error: device not found 出现上面情况,首先检查设备管理器中,安卓的驱动是否安装OK? 如果驱动 ...
- webdriver常用API
本章涉及Selenium WebDriver的所有接口. Recommended Import Style 推荐的导入风格如下: from selenium import webdriver 然后,你 ...
- Linux之安装软件
1. 下载获得redis-3.0.4.tar.gz后将它放入我们的Linux目录/opt 2. 在SecureCRT界面上点SecureFX图标 在本地窗口中找到要上传的文件 在要上传的文件上点右键 ...
- sklearn中的predict与predict_proba的区别(得到各条记录每个标签的概率(支持度))
假定在一个k分类问题中,测试集中共有n个样本.则: predict返回的是一个大小为n的一维数组,一维数组中的第i个值为模型预测第i个预测样本的标签: predict_proba返回的是一个n行k列的 ...
- SpringBoot中RedisTemplate订阅发布对象
解说 RedisMessageListenerContainer Redis订阅发布的监听容器,你的消息发布.订阅配置都必须在这里面实现 addMessageListener(MessageListe ...
- 1095 Cars on Campus
题意:给出N量车的车牌号,进出的时间,进/出状态.然后给出若干个查询,要求计算在每一查询时刻校园内停着的汽车数量,最后输出这一天中停放时间最长的车辆(若车不止一辆,则按字典序输出)以及停放时间.注:查 ...
- 如何搭建struts2框架
一.首先,下载5个Struts2核心jar包: commons-logging-1.1.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar struts2-core- ...