#14 Longest Common Prefix
startsWith 請把邏輯寫在紙上會更清楚,因為直接寫 code 思考會常常亂掉 / 這題 Edge Case 很多,值得再寫一次
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 "".
input: 給一字串陣列
output: 找出他們共同 prefix,沒有的話就回傳空字串 ""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.
*/
// Edge Case []
// 只有一個值 ["a"] 那是印 a 還是 "" (印 "a"")
/**
* @param {string[]} strs
* @return {string}
*/
// var longestCommonPrefix = function(strs) {Edge Case
怎麼解



完整程式碼
Last updated