Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/fkdl0048/Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdl0048 committed Oct 8, 2024
2 parents b77ef1f + 01aa542 commit 74cb042
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LeetCode/Combination Sum II.cpp
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();
}
}
};
27 changes: 27 additions & 0 deletions LeetCode/Combination Sum.cpp
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();
}
}
};
28 changes: 28 additions & 0 deletions LeetCode/Count and Say.cpp
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;
}
};
23 changes: 23 additions & 0 deletions LeetCode/First Missing Positive.cpp
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;
}
};
41 changes: 41 additions & 0 deletions LeetCode/Sudoku Solver.cpp
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;
}
};

0 comments on commit 74cb042

Please sign in to comment.