Add more algorithm solutions for leetcode

This commit is contained in:
dang hai 2019-08-13 11:38:41 -07:00
parent 850c001a99
commit de63c24503
11 changed files with 169 additions and 1 deletions

View File

@ -6,6 +6,16 @@ LeetCode
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy|
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./src/35.c)|Easy|
|704|[Search Insert Position](https://leetcode.com/problems/binary-search/) | [C](./src/704.c)|Easy|
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c)|Easy|
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [C](./src/217.c)|Easy|
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [C](./src/344.c)|Easy|
|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C](./src/387.c)|Easy|
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C](./src/442.c)|Medium|
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy|
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|
|704|[Binary Search](https://leetcode.com/problems/binary-search/) | [C](./src/704.c)|Easy|
|709|[To Lower Case](https://leetcode.com/problems/to-lower-case/) | [C](./src/709.c)|Easy|
|905|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [C](./src/905.c)|Easy|
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | [C](./src/917.c)|Easy|

19
leetcode/src/125.c Normal file
View File

@ -0,0 +1,19 @@
bool isPalindrome(char * s){
int start = 0, end = strlen(s) - 1;
while(start < end) {
if (!isalpha(s[start]) && !isalnum(s[start])) {
start++;
}
else if (!isalpha(s[end]) && !isalnum(s[end])) {
end--;
} else {
char c1 = tolower(s[start]);
char c2 = tolower(s[end]);
if(c1 != c2)
return 0;
start++;
end--;
}
}
return 1;
}

13
leetcode/src/217.c Normal file
View File

@ -0,0 +1,13 @@
int numcmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
bool containsDuplicate(int* nums, int numsSize){
int i;
qsort(nums, numsSize, sizeof(int), numcmp);
for (i = 0; i < numsSize - 1; i++) {
if(nums[i] == nums[i+1])
return 1;
}
return 0;
}

10
leetcode/src/26.c Normal file
View File

@ -0,0 +1,10 @@
int removeDuplicates(int* nums, int numsSize){
int count = 0, i;
for (i = 1; i < numsSize; i++) {
if (nums[i] == nums[i-1])
count++;
else
nums[i-count] = nums[i];
}
return numsSize - count;
}

10
leetcode/src/344.c Normal file
View File

@ -0,0 +1,10 @@
void reverseString(char* s, int sSize){
int last = sSize - 1, i;
for (i = 0; i < last; i++) {
char tmp = s[i];
s[i] = s[last];
s[last] = tmp;
last--;
}
}

11
leetcode/src/387.c Normal file
View File

@ -0,0 +1,11 @@
int firstUniqChar(char * s){
int *arr = calloc(256, sizeof(int));
int i;
for(i = 0; i < strlen(s); i++)
arr[s[i]] = arr[s[i]] + 1;
for(i = 0; i < strlen(s); i++) {
if(arr[s[i]] == 1)
return i;
}
return -1;
}

21
leetcode/src/442.c Normal file
View File

@ -0,0 +1,21 @@
int cmpval (const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int* findDuplicates(int* nums, int numsSize, int* returnSize){
int i;
qsort(nums, numsSize, sizeof(int), cmpval);
int *retArr = malloc(numsSize * sizeof(int));
*returnSize = 0;
for (i = 0; i < numsSize - 1;) {
if (nums[i] == nums[i + 1]) {\
retArr[*returnSize] = nums[i];
*returnSize = *returnSize + 1;
i = i + 2;
} else {
i = i + 1;
}
}
return retArr;
}

39
leetcode/src/520.c Normal file
View File

@ -0,0 +1,39 @@
bool detectCapitalUse(char * word){
int len = strlen(word);
if(len == 1)
return 1;
int countUpper = 0, i;
for(i = 0; i < len; i++) {
if(isupper(word[i]))
countUpper++;
}
/* All lower case */
if (countUpper == 0)
return 1;
/* 1st character is upper, and the rest is lower case */
if (countUpper == 1 && isupper(word[0]))
return 1;
/* Check all character is upper case? */
else
return countUpper == len;
}
/* Another way */
bool isAllUpper(char *word) {
int len = strlen(word);
for(int i = 0; i < len; i++) {
if(islower(word[i]))
return 0;
}
return 1;
}
bool detectCapitalUse(char * word){
int len = strlen(word);
for(int i = 1; i < len; i++) {
if(isupper(word[i]) && !isAllUpper(word))
return 0;
}
return 1;
}

10
leetcode/src/561.c Normal file
View File

@ -0,0 +1,10 @@
int cmpval (const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
int arrayPairSum(int* nums, int numsSize){
int sum = 0, i;
qsort(nums, numsSize, sizeof(int), cmpval);
for(i = 0; i < numsSize; i = i + 2)
sum = sum + nums[i];
return sum;
}

5
leetcode/src/709.c Normal file
View File

@ -0,0 +1,5 @@
char * toLowerCase(char * str){
for (int i = 0; i< strlen(str); i++)
str[i] = tolower(str[i]);
return str;
}

20
leetcode/src/917.c Normal file
View File

@ -0,0 +1,20 @@
char * reverseOnlyLetters(char * S){
int last = strlen(S) - 1, i;
for(i = 0; i < last;) {
if(!isalpha(S[i])) {
i++;
continue;
}
if(!isalpha(S[last])) {
last--;
continue;
}
char tmp = S[i];
S[i] = S[last];
S[last] = tmp;
i++;
last--;
}
return S;
}