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 ...
随机推荐
- Python,正则表达式 - (?:)示例
例如正则表达式a(?:b),匹配后没有包含'b'的分组 >>> string 'ab ac' >>> import re >>> string = ...
- 了解 go 的 Context
go 的 Context 一直对 go 的 Context 一知半解,不了解其用途,因此在这里着重了解一下 go 语言的 Context 飞雪无情的一个博文对 go 的 Context 讲的比较易懂一 ...
- Python(一):一行解法参考
#一行快速排序quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in arra ...
- VMware 搭建linux虚拟机环境
1.任务管理器-服务 确认VMware服务是否启动 2.VMware生成网关地址 编辑--虚拟网络编辑器 VMnet8 NAT设置子网IP,子网掩码,网关 3.windows网络--更改适配器设置-- ...
- vue+vuex项目中怎么实现input模糊查询
1,首先给input框添加方法,但是用的是element-ui的组件,对input进行了封装,不能直接用原生的方法!,在element组件中,input框中方法有实例参数$event,代表事件对象 ...
- Ubuntu中获取和使用uiautomatorviewer
图省事的办法是直接在网上找个android platform tools的某个版本下载下来 比如这个网站里面的:Android SDK工具——Platform-tools 个人比较倾向于先下载andr ...
- Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)
DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把 ...
- Go_栈
1. 栈的介绍 2. 栈的应用 3. 栈入门 package main import ( "fmt" "errors" ) //使用数组来模拟一个栈的使用 ty ...
- 统计字符在字符串中第n次出现的位置
输入一个字符串s,一个数字n和一个字符c,统计这个字符c在字符串s中第n次出现的位置 输入格式: 输入3行.第1行是字符串s,第2行是数字n,第3行是被查找的字符c. 输出格式: 第n个字符在字符串中 ...
- C++记录(一)
1 extern 符表示该变量不是当前作用域定义的,用于声明. 如extern i;表示i不是当前作用域里的,是其他某个include的cpp文件里的变量. 2 int *p=0;相当于初始化p为空指 ...