-
-
Notifications
You must be signed in to change notification settings - Fork 245
[crumbs22] Week08 Solutions #1510
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
Changes from all commits
Commits
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
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,60 @@ | ||
#include <vector> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class Node { | ||
public: | ||
int val; | ||
vector<Node*> neighbors; | ||
Node() { | ||
val = 0; | ||
neighbors = vector<Node*>(); | ||
} | ||
Node(int _val) { | ||
val = _val; | ||
neighbors = vector<Node*>(); | ||
} | ||
Node(int _val, vector<Node*> _neighbors) { | ||
val = _val; | ||
neighbors = _neighbors; | ||
} | ||
}; | ||
/* | ||
std::vector<T> v; | ||
|
||
v.emplace_back(arg1, arg2, ...); // 생성자 인자를 바로 전달해 컨테이너 안에서 직접 T 객체를 생성 | ||
*/ | ||
|
||
#include <unordered_map> | ||
#include <queue> | ||
|
||
class Solution { | ||
public: | ||
Node* cloneGraph(Node* node) { | ||
if (!node) | ||
return (nullptr); | ||
unordered_map<Node*, Node*> m; | ||
queue<Node*> q; | ||
|
||
m[node] = new Node(node->val); // 시작 노드를 복제하고 맵과 큐에 등록 | ||
q.push(node); | ||
|
||
// BFS | ||
while (!q.empty()) { | ||
Node* cur = q.front(); | ||
q.pop(); | ||
|
||
for (Node* nei : cur->neighbors) { | ||
|
||
// 아직 복제하지 않은 노드일 때 | ||
if (!m.count(nei)) { | ||
m[nei] = new Node(nei->val); | ||
q.push(nei); | ||
} | ||
m[cur]->neighbors.push_back(m[nei]); // 현재 복제본에 이 이웃의 복제본을 연결 | ||
} | ||
} | ||
return m[node]; | ||
} | ||
}; |
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,34 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
/* | ||
시간 복잡도 : O(n) | ||
공간 복잡도 : O(1) | ||
|
||
구간 길이 windowSize = (right - left + 1)에서, | ||
windowSize - maxCount <= k 이면 이 구간을 모두 같은 문자로 만들 수 있다 | ||
windowSize - maxCount > k 이면 교체가능한 횟수가 초과되었으므로 left를 한 칸 밀어서 구간을 줄인다 | ||
*/ | ||
class Solution { | ||
public: | ||
int characterReplacement(string s, int k) { | ||
int left = 0; | ||
int maxCount = 0; | ||
int answer = 0; | ||
int count[26] = {0}; | ||
|
||
for (int right = 0; right < s.size(); right++) { | ||
|
||
count[s[right] - 'A']++; | ||
maxCount = max(maxCount, count[s[right] - 'A']); // 윈도우 안에서 가장 많은 빈도를 갱신 | ||
while (right - left + 1 - maxCount > k) { | ||
count[s[left] - 'A']--; | ||
left++; | ||
} | ||
answer = max(answer, right - left + 1); | ||
|
||
} | ||
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,30 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
/* | ||
중심 확장 방식 | ||
전체 문자열길이 n에 대해서 | ||
- 홀수 길이용 n개 | ||
- 짝수 길이용 n - 1개 | ||
전체 2n - 1개의 중심에서 각 양쪽으로 확장하며 카운트 | ||
*/ | ||
class Solution { | ||
public: | ||
int countSubstrings(string s) { | ||
int answer = 0; | ||
int n = s.size(); | ||
|
||
for (int center = 0; center < 2 * n - 1; center++) { | ||
int left = center / 2; | ||
int right = left + (center % 2); | ||
|
||
while (left >= 0 && right < n && s[left] == s[right]) { | ||
answer++; | ||
left--; | ||
right++; | ||
} | ||
} | ||
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,14 @@ | ||
#include <iostream> | ||
|
||
class Solution { | ||
public: | ||
uint32_t reverseBits(uint32_t n) { | ||
unsigned int reverse = 0; | ||
for (int i = 0; i < 32; i++) { | ||
reverse <<= 1; // 한 비트를 왼쪽으로 당겨서 공간을 만들고 | ||
reverse |= (n & 1); // n의 최하위 비트를 reverse의 최하위 비트에 저장 | ||
n >>= 1; // n의 다음 비트를 최하위 비트로 당겨온다 | ||
} | ||
return (reverse); | ||
} | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
짝수 홀수 문자열 한번에 확인하는 방법이 신기하네요!