forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
6d998e7
commit c78db79
Showing
2 changed files
with
38 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,2 @@ | ||
all: | ||
gcc -O2 -o test rm_dup.c |
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,36 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int removeDuplicates(int* nums, int numsSize) | ||
{ | ||
if (numsSize <= 1) { | ||
return numsSize; | ||
} | ||
|
||
int i = 0, j, count = 1; | ||
while (i < numsSize) { | ||
for (j = i + 1; j < numsSize && nums[i] == nums[j]; j++) {} | ||
if (j < numsSize) { | ||
nums[count++] = nums[j]; | ||
} | ||
i = j; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i, size = argc - 1; | ||
int *nums = malloc(size * sizeof(int)); | ||
for (i = 0; i < argc - 1; i++) { | ||
nums[i] = atoi(argv[i + 1]); | ||
} | ||
|
||
int count = removeDuplicates(nums, size); | ||
for (i = 0; i < count; i++) { | ||
printf("%d ", nums[i]); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |