[LeeCode]14. 最长公共前缀
题目链接:https://leetcode-cn.com/problems/longest-common-prefix/
题目描述:
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例:
示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z 。
思路:
思路1:
python特性,取每一个单词的同一位置的字母,看是否相同.
思路2:
取一个单词s,和后面单词比较,看s与每个单词相同的最长前缀是多少!遍历所有单词
思路3:
按字典排序数组,比较第一个,和最后一个单词,有多少前缀相同.
关注我的知乎专栏,了解更多解题方法!
代码:
python
思路一:
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
res = ""
for tmp in zip(*strs):
tmp_set = set(tmp)
if len(tmp_set) == 1:
res += tmp[0]
else:
break
return res
思路二:
python
class Solution:
def longestCommonPrefix(self, s: List[str]) -> str:
if not s:
return ""
res = s[0]
i = 1
while i < len(s):
while s[i].find(res) != 0:
res = res[0:len(res)-1]
i += 1
return res
java
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String res = strs[0];
int i = 1;
while (i < strs.length) {
while (strs[i].indexOf(res) != 0) {
res = res.substring(0, res.length() - 1);
}
i += 1;
}
return res;
}
}
思路三:
python
class Solution:
def longestCommonPrefix(self, s: List[str]) -> str:
if not s:
return ""
s.sort()
n = len(s)
a = s[0]
b = s[n-1]
res = ""
for i in range(len(a)):
if i < len(b) and a[i] == b[i]:
res += a[i]
else:
break
return res
java
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
StringBuilder res = new StringBuilder();
Arrays.sort(strs);
// 字符串转数组
char[] a = strs[0].toCharArray();
char[] b = strs[strs.length - 1].toCharArray();
for (int i = 0; i < a.length; i++) {
if (i < b.length && a[i] == b[i]) {
res.append(a[i]);
}
else{
break;
}
}
return res.toString();
}
}
[LeeCode]14. 最长公共前缀的更多相关文章
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- Java实现 LeetCode 14 最长公共前缀
14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&quo ...
- python(leetcode)-14最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- 【LeetCode】14. 最长公共前缀
题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1:输入: ["flower","flow&quo ...
- 力扣(LeetCode) 14. 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- Leetcode题库——14.最长公共前缀
@author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...
- leetcode 14 最长公共前缀
描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...
- 【leetcode算法-简单】14. 最长公共前缀
[题目描述] 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...
随机推荐
- JSP面试题都在这里
下面是我整理下来的JSP知识点: 图上的知识点都可以在我其他的文章内找到相应内容. JSP常见面试题 jsp静态包含和动态包含的区别 jsp静态包含和动态包含的区别 在讲解request对象的时候,我 ...
- PE知识复习之PE新增节
PE知识复习之PE新增节 一丶为什么新增节.以及新增节的步骤 例如前几讲.我们的PE文件在空白区可以添加代码.但是这样是由一个弊端的.因为你的空白区节属性可能是只读的不能执行.如果你修改了属性.那么程 ...
- jumpserver篇--安装(高可用性 mariadb+haproxy)
1. 需求 为了解决目前登陆方式多种多样,防火墙配置复杂,历史操作无记录,用户权限混乱等等 2. Jumpserver测试环境搭建 2.1. 环境 os:CentOS release 6.8 mini ...
- 巨杉数据库加入CNCF云原生应用计算基金会,共建开源技术生态
近日,巨杉数据库正式加入全球顶级开源社区,云原生应用计算基金会 (Cloud Native Computing Foundation,以下简称CNCF),成为CNCF基金会会员,是中国最早加入的开源云 ...
- WebService简单介绍(一)
分布式系统或软件如何通信?使用WebService服务.说它是服务可以,web通信中间件也ok,web通信组件....... 特点 自包含 自描述 跨平台.跨语言 基于开放和标准 (用了xml,嗯,开 ...
- C# List 集合 交集、并集、差集、去重, 对象集合、 对象、引用类型、交并差补、List<T>
关键词:C# List 集合 交集.并集.差集.去重, 对象集合. 对象.引用类型.交并差.List<T> 有时候看官网文档是最高效的学习方式! 一.简单集合 Intersect 交集, ...
- 用php输出心形曲线
<?php for($t=0;$t<360;$t++) { $y=2*cos($t)-cos(2*$t); //笛卡尔心形曲线函数 $x=2*sin($t)-sin(2*$t); $x+= ...
- 监控工具之zabbix server3.4 部署配置
[root@localhost src]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@localhost s ...
- Android 为TV端助力
记录两次事情: 第一个给view添加动画效果,需要保证view是可以获取焦点的 第二个给listview,GridView设置选择器 listselector时,要保证他的子item无背景,否则选择器 ...
- AndroidTV端的requestFocus()问题
每次开机盒子或者电视的时候,发现给某些控件设置请求焦点 requestFocus 会失效 最终的解决办法就是延时请求 view.postDelayed(new Runnable() { @Overri ...