Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wan] Week 7 #947

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions longest-substring-without-repeating-characters/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 3. Longest Substring Without Repeating Characters
* Given a string s, find the length of the longest substring without repeating characters.
*
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
*
*/

// O(n^2) time
// O(n) space
function lengthOfLongestSubstring(s: string): number {
let result = 0;

for (let i = 0; i < s.length; i++) {
let set = new Set();
let substring = 0;

for (let j = i; j < s.length; j++) {
if (set.has(s[j])) {
break;
}

set.add(s[j]);
substring++;
}

result = Math.max(result, substring);
}

return result;
}
45 changes: 45 additions & 0 deletions reverse-linked-list/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 206. Reverse Linked List
* Given the head of a singly linked list, reverse the list, and return the reversed list.
*
* https://leetcode.com/problems/reverse-linked-list/description/
*
*/

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}

// O(n) time
// O(1) space
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;
let next: ListNode | null = null;

while (head) {
next = head.next;
head.next = prev;

prev = head;
head = next;
}

return prev;
}
Loading