leedCode练题——14. Longest Common Prefix

1、题目

14. Longest Common Prefix

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   : [email protected]# @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([""]))

原文地址:https://www.cnblogs.com/Smart-Cat/p/12240688.html

时间: 2024-11-05 23:22:37

leedCode练题——14. Longest Common Prefix的相关文章

【LeetCode】LeetCode——第14题:Longest Common Prefix

14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Submissions: 345681 Difficulty: Easy Write a function to find the longest common prefix string amongst an array of strings. Subscribe to see which compan

14. Longest Common Prefix【leetcode】

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 寻找一个数组中最长的公共前缀 例如["baaa","caaabbb","aaaa"]输出"aaa" 结题思路: 判断非空的情况在进行计算 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中 用第一

leetCode 14. Longest Common Prefix 字符串

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 题目大意:求一组字符串的最长前缀. 代码如下: class Solution { public:     string longestCommonPrefix(vector<string>& strs) {         if(strs.size() == 0)

No.14 Longest Common Prefix

No.14 Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 求一组string的最长公共前缀想法:找到最短的那个,然后依次对比 典型的字符串操作,从前向后比较即可 1 #include "stdafx.h" 2 #include <string> 3 #include <vector> 4 #i

[LeetCode][Python]14: Longest Common Prefix

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-common-prefix/14: Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.===Comments by Dabay===注意边

Java [leetcode 14] Longest Common Prefix

小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 该问题就是找到所有数组字符串里面的最长相同前字串.所以我的思路是先找到数组中最短的那个字符串,然后每次比较的时候最多循环该长度就行,这样避免字符串下标溢出的问题.设置StringBuilder对象用于存

LeetCode记录之14——Longest Common Prefix

本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest common prefix string amongst an array of strings. 编写一个函数来查找字符串数组中最长的公共前缀字符串. 1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3

【LeetCode】14 - Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. Solution: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { //runtime:4ms 4 string str=""; 5 if(strs.empty())return

[LeetCode][14]Longest Common Prefix解析 两种算法和底层源码的深入对比-Java实现

Q: Write a function to find the longest common prefix string amongst an array of strings. A: 这题的大概意思就是说给你一组字符串找出其中最长的哪个通用的前缀出来.这个东西不难找,但是如何找的又快又好不简单.其实这题本来就是easy题,但是却让我联想到了<数据结构与算法分析>上的一道题目,那道题目是这样的: 给一个8900个字的字典,从中间找出类似abc.bbc.abb这样单词中只有一个字母不同的单词进行