-
-
Notifications
You must be signed in to change notification settings - Fork 195
[moonhyeok] Week7 #949
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
Merged
Merged
[moonhyeok] Week7 #949
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
longest-substring-without-repeating-characters/mike2ox.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* https://leetcode.com/problems/longest-substring-without-repeating-characters/ | ||
* 풀이방법: Sliding Window | ||
* | ||
* 공간 복잡도: O(1) | ||
* 시간 복잡도: O(n) | ||
*/ | ||
|
||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
int lengthOfLongestSubstring(string s) { | ||
int answer = 0; | ||
vector<int> visit(256, -1); | ||
int i, j; | ||
|
||
for (i = 0, j = 0; i < s.size(); i++){ | ||
if (visit[s[i]] != -1 && visit[s[i]] >= j && visit[s[i]] < i) { | ||
j = visit[s[i]] + 1; | ||
} | ||
answer = max(answer, i - j + 1); | ||
visit[s[i]] = i; | ||
} | ||
return answer; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* https://leetcode.com/problems/number-of-islands/ | ||
* 풀이방법: BFS를 사용하여 섬의 개수를 구함 | ||
* | ||
* 시간복잡도: O(n * m) (n: 그리드의 행, m: 그리드의 열) | ||
* 공간복잡도: O(n * m) (그리드의 모든 요소를 방문) | ||
*/ | ||
|
||
function numIslands(grid: string[][]): number { | ||
if (!grid.length) return 0; // 그리드가 비어있는 경우 | ||
|
||
const rows = grid.length; | ||
const cols = grid[0].length; | ||
let islands = 0; | ||
|
||
const bfs = (startRow: number, startCol: number) => { | ||
const q: number[][] = [[startRow, startCol]]; | ||
grid[startRow][startCol] = "0"; | ||
|
||
const directions = [ | ||
[1, 0], | ||
[-1, 0], | ||
[0, 1], | ||
[0, -1], | ||
]; // 상하좌우 | ||
|
||
while (q.length) { | ||
const [r, c] = q.shift()!; | ||
|
||
for (const [dx, dy] of directions) { | ||
const newR = r + dx; | ||
const newC = c + dy; | ||
|
||
if ( | ||
newR >= 0 && | ||
newR < rows && | ||
newC >= 0 && | ||
newC < cols && | ||
grid[newR][newC] === "1" | ||
) { | ||
q.push([newR, newC]); | ||
grid[newR][newC] = "0"; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
for (let r = 0; r < rows; r++) { | ||
for (let c = 0; c < cols; c++) { | ||
if (grid[r][c] === "1") { | ||
islands++; | ||
bfs(r, c); | ||
} | ||
} | ||
} | ||
|
||
return islands; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* https://leetcode.com/problems/reverse-linked-list/ | ||
* 풀이방법: 스택을 사용하여 역순으로 노드를 생성 | ||
* | ||
* 시간복잡도: O(n) (n: 리스트의 길이) | ||
* 공간복잡도: O(n) (스택에 모든 노드를 저장) | ||
*/ | ||
|
||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
|
||
function reverseList(head: ListNode | null): ListNode | null { | ||
if (!head) return null; | ||
|
||
const stack: number[] = []; | ||
let result: ListNode | null = null; | ||
let lastNode: ListNode; | ||
while (head) { | ||
stack.push(head.val); | ||
head = head.next; | ||
} | ||
for (let i = stack.length - 1; i >= 0; i--) { | ||
if (!result) { | ||
result = new ListNode(stack[i]); | ||
lastNode = result; | ||
continue; | ||
} | ||
lastNode.next = new ListNode(stack[i]); | ||
lastNode = lastNode.next; | ||
} | ||
return result; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.