題目解說
原題目
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:
|
|
Example 2:
|
|
Constraints:
1 <= strs.length <= 2000 <= strs[i].length <= 200strs[i]consists of only lowercase English letters.
翻譯
給定一個陣列strs,裡面會有字串,找出此陣列字串相同的前綴,如果沒有,返回""。
解題策略
邏輯上是後面的字串去跟前面的做比較就好,所以聲明字串pre,一開始就宣告為陣列的第一個元素,如果比對不到,就將pre刪掉一個字符,刪到比對出相同前綴為止。
JS程式碼
|
|