File tree Expand file tree Collapse file tree 8 files changed +239
-0
lines changed Expand file tree Collapse file tree 8 files changed +239
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=1 lang=javascript
3
+ *
4
+ * [1] 两数之和
5
+ * 1.双层循环的时间复杂度为O(n^2),效率很低。
6
+ * 利用哈希表来存放整数及其下标,能使寻找整数下标的时间复杂度降为O(1),
7
+ * 即相当于减少了一层循环,整体的时间复杂度变为O(n)。
8
+ * 2.如果没有符合要求的整数,可返回[-1,-1]。
9
+ */
10
+
11
+ // @lc code=start
12
+ /**
13
+ * @param {number[] } nums
14
+ * @param {number } target
15
+ * @return {number[] }
16
+ */
17
+ var twoSum = function ( nums , target ) {
18
+ const numsHash = { } ;
19
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
20
+ const num1 = nums [ i ] ,
21
+ num2 = target - num1 ,
22
+ j = numsHash [ num2 ] ;
23
+ if ( j !== undefined ) return [ i , j ] ;
24
+ numsHash [ num1 ] = i ;
25
+ }
26
+ return [ - 1 , - 1 ] ;
27
+ } ;
28
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=2 lang=javascript
3
+ *
4
+ * [2] 两数相加
5
+ * 1.因为链表中的位数为逆序,所以应顺序相加链表。
6
+ * 2.注意处理有进位的情况。
7
+ */
8
+
9
+ // @lc code=start
10
+ /**
11
+ * Definition for singly-linked list.
12
+ * function ListNode(val) {
13
+ * this.val = val;
14
+ * this.next = null;
15
+ * }
16
+ */
17
+ /**
18
+ * @param {ListNode } l1
19
+ * @param {ListNode } l2
20
+ * @return {ListNode }
21
+ */
22
+ var addTwoNumbers = function ( l1 , l2 ) {
23
+ const list = new ListNode ( 0 ) ;
24
+ let head = list ,
25
+ carry = false ;
26
+ while ( l1 !== null || l2 !== null || carry ) {
27
+ let sum = Number ( carry ) ;
28
+ if ( l1 !== null ) {
29
+ sum += l1 . val ;
30
+ l1 = l1 . next ;
31
+ }
32
+ if ( l2 !== null ) {
33
+ sum += l2 . val ;
34
+ l2 = l2 . next ;
35
+ }
36
+ carry = sum >= 10 ;
37
+ sum = carry ? sum - 10 : sum ;
38
+ head . next = new ListNode ( sum ) ;
39
+ head = head . next ;
40
+ }
41
+ return list . next ;
42
+ } ;
43
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=20 lang=javascript
3
+ *
4
+ * [20] 有效的括号
5
+ * 1.遇到左括号时,该括号入栈
6
+ * 2.遇到右括号时,如该括号与栈顶括号对应,则出栈,否则返回false
7
+ * 3.最后判断栈长度是否为0,如果不为0,返回true
8
+ */
9
+
10
+ // @lc code=start
11
+ /**
12
+ * @param {string } s
13
+ * @return {boolean }
14
+ */
15
+ var isValid = function ( s ) {
16
+ const stack = [ ] ,
17
+ lefts = [ "(" , "{" , "[" ] ,
18
+ rights = [ ")" , "}" , "]" ] ;
19
+ for ( let char of s ) {
20
+ const [ i , j ] = [ lefts . indexOf ( char ) , rights . indexOf ( char ) ] ;
21
+ if ( i !== - 1 ) stack . push ( i ) ;
22
+ else if ( j !== stack . pop ( ) ) return false ;
23
+ }
24
+ return stack . length === 0 ;
25
+ } ;
26
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=3 lang=javascript
3
+ *
4
+ * [3] 无重复字符的最长子串
5
+ * 1.用哈希表存放字符及其下标。遍历s时,可借此判断是否出现重复字符。
6
+ * 当前字符下标i减去重复字符下标j,即是以当前字符为末端字符的无重复字符子串的长度len。
7
+ * 2.因为j只能往右走,所以当新j大于旧j时,才更新j。j初始为-1。
8
+ * 3.用max表示结果,max初始为0。如果len大于max,才更新max。
9
+ */
10
+
11
+ // @lc code=start
12
+ /**
13
+ * @param {string } s
14
+ * @return {number }
15
+ */
16
+ var lengthOfLongestSubstring = function ( s ) {
17
+ const charHash = { } ;
18
+ let left = - 1 ,
19
+ max = 0 ;
20
+ for ( let i = 0 ; i < s . length ; i ++ ) {
21
+ const char = s [ i ] ,
22
+ j = charHash [ char ] ;
23
+ if ( j !== undefined && j > left ) left = j ;
24
+ max = Math . max ( max , i - left ) ;
25
+ charHash [ char ] = i ;
26
+ }
27
+ return max ;
28
+ } ;
29
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=4 lang=javascript
3
+ *
4
+ * [4] 寻找两个正序数组的中位数
5
+ * 1.既然要求时间复杂度为O(log(m+n)),那么应该想到二分法。
6
+ * 思路比较巧妙,三言两语讲不清楚,建议直接看官方题解。
7
+ */
8
+
9
+ // @lc code=start
10
+ /**
11
+ * @param {number[] } nums1
12
+ * @param {number[] } nums2
13
+ * @return {number }
14
+ */
15
+ var findMedianSortedArrays = function ( nums1 , nums2 ) {
16
+ // 把短的数组放前面,便于后面比较
17
+ const len1 = nums1 . length ,
18
+ len2 = nums2 . length ;
19
+ if ( len1 > len2 ) return findMedianSortedArrays ( nums2 , nums1 ) ;
20
+ // 如果总长度为奇数,则第k个数为中位数
21
+ // 如果总长度为偶数,则第k和第k+1个数为中位数
22
+ const K = ( ( len1 + len2 ) / 2 ) >> 1 ;
23
+ let k = K ,
24
+ i = ( k >> 1 ) - 1 ,
25
+ j = ( k >> 1 ) - 1 ,
26
+ num1 ,
27
+ num2 ;
28
+ while ( i + j + 1 < K ) {
29
+ i = Math . min ( len1 - 1 , i ) ;
30
+ j = Math . min ( len2 - 1 , j ) ;
31
+ num1 = nums1 [ i ] ;
32
+ num2 = nums2 [ j ] ;
33
+ k = K - i - j ;
34
+ if ( num1 >= num2 ) j += k >> 2 ;
35
+ else i += k >> 2 ;
36
+ }
37
+ console . log ( K , k , i , j ) ;
38
+ return 0 ;
39
+ } ;
40
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=94 lang=javascript
3
+ *
4
+ * [94] 二叉树的中序遍历
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * Definition for a binary tree node.
10
+ * function TreeNode(val) {
11
+ * this.val = val;
12
+ * this.left = this.right = null;
13
+ * }
14
+ */
15
+ /**
16
+ * @param {TreeNode } root
17
+ * @return {number[] }
18
+ */
19
+ var inorderTraversal = function ( root ) {
20
+ /* 迭代 */
21
+ const rets = [ ] ;
22
+
23
+ /* 递归
24
+ const rets = [];
25
+ if (root !== null) traverse(root);
26
+ function traverse(root) {
27
+ const { val, left, right } = root;
28
+ if (left !== null) traverse(left);
29
+ rets.push(val);
30
+ if (right !== null) traverse(right);
31
+ }
32
+ return rets;
33
+ */
34
+ } ;
35
+ // @lc code=end
Original file line number Diff line number Diff line change
1
+ ## 说明
2
+
3
+ 这是我的 LeetCode 题解。
4
+
5
+ 以` 1.两数之和.js ` 为例,对文件格式进行说明:
6
+
7
+ ``` js
8
+ /*
9
+ * @lc app=leetcode.cn id=1 lang=javascript
10
+ *
11
+ * 【编号】【题目】
12
+ * [1] 两数之和
13
+ * 【解题思路】
14
+ * 1.双层循环的时间复杂度为O(n^2),效率很低。
15
+ * 利用哈希表来存放整数及其下标,能使寻找整数下标的时间复杂度降为O(1),
16
+ * 即相当于减少了一层循环,整体的时间复杂度变为O(n)。
17
+ * 2.如果没有符合要求的整数,可返回[-1,-1]。
18
+ */
19
+
20
+ // @lc code=start
21
+ /**
22
+ * @param {number[]} nums
23
+ * @param {number} target
24
+ * @return {number[]}
25
+ */
26
+ var twoSum = function (nums , target ) {
27
+ // 【代码】
28
+ };
29
+ // @lc code=end
30
+ ```
Original file line number Diff line number Diff line change
1
+ {
2
+ "folders" : [
3
+ {
4
+ "name" : " leetcode-solutions" ,
5
+ "path" : " ."
6
+ }
7
+ ]
8
+ }
You can’t perform that action at this time.
0 commit comments