leetCode练题——14. Longest Common Prefix
1、题目
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
2、我的解法
# -*- coding: utf-8 -*-
# @Time : 2020/1/29 12:28
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 14. Longest Common Prefix.py from typing import List class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
d = len(strs)
minLength = 999999
for str in strs:
thisLength = len(str)
if thisLength < minLength:
minLength = thisLength
res = ""
if d > 0:
for i in range(minLength):
n = 0
r = strs[0][i]
for j in range(d):
if r == strs[j][i]:
n = n + 1
if n == d:
break
else:
return res
res = res + r
return res
else:
return res # print(Solution().longestCommonPrefix(["flower", "flow", "flight"]))
print(Solution().longestCommonPrefix([""]))
leetCode练题——14. Longest Common Prefix的更多相关文章
- LeetCode记录之14——Longest Common Prefix
本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
随机推荐
- MyBatis(4)——配置文件优化
配置文件优化 执行流程:读取配置流程->sqlSessionFactory->sqlSession(连接.读取sql并执行相应操作.关闭) a)配置优化:通过中文参考指南的说明可知-> ...
- cef源码分析之cefsimple
下面是cefsimple的入口代码,主要分成两个部分 // Entry point function for all processes. int APIENTRY wWinMain(HINSTANC ...
- Spring 属性依赖注入
1.1 属性依赖注入 依赖注入方式:手动装配 和 自动装配 手动装配:一般进行配置信息都采用手动 基于xml装配:构造方法.setter方法 基于注解装配: 自动装配:struts和spring ...
- Ansible - 配置文件
概述 再水一发 ref Ansible Configuration Settings 1. 查看 概述 查看 配置文件 的默认位置 命令 > ansible --version 结果 confi ...
- PowerDesigner--comment和name互相复制
1.comment复制到name 脚本代码: Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl Set ...
- 输入两个正整数num1、num2,计算并输出它们的和、差、积、整数商和余数
课本例题 /*输入两个正整数num1.num2,计算并输出它们的和.差.积.整数商和余数.*/ #include<stdio.h> int main() { int num1, num2; ...
- NMAP输出结果中CPE的含义【转】
CPE全称是Common Platform Enumeration,意思是通用平台枚举项:它是NMAP对识别出来的软件.操作系统和硬件的一种命名方式:格式如下: cpe:/<part>:& ...
- 通过python代码连接MySQL以及插入和显示数据
通过python代码连接MySQL以及插入和显示数据: 数据库huahui创建一个shibie的表,里面有两个varcahr(100)的字段,num和result. import pymysql im ...
- 如何创建Maven项目和Spring IOC例子
把如何创建Maven项目和创建Spring IOC的例子分享给大家,希望能对大家有帮助! 我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/12 ...
- 第二次写linux驱动总结
第一次写驱动是在去年,2019年十月份左右.当时是看着韦老师的视频一步步完成的.其中经历了很多error.搭建环境花费了很多精力.时间来到了2020年2月19日星期三,韦老师新视频出来了,我跟着再来了 ...