File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 5
5
| 1| [ 两数之和] ( https://leetcode-cn.com/problems/two-sum/ ) | [ JavaScript] ( ./algorithms/two-sum.js ) | Easy|
6
6
| 2| [ 两数相加] ( https://leetcode-cn.com/problems/add-two-numbers/ ) | [ JavaScript] ( ./algorithms/add-two-numbers.js ) | Medium|
7
7
| 9| [ 回文数] ( https://leetcode-cn.com/problems/palindrome-number/ ) | [ JavaScript] ( ./algorithms/palindrome-number.js ) | Easy|
8
-
8
+ | 14 | [ 最长公共前缀 ] ( https://leetcode-cn.com/problems/longest-common-prefix/ ) | [ JavaScript ] ( ./algorithms/longest-common-prefix.js ) | Easy |
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } strs
3
+ * @return {string }
4
+ */
5
+ var longestCommonPrefix = function ( strs ) {
6
+ if ( ! strs [ 0 ] . length ) {
7
+ return "" ;
8
+ }
9
+
10
+ const first = strs [ 0 ] ;
11
+
12
+ for ( let i = 0 ; i <= first . length ; i ++ ) {
13
+ for ( let j = 1 ; j < strs . length ; j ++ ) {
14
+ if ( first [ i ] !== strs [ j ] [ i ] ) {
15
+ return first . slice ( 0 , i ) ;
16
+ }
17
+ }
18
+ }
19
+ } ;
You can’t perform that action at this time.
0 commit comments