Longest Common Prefix

 1 var longestCommonPrefix = function(strs) {
 2     if (strs.length < 1) {
 3         return "";
 4     }
 5 
 6     var prefix = strs[0];
 7 
 8     for (var i = 1; i < strs.length; i++) {
 9         if (prefix.length < 1) {
10             return "";
11         }
12 
13         var len = prefix.length < strs[i].length ? prefix.length : strs[i].length;
14 
15         for (var j = 0; j < len; j++) {
16             if (prefix[j] != strs[i][j]) {
17                 break;
18             }
19         }
20 
21         prefix = prefix.substr(0, j);
22     }
23 
24     return prefix;
25 };

 

posted @ 2015-12-24 20:30  叫我霍啊啊啊  阅读(66)  评论(0编辑  收藏  举报