开始容易,坚持不易,愿你我都能坚持下去。
14. Longest Common Prefix
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: Input: strs = [“flower”,“flow”,“flight”] Output: “fl” Example 2: Input: strs = [“dog”,“racecar”,“car”] Output: “” Explanation: There is no common prefix among the input strings.
思路:以数组中第一个单词为基准,依次遍历剩下的。
var longestCommonPrefix = function(strs) { if(!strs.length) return ''; let cp = ''; for(let index = 0;index<strs[0].length;index++){ for(let i=1;i<strs.length;i++){ if(index>strs[i].length || strs[i][index]!==strs[0][index]){ return cp } } cp += strs[0][index] } return cp };What the devil is ‘this’!
这篇文章讲的是js中的恶魔-this
这篇文章还挺有实用性的,感兴趣的自己去看原文吧~
无
无
