Add more solution for leetcode

This commit is contained in:
dang hai 2019-08-24 10:08:45 -07:00
parent a25c895792
commit 9a75c4e850
12 changed files with 203 additions and 19 deletions

View File

@ -1,19 +0,0 @@
// for strlen( )
#include<string.h>
int numJewelsInStones(char * j, char * s){
// as strlen is O(n), store it once rather than using it in for loop
int cnt[500],lens=strlen(s),lenj=strlen(j),sol=0;
memset(cnt,0,sizeof(cnt));
// lookup to know which character occurs in j
for(int i=0;i<lenj;i++)
cnt[j[i]]++;
// count the characters in s
for(int i=0;i<lens;i++)
sol+=cnt[s[i]];
return sol;
}

View File

@ -6,10 +6,19 @@ LeetCode
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy|
|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|
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [C](./src/108.c)|Easy|
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c)|Easy|
|136|[Single Number](https://leetcode.com/problems/single-number/) | [C](./src/136.c)|Easy|
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [C](./src/141.c)|Easy|
|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy|
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium|
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [C](./src/217.c)|Easy|
|268|[Missing Number](https://leetcode.com/problems/missing-number/) | [C](./src/268.c)|Easy|
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C](./src/287.c)|Medium|
|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|

14
leetcode/src/1.c Normal file
View File

@ -0,0 +1,14 @@
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int i, j;
int *ret = calloc(2, sizeof(int));
for (i = 0; i < numsSize; i++) {
int key = target - nums[i];
for (j = i + 1; j < numsSize; j++)
if (nums[j] == key) {
ret[0] = i;
ret[1] = j;
}
}
*returnSize = 2;
return ret;
}

29
leetcode/src/108.c Normal file
View File

@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* convertBST(int *nums, int left, int right) {
if (left > right)
return NULL;
else {
int mid = (right + left) / 2;
struct TreeNode *new_val = malloc(sizeof(struct TreeNode));
new_val->val = nums[mid];
new_val->left = convertBST(nums, left, mid - 1);
new_val->right = convertBST(nums, mid + 1, right);
return new_val;
}
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
if(numsSize == 0)
return NULL;
else
return convertBST(nums, 0, numsSize -1);
}

6
leetcode/src/136.c Normal file
View File

@ -0,0 +1,6 @@
int singleNumber(int* nums, int numsSize){
int i, result = 0;
for(i = 0; i < numsSize; i++)
result = result ^ nums[i];
return result;
}

16
leetcode/src/141.c Normal file
View File

@ -0,0 +1,16 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode *fast=head, *slow=head;
while( slow && fast && fast->next ){
fast=fast->next->next;
slow=slow->next;
if(fast==slow) return true;
}
return false;
}

16
leetcode/src/169.c Normal file
View File

@ -0,0 +1,16 @@
/* Boyer-Moore Majority Vote Algorithm
* http://www.cs.utexas.edu/~moore/best-ideas/mjrty/ */
int majorityElement(int* nums, int numsSize){
int count = 1;
int majorNum = nums[0];
for (int i = 1; i < numsSize; i++) {
if(count == 0) {
majorNum = nums[i];
count++;
} else if (majorNum == nums[i])
count++;
else
count--;
}
return majorNum;
}

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

@ -0,0 +1,29 @@
bool isValid(char * s){
int i, k = 0, len = strlen(s);
char *store = calloc(len, sizeof(char));
for( i = 0; s[i] != '\0'; i++) {
switch(s[i]) {
case '(':
case '{':
case '[':
store[k++] = s[i];
break;
case ')':
if(k < 1 || store[--k] != '(')
goto out;
break;
case '}':
if(k < 1 || store[--k] != '{')
goto out;
break;
case ']':
if(k < 1 || store[--k] != '[')
goto out;
break;
}
}
out:
free(store);
return s[i] == '\0' && k == 0;
}

8
leetcode/src/215.c Normal file
View File

@ -0,0 +1,8 @@
int *cmpval (const void *a, const void *b) {
return *(int *)b - *(int *)a;
}
int findKthLargest(int* nums, int numsSize, int k){
qsort(nums, numsSize, sizeof(int), cmpval);
return nums[k-1];
}

8
leetcode/src/268.c Normal file
View File

@ -0,0 +1,8 @@
int missingNumber(int* nums, int numsSize){
int i, actual_sum = 0, sum = 0;
for(i = 0; i < numsSize; i++) {
sum = sum + nums[i];
actual_sum = actual_sum + i;
}
return actual_sum + numsSize - sum;
}

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

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

55
leetcode/src/3.c Normal file
View File

@ -0,0 +1,55 @@
int lengthOfLongestSubstring(char* str) {
int n = strlen(str);
if(!n) return 0;
int L_len = 1; // lenght of longest substring
int C_len = 1; // lenght of current substring
int P_ind, i; // P_ind for previous index
int visited[256]; // visited will keep track of visiting char for the last instance.
// since there are 256 ASCII char, its size is limited to that value.
memset(visited, -1, sizeof(int) * 256);
visited[str[0]] = 0; // the index of that char will tell us that when it was visited.
for (i = 1; i < n; i++)
{
P_ind = visited[str[i]];
if (P_ind == -1 || i - C_len > P_ind)
C_len++; // if the current char was not visited earlier, or it is not the part of current substring
else
{ // otherwise, we need to change the current/longest substring length
if (C_len > L_len) L_len = C_len;
C_len = i - P_ind;
}
visited[str[i]] = i;
}
if (C_len > L_len) L_len = C_len;
return L_len;
}
/* Brute force */
int lengthOfLongestSubstring(char * s){
int cur_max = 0, max = 0;
int counter[255];
int end = 0;
memset(counter, 0, sizeof(int) *255);
while (end < strlen(s)) {
if (counter[s[end]] == 0) {
counter[s[end]]++;
end++;
cur_max++;
} else {
char c = s[end];
memset(counter, 0, 255 * sizeof(int));
if (cur_max >= max)
max = cur_max;
cur_max = 0;
while(s[end - 1] != c)
end--;
}
}
if (cur_max >= max)
max = cur_max;
return max;
}