Skip to content

[yeoju] week 1 #680

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

Closed
wants to merge 2 commits into from
Closed
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
60 changes: 60 additions & 0 deletions contains-duplicate/aa601.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdbool.h>

void merge(int *nums, int left, int mid, int right)
{
int i;
int j;
int k;
int leftArr[mid - left + 1];
int rightArr[right - mid];

i = -1;
while (++i < mid - left + 1) // 왼쪽 분할된 부분 넣기
leftArr[i] = nums[left + i];
i = -1;
while (++i < right - mid) // 오른쪽 분할된 부분 넣기
rightArr[i] = nums[mid + i + 1];
i = 0;
j = 0;
k = left; // **** nums배열인덱스 => left부터 시작
// 나누어진 배열끼리 비교해서 nums배열 재배치
while ((i < mid - left + 1) && (j < right - mid)) {
if (leftArr[i] <= rightArr[j])
nums[k] = leftArr[i++];
else
nums[k] = rightArr[j++];
++k;
}
while (i < mid - left + 1) // left배열 남아있으면 마저 삽입
nums[k++] = leftArr[i++];
while (j < right - mid) // right배열 남아있으면 마저 삽입
nums[k++] = rightArr[j++];
}

void mergeSort(int *nums, int left, int right) {
int mid;

if (left < right)
{
mid = (left + right) / 2;
mergeSort(nums, left, mid); // 왼쪽분할
mergeSort(nums, mid + 1, right); // 오른쪽분할
merge(nums, left, mid, right);
}
}

bool containsDuplicate(int* nums, int numsSize) {
int i;

mergeSort(nums, 0, numsSize - 1);
i = -1;
while (++i + 1 < numsSize) {
if (nums[i] == nums[i + 1])
return (true);
}
return (false);
}



Loading