14. 最长公共前缀

https://leetcode-cn.com/problems/longest-common-prefix/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-23 21:57:58
 * @LastEditTime: 2020-08-23 22:28:04
 * @Description: https://leetcode-cn.com/problems/longest-common-prefix/
 * @FilePath: \leetcode-googtech\#102. Binary Tree Level Order Traversal\14.最长公共前缀.java
 * @WebSite: https://algorithm.show/
 */

/*
 * @lc app=leetcode.cn id=14 lang=java
 *
 * [14] 最长公共前缀
 */

// @lc code=start
class Solution {
    public String longestCommonPrefix(String[] strs) {
        // 待解. . .
    }
}
// @lc code=end

Python

'''
Author: Goog Tech
Date: 2020-08-23 22:07:22
LastEditTime: 2020-08-23 22:11:34
Description: https://leetcode-cn.com/problems/longest-common-prefix/
FilePath: \leetcode-googtech\#102. Binary Tree Level Order Traversal\14.最长公共前缀.py
WebSite: https://algorithm.show/
'''

#
# @lc app=leetcode.cn id=14 lang=python
#
# [14] 最长公共前缀
#

# @lc code=start
class Solution(object):

    # 解题思路: 先找出数组中字典序最小和最大的字符串,
    # 而最长公共前缀即为这两个字符串的公共前缀哟
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if not strs: return ''
        minStr, maxStr = min(strs), max(strs)
        for i in range(len(minStr)):
            if minStr[i] != maxStr[i]:
                return minStr[:i]
        return minStr
# @lc code=end
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""