zoukankan      html  css  js  c++  java
  • 【LeetCode】14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings.

    题意:找出所给几个字符串的相同前缀

    思路:用第一个字符串和之后的所有字符串进行对比,标示出相同字符串的超尾指针就行

    ps:通过这个题发现自己Python基础好差啊

     1 class Solution(object):
     2     def longestCommonPrefix(self, strs):
     3         """
     4         :type strs: List[str]
     5         :rtype: str
     6         """
     7         if len(strs)==0: return ''
     8         str = strs[0]
     9         min = len(str)
    10         for i in range(1, len(strs)):
    11             j=0
    12             tmp = strs[i]
    13             while j<min and j<len(tmp) and str[j] == tmp[j]:
    14                 j+=1
    15             if j<min: min = j
    16         
    17         return str[:min]

     耗时84ms,排名太靠后了,不能忍

     1 class Solution(object):
     2     def longestCommonPrefix(self, strs):
     3         """
     4         :type strs: List[str]
     5         :rtype: str
     6         """
     7         if len(strs)==0: return ''
     8         str = strs[0]
     9         min = len(str)
    10         for tmp in strs[1:]:
    11             j=0
    12             while j<min and j<len(tmp) and str[j]==tmp[j]:
    13                 j+=1
    14             if j<min: min=j
    15         
    16         return str[:min]

     耗时59ms,好了点,但还是不理想

     1 class Solution(object):
     2     def longestCommonPrefix(self, strs):
     3         """
     4         :type strs: List[str]
     5         :rtype: str
     6         """
     7         if len(strs)==0: return ''
     8         str = strs[0]
     9         min = len(str)
    10         for tmp in strs[1:]:
    11             j = 0
    12             l = len(tmp)
    13             while j<min and j<l and str[j]==tmp[j]:
    14                 j+=1
    15             if j<min: min=j
    16         
    17         return str[:min]

    把重复计算的min = len(str)保存后,变成了49ms,len()耗时挺长啊,感觉还能继续改

     1 class Solution(object):
     2     def longestCommonPrefix(self, strs):
     3         """
     4         :type strs: List[str]
     5         :rtype: str
     6         """
     7         if len(strs)==0: return ''
     8         str = strs[0]
     9         min = len(str)
    10         for tmp in strs[1:]:
    11             j = 0
    12             l = len(tmp)
    13             while j<min and j<l and str[j]==tmp[j]:
    14                 j+=1
    15             min=j     #不用判断,j的值肯定小于min,直接更新
    16         
    17         return str[:min]

    又减了4ms

    感觉还能改,但是不知道怎么改了。。。

    没办法,

    只有这样了。。

    !!!!

    0ms

     1 char* longestCommonPrefix(char** strs, int strsSize) {
     2     if(!strsSize) return "";
     3     char *str=strs[0];
     4     int i,j;
     5     for(i=1;i<strsSize;i++){
     6         j=0;
     7         while(str[j]&&strs[i][j]&&str[j]==strs[i][j])
     8             j++;
     9         str[j]='';
    10     }
    11     return str;
    12 }
  • 相关阅读:
    open-falcon之agent
    centos 7 部署 open-falcon 0.2.0
    高可用Redis服务架构分析与搭建
    python操作mongo脚本
    mongo查询日期格式数据
    离线下载pip包安装
    mongo同步到es
    mongo ttl索引
    kibana多台服务部署
    logstash过滤配置
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6210846.html
Copyright © 2011-2022 走看看