Skip to content

Commit 78d642f

Browse files
author
Victor
committed
initial publish
leetcode solutions in c.
0 parents  commit 78d642f

File tree

313 files changed

+26836
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

313 files changed

+26836
-0
lines changed

1. Two Sum.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
1. Two Sum
3+
4+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
5+
6+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
8+
9+
Example:
10+
Given nums = [2, 7, 11, 15], target = 9,
11+
12+
Because nums[0] + nums[1] = 2 + 7 = 9,
13+
return [0, 1].
14+
*/
15+
16+
/**
17+
* Note: The returned array must be malloced, assume caller calls free().
18+
*/
19+
typedef struct data_s {
20+
   int val;
21+
   int idx;
22+
} data_t;
23+
int cmp(const void *a, const void *b) {
24+
   return ((data_t *)a)->val - ((data_t *)b)->val;
25+
}
26+
int* twoSum(int* nums, int numsSize, int target) {
27+
   int *indices;
28+
   int i, j, k;
29+
   
30+
#if 0
31+
   for (i = 0; i < numsSize - 1; i ++) {
32+
       for (j = i + 1; j < numsSize; j ++) {
33+
           if (nums[i] + nums[j] == target) {
34+
               indices = malloc(2 * sizeof(int));
35+
               //assert(indices);
36+
               indices[0] = i;
37+
               indices[1] = j;
38+
               return indices;
39+
          }
40+
      }
41+
  }
42+
#else
43+
   data_t *array;
44+
   array = malloc((numsSize + 1) * sizeof(data_t));
45+
   //assert(array);
46+
   for (i = 0; i < numsSize; i ++) {
47+
       array[i].val = nums[i];
48+
       array[i].idx = i;
49+
  }
50+
   qsort(array, numsSize, sizeof(data_t), cmp);
51+
   i = 0;
52+
   j = numsSize - 1;
53+
   while (i < j) {
54+
       k = array[i].val + array[j].val;
55+
       if (k == target) {
56+
           indices = malloc(2 * sizeof(int));
57+
           //assert(indices);
58+
           indices[0] = array[i].idx;
59+
           indices[1] = array[j].idx;
60+
           free(array);
61+
           return indices;
62+
      } else if (k < target) {
63+
           i ++;
64+
      } else {
65+
           j --;
66+
      }
67+
  }
68+
   free(array);
69+
#endif
70+
   
71+
   return NULL;
72+
}
73+
74+
75+
/*
76+
Difficulty:Easy
77+
Total Accepted:586.7K
78+
Total Submissions:1.7M
79+
80+
81+
Companies LinkedIn Uber Airbnb Facebook Amazon Microsoft Apple Yahoo Dropbox Bloomberg Yelp Adobe
82+
Related Topics Array Hash Table
83+
Similar Questions
84+
85+
86+
3Sum
87+
88+
4Sum
89+
90+
Two Sum II - Input array is sorted
91+
92+
Two Sum III - Data structure design
93+
94+
Subarray Sum Equals K
95+
96+
Two Sum IV - Input is a BST
97+
*/

10. Regular Expression Matching.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
10. Regular Expression Matching
3+
4+
Implement regular expression matching with support for '.' and '*'.
5+
6+
'.' Matches any single character.
7+
'*' Matches zero or more of the preceding element.
8+
9+
The matching should cover the entire input string (not partial).
10+
11+
The function prototype should be:
12+
bool isMatch(const char *s, const char *p)
13+
14+
Some examples:
15+
isMatch("aa","a") → false
16+
isMatch("aa","aa") → true
17+
isMatch("aaa","aa") → false
18+
isMatch("aa", "a*") → true
19+
isMatch("aa", ".*") → true
20+
isMatch("ab", ".*") → true
21+
isMatch("aab", "c*a*b") → true
22+
*/
23+
24+
#define NOT_MATCH(A, B) (((B) == '.' && (A) == 0) || ((B) != '.' && (A) != (B)))
25+
#define MATCH(A, B)     ((A) == (B) || (B) == '.')
26+
#define IDX(I, J)       (((I) + 1) * (plen + 1) + (J) + 1)
27+
28+
bool match_recursive(char *s, char *p, int *retry) {
29+
   if (*p == 0) return *s == 0;
30+
   if (*(p + 1) == '*') {
31+
       while (*retry) {
32+
           if (match_recursive(s, p + 2, retry)) {
33+
               return true;
34+
          }
35+
           if (NOT_MATCH(*s, *p)) {
36+
               return false;
37+
          }
38+
           s ++;
39+
      }
40+
       *retry = 0;
41+
       return false;
42+
  }
43+
   if (NOT_MATCH(*s, *p)) {
44+
       return false;
45+
  }
46+
   return match_recursive(s + 1, p + 1, retry);
47+
}
48+
bool isMatch(char* s, char* p) {
49+
#if 0  // 22ms
50+
   int retry = 1;
51+
   return match_recursive(s, p, &retry);
52+
#else  // 9ms
53+
   int *dp;
54+
   int i, j;
55+
   int slen, plen;
56+
   
57+
   slen = strlen(s);
58+
   plen = strlen(p);
59+
   
60+
   dp = calloc((slen + 1) * (plen + 1), sizeof(int));
61+
   //assert(dp);
62+
63+
   dp[0] = 1;
64+
   for (j = 0; j < plen; j ++) {
65+
       if (p[j] == '*') {
66+
           dp[IDX(-1, j)] = dp[IDX(-1, j - 2)];
67+
      }
68+
  }
69+
   for (i = 0; i < slen; i++) {
70+
       for (j = 0; j < plen; j++) {
71+
           if (p[j] != '*') {
72+
               dp[IDX(i, j)] = dp[IDX(i - 1, j - 1)] && MATCH(s[i], p[j]);
73+
          } else {
74+
               dp[IDX(i, j)] = dp[IDX(i, j - 2)] ||                                 // no s
75+
                               dp[IDX(i, j - 1)] ||                                 // one s
76+
                              (MATCH(s[i], p[j - 1]) && dp[IDX(i - 1, j)]);        // more s
77+
          }
78+
      }
79+
  }
80+
   
81+
   i = dp[IDX(slen - 1, plen - 1)];
82+
   
83+
   free(dp);
84+
   
85+
   return i;
86+
#endif
87+
}
88+
89+
90+
/*
91+
Difficulty:Hard
92+
Total Accepted:147.1K
93+
Total Submissions:610.5K
94+
95+
96+
Companies Google Uber Airbnb Facebook Twitter
97+
Related Topics Dynamic Programming Backtracking String
98+
Similar Questions
99+
100+
101+
Wildcard Matching
102+
*/

101. Symmetric Tree.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
101. Symmetric Tree
3+
4+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
5+
6+
7+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
8+
1
9+
/ \
10+
2 2
11+
/ \ / \
12+
3 4 4 3
13+
14+
15+
16+
But the following [1,2,2,null,3,null,3] is not:
17+
1
18+
/ \
19+
2 2
20+
\ \
21+
3 3
22+
23+
24+
25+
26+
Note:
27+
Bonus points if you could solve it both recursively and iteratively.
28+
*/
29+
30+
/**
31+
* Definition for a binary tree node.
32+
* struct TreeNode {
33+
*     int val;
34+
*     struct TreeNode *left;
35+
*     struct TreeNode *right;
36+
* };
37+
*/
38+
#define PUSH(N) do { p[n ++] = N; } while (0)
39+
40+
bool isSymmetric(struct TreeNode* root) {
41+
   struct TreeNode **p, **tmp;
42+
   int dep, n, i, j, k;
43+
   
44+
   if (!root) return true;
45+
   
46+
   dep = 0; n = 0;
47+
   p = malloc((1 << dep) * sizeof(struct TreeNode *));
48+
   //assert(p);
49+
   PUSH(root);
50+
   while (n) {
51+
       for (i = 0, j = n - 1; i < j; i ++, j --) {
52+
           if ((!p[i] && p[j]) ||
53+
              (!p[j] && p[i]) ||
54+
              ( p[i] && p[i]->val != p[j]->val)) {
55+
               free(p);
56+
               return false;
57+
          }
58+
      }
59+
       tmp = p; k = n;
60+
       dep ++; n = 0;
61+
       p = malloc((1 << dep) * sizeof(struct TreeNode *));
62+
       //assert(p);
63+
       for (i = 0; i < k; i ++) {
64+
           if (tmp[i]) {
65+
               PUSH(tmp[i]->left);
66+
               PUSH(tmp[i]->right);
67+
          }
68+
      }
69+
       free(tmp);
70+
  }
71+
   free(p);
72+
   return true;
73+
}
74+
75+
76+
/*
77+
Difficulty:Easy
78+
Total Accepted:187.9K
79+
Total Submissions:484.8K
80+
81+
82+
Companies LinkedIn Bloomberg Microsoft
83+
Related Topics Tree Depth-first Search Breadth-first Search
84+
85+
*/

0 commit comments

Comments
 (0)