1. 程式人生 > >LeetCode: 14. 最長公共字首

LeetCode: 14. 最長公共字首

題目

編寫一個函式來查詢字串陣列中最長的公共字首字串。

分析

依次比較

解題

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        String ret = strs[0];
        for (int i = 0; i< strs.length-1; i++) {
            ret =  compare(ret, strs[i+1
]); if (ret.length() == 0) { return ret; } } return ret; } public String compare(String s1, String s2) { int min = Math.min(s1.length(), s2.length()); if (min == 0) { return ""; } for (int i = 0
; i< min; i++) { if (s1.charAt(i) != s2.charAt(i)) { return s1.substring(0, i); } } return s1.length() == min ? s1 : s2; } }

啟示

此題較為簡單,依次比較而不是兩兩比較

相關推薦

leetcode 14. 公共字首

編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸

LeetCode(14. 公共字首)

演算法描述 : 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl"

Leetcode 14.公共字首(Python3)

14.最長公共字首 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸入: ["dog","racec

LeetCode 14. 公共字首(C、C++、python)

編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸入: ["dog","racecar","car"] 輸

LeetCode 14. 公共字首 Longest Common Prefix(C語言)

題目描述: 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 “”。 示例 1: 輸入: [“flower”,“flow”,“flight”] 輸出: “fl” 示例 2: 輸入: [“dog”,“racecar”

LeetCode-14 公共字首

文章目錄 題目描述 我的解法 反思1 優化1 反思2 優化2 其他思路 總結 Github 題目描述 編寫一個函式來查詢字串

Leetcode 14 公共字首 python

編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 “”。 示例 1: 輸入: [“flower”,“flow”,“flight”] 輸出: “fl” 示例 2: 輸入: [

leetcode-14公共字首

leetcode-14最長公共字首

LeetCode: 14. 公共字首

題目 編寫一個函式來查詢字串陣列中最長的公共字首字串。 分析 依次比較 解題 class Solution { public String longestCommonPre

Python, LeetCode, 14. 公共字首

class Solution:     def longestCommonPrefix(self, strs):         """         :type strs: List[str]         :rtype: str         """        

LeetCode(14)公共字首

編寫一個函式來查詢字串陣列中的最長公共字首。如果不存在公共字首,返回空字串 ""。示例 1:輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2:輸入: ["dog","racecar","car"] 輸出: "" 解釋: 輸入不存在公共字

leetcode簡單題14. 公共字首

class Solution: def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ if(l

LeetCode公共字首14

LeetCode:最長公共字首【14】 題目描述 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸入: ["dog","

LeetCode】#14公共字首(Longest Common Prefix)

【LeetCode】#14最長公共字首(Longest Common Prefix) 題目描述 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 “”。 示例 示例 1: 輸入: [“flower”,“flow”,“flight”] 輸出: “

LeetCode -[簡單]-14. 公共字首-Java實現

編寫一個函式來查詢字串陣列中的最長公共字首。如果不存在公共字首,返回空字串 ""。示例 1:輸入:["flower","flow","flight"]輸出:["fl"]說明:所有輸入只包含小寫字母 a-z 。程式碼:class Solution { public String longestCommo

LeetCode14. 公共字首(C++)

題目: 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸入: ["dog","racecar","car"] 輸出

LeetCode題目-- 公共字首(python實現)

題目 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例 1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例 2: 輸入: ["dog",

Python實現-14.公共字首

題目描述: 編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 示例1: 輸入: ["flower","flow","flight"] 輸出: "fl" 示例:2 輸入: ["dog","racecar","car"] 輸出:

14. 公共字首

/* 思路: 最長公共字首的意思就是從第一個字母開始的每個字串相同的字首 1、首先找到最短的那個字串的長度和最短的那個字串的下標 2、用兩個迴圈 第一個迴圈,遍歷每個字串的第i個字母 第二個迴圈,遍歷每個字串 3、判斷每個字串的第i個字元是否

leecode:第14題:14. 公共字首

編寫一個函式來查詢字串陣列中的最長公共字首。 如果不存在公共字首,返回空字串 ""。 if (strs.length < 1 || strs == null) {             return "";         }