File tree Expand file tree Collapse file tree 7 files changed +138
-0
lines changed
19-remove-nth-node-from-end-of-list
3-longest-substring-without-repeating-characters Expand file tree Collapse file tree 7 files changed +138
-0
lines changed Original file line number Diff line number Diff line change 50
50
- [ ] Add Discuss 31
51
51
- [ ] 实现冒泡排序
52
52
- [ ] 实现选择排序
53
+
54
+ 206
55
+ 3
56
+ 2
57
+ 5
58
+ 19
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @param {number } target
4
+ * @return {number[] }
5
+ */
6
+ var twoSum = function ( nums , target ) {
7
+ const hash = { } ;
8
+
9
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
10
+ if ( hash [ nums [ i ] ] !== undefined ) {
11
+ return [ hash [ nums [ i ] ] , i ] ;
12
+ }
13
+ hash [ target - nums [ i ] ] = i ;
14
+ }
15
+ } ;
Original file line number Diff line number Diff line change
1
+ var removeNthFromEnd = function ( head , n ) {
2
+ let preHead = new ListNode ( 0 ) ;
3
+ preHead . next = head ;
4
+ let fast = preHead ;
5
+ let slow = preHead ;
6
+
7
+ // 快先走 n+1 步
8
+ while ( n -- ) {
9
+ fast = fast . next ;
10
+ }
11
+ // fast、slow 一起前进
12
+ while ( fast && fast . next ) {
13
+ fast = fast . next ;
14
+ slow = slow . next ;
15
+ }
16
+ slow . next = slow . next . next ;
17
+ return preHead . next ;
18
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } l1
10
+ * @param {ListNode } l2
11
+ * @return {ListNode }
12
+ */
13
+ var addTwoNumbers = function ( l1 , l2 ) {
14
+ const pre = new ListNode ( ) ;
15
+ let cur = pre ;
16
+ let carry = 0 ;
17
+
18
+ while ( l1 || l2 ) {
19
+ const sum = ( l1 ? l1 . val : 0 ) + ( l2 ? l2 . val : 0 ) + carry ;
20
+ carry = sum >= 10 ? 1 : 0 ;
21
+ cur . next = new ListNode ( sum % 10 ) ;
22
+ cur = cur . next ;
23
+
24
+ l1 && ( l1 = l1 . next ) ;
25
+ l2 && ( l2 = l2 . next ) ;
26
+ }
27
+
28
+ carry && ( cur . next = new ListNode ( carry ) ) ;
29
+
30
+ return pre . next ;
31
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isValid = function ( s ) {
6
+ const hash = {
7
+ ')' : '(' ,
8
+ ']' : '[' ,
9
+ '}' : '{' ,
10
+ } ;
11
+
12
+ const stack = [ ] ;
13
+ for ( let i = 0 ; i < s . length ; i ++ ) {
14
+ const cur = stack . pop ( ) ;
15
+ if ( cur === undefined ) {
16
+ stack . push ( s [ i ] ) ;
17
+ continue ;
18
+ }
19
+ if ( hash [ s [ i ] ] !== cur ) {
20
+ stack . push ( cur ) ;
21
+ stack . push ( s [ i ] ) ;
22
+ }
23
+ }
24
+
25
+ return stack . length === 0 ;
26
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {ListNode }
11
+ */
12
+ var reverseList = function ( head ) {
13
+ let [ reverse , cur ] = [ null , head ] ;
14
+
15
+ while ( cur ) {
16
+ const next = cur . next ;
17
+ cur . next = reverse ;
18
+ reverse = cur ;
19
+ cur = next ;
20
+ }
21
+
22
+ return reverse ;
23
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ var lengthOfLongestSubstring = function ( s ) {
6
+ let res = 0 ;
7
+ let start = 0 ;
8
+ const map = new Map ( ) ;
9
+
10
+ for ( let i = 0 ; i < s . length ; i ++ ) {
11
+ if ( map . has ( s [ i ] ) ) {
12
+ start = Math . max ( start , map . get ( s [ i ] ) ) ;
13
+ }
14
+ res = Math . max ( res , i - start + 1 ) ;
15
+ map . set ( s [ i ] , i + 1 ) ;
16
+ }
17
+
18
+ return res ;
19
+ } ;
You can’t perform that action at this time.
0 commit comments