-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/fkdl0048/Algorithm
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { | ||
vector<vector<int>> result; | ||
vector<int> currentCombination; | ||
|
||
sort(candidates.begin(), candidates.end()); | ||
backTrack(candidates, target, 0, currentCombination, result); | ||
return result; | ||
} | ||
|
||
private: | ||
void backTrack(vector<int>& candidates, int target, int index, vector<int>& currentCombination, vector<vector<int>>& result) { | ||
if (target == 0) { | ||
result.push_back(currentCombination); | ||
return; | ||
} | ||
|
||
if (target < 0) return; | ||
|
||
for (int i = index; i < candidates.size(); i++) { | ||
|
||
if (i > index && candidates[i] == candidates[i - 1]) continue; | ||
|
||
currentCombination.push_back(candidates[i]); | ||
backTrack(candidates, target - candidates[i], i + 1, currentCombination, result); | ||
currentCombination.pop_back(); | ||
} | ||
} | ||
}; |
This file contains 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 @@ | ||
class Solution { | ||
public: | ||
vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
vector<vector<int>> result; | ||
vector<int> currentCombiation; | ||
backTrack(candidates, 0, target, currentCombiation, result); | ||
return result; | ||
} | ||
|
||
private: | ||
void backTrack(vector<int>& candidates, int startIdx, int target, vector<int>& currentCombiation, vector<vector<int>>& result){ | ||
if (target == 0) { | ||
result.push_back(currentCombiation); | ||
return; | ||
} | ||
|
||
if (target < 0) { | ||
return; | ||
} | ||
|
||
for (int i = startIdx; i < candidates.size(); i++) { | ||
currentCombiation.push_back(candidates[i]); | ||
backTrack(candidates, i, target - candidates[i], currentCombiation, result); | ||
currentCombiation.pop_back(); | ||
} | ||
} | ||
}; |
This file contains 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,28 @@ | ||
class Solution { | ||
public: | ||
string countAndSay(int n) { | ||
return countAndSayRecursion(n); | ||
} | ||
|
||
private: | ||
string countAndSayRecursion(int n){ | ||
if (n == 1) return "1"; | ||
|
||
string prev = countAndSayRecursion(n - 1); | ||
string result = ""; | ||
int count = 1; | ||
|
||
for (int i = 1; i < prev.size(); i++) { | ||
if (prev[i] == prev[i - 1]) { | ||
count++; | ||
} | ||
else { | ||
result += to_string(count) + prev[i - 1]; | ||
count = 1; | ||
} | ||
} | ||
result += to_string(count) + prev.back(); | ||
|
||
return result; | ||
} | ||
}; |
This file contains 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,23 @@ | ||
class Solution { | ||
public: | ||
int firstMissingPositive(vector<int>& nums) { | ||
int n = nums.size(); | ||
|
||
// 배열을 정렬하는 대신, 각 숫자를 그 숫자가 있어야 할 위치로 이동 | ||
for (int i = 0; i < n; ++i) { | ||
while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { | ||
swap(nums[i], nums[nums[i] - 1]); | ||
} | ||
} | ||
|
||
// 첫 번째로 잘못된 위치의 숫자 찾기 | ||
for (int i = 0; i < n; ++i) { | ||
if (nums[i] != i + 1) { | ||
return i + 1; | ||
} | ||
} | ||
|
||
// 모든 숫자가 제자리에 있을 경우, n + 1이 첫 번째 누락된 양수 | ||
return n + 1; | ||
} | ||
}; |
This file contains 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 @@ | ||
class Solution { | ||
public: | ||
void solveSudoku(vector<vector<char>>& board) { | ||
solve(board); | ||
} | ||
private: | ||
bool solve(vector<vector<char>>& board) { | ||
for (int i = 0; i < 9; i++) { | ||
for (int j = 0; j < 9; j++) { | ||
if (board[i][j] == '.') { | ||
for (char k = '1'; k <= '9'; k++) { | ||
if (isVaild(board, i, j, k)) { | ||
board[i][j] = k; | ||
|
||
if (solve(board)) { | ||
return true; | ||
} | ||
else { | ||
board[i][j] = '.'; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool isVaild(vector<vector<char>>& board, int i, int j, char k) { | ||
for (int idx = 0; idx < 9; idx++){ | ||
if (board[idx][j] == k) return false; | ||
if (board[i][idx] == k) return false; | ||
if (board[(i / 3) * 3 + idx / 3][(j / 3) * 3 + idx % 3] == k) return false; | ||
} | ||
|
||
return true; | ||
} | ||
}; |