File tree Expand file tree Collapse file tree 2 files changed +76
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 3. Longest Substring Without Repeating Characters
3
+ * Given a string s, find the length of the longest substring without repeating characters.
4
+ *
5
+ * https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
6
+ *
7
+ */
8
+
9
+ // O(n^2) time
10
+ // O(n) space
11
+ function lengthOfLongestSubstring ( s : string ) : number {
12
+ let result = 0 ;
13
+
14
+ for ( let i = 0 ; i < s . length ; i ++ ) {
15
+ let set = new Set ( ) ;
16
+ let substring = 0 ;
17
+
18
+ for ( let j = i ; j < s . length ; j ++ ) {
19
+ if ( set . has ( s [ j ] ) ) {
20
+ break ;
21
+ }
22
+
23
+ set . add ( s [ j ] ) ;
24
+ substring ++ ;
25
+ }
26
+
27
+ result = Math . max ( result , substring ) ;
28
+ }
29
+
30
+ return result ;
31
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 206. Reverse Linked List
3
+ * Given the head of a singly linked list, reverse the list, and return the reversed list.
4
+ *
5
+ * https://leetcode.com/problems/reverse-linked-list/description/
6
+ *
7
+ */
8
+
9
+ /**
10
+ * Definition for singly-linked list.
11
+ * class ListNode {
12
+ * val: number
13
+ * next: ListNode | null
14
+ * constructor(val?: number, next?: ListNode | null) {
15
+ * this.val = (val===undefined ? 0 : val)
16
+ * this.next = (next===undefined ? null : next)
17
+ * }
18
+ * }
19
+ */
20
+
21
+ class ListNode {
22
+ val : number ;
23
+ next : ListNode | null ;
24
+ constructor ( val ?: number , next ?: ListNode | null ) {
25
+ this . val = val === undefined ? 0 : val ;
26
+ this . next = next === undefined ? null : next ;
27
+ }
28
+ }
29
+
30
+ // O(n) time
31
+ // O(1) space
32
+ function reverseList ( head : ListNode | null ) : ListNode | null {
33
+ let prev : ListNode | null = null ;
34
+ let next : ListNode | null = null ;
35
+
36
+ while ( head ) {
37
+ next = head . next ;
38
+ head . next = prev ;
39
+
40
+ prev = head ;
41
+ head = next ;
42
+ }
43
+
44
+ return prev ;
45
+ }
You can’t perform that action at this time.
0 commit comments