Skip to content

Commit cc0ab4a

Browse files
add some codes
1 parent e52cc3d commit cc0ab4a

File tree

8 files changed

+340
-4
lines changed

8 files changed

+340
-4
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Implement regular expression matching with support for '.' and '*'.
3+
4+
* '.' Matches any single character.
5+
* '*' Matches zero or more of the preceding element.
6+
7+
* The matching should cover the entire input string (not partial).
8+
9+
* The function prototype should be:
10+
* bool isMatch(const char *s, const char *p)
11+
12+
* Some examples:
13+
* isMatch("aa","a") ? false
14+
* isMatch("aa","aa") ? true
15+
* isMatch("aaa","aa") ? false
16+
* isMatch("aa", "a*") ? true
17+
* isMatch("aa", ".*") ? true
18+
* isMatch("ab", ".*") ? true
19+
* isMatch("aab", "c*a*b") ? true
20+
* Created by supercoderx on 2017/8/11.
21+
*/
22+
23+
#include "../main.h"
24+
#include
25+
26+
bool isMatch(char* s, char* p) {
27+
if(p == NULL || *p){
28+
return *s;
29+
}
30+
31+
}

C/1-50/46-Permutations.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Given a collection of distinct numbers, return all possible permutations.
3+
4+
* For example,
5+
* [1,2,3] have the following permutations:
6+
* [
7+
* [1,2,3],
8+
* [1,3,2],
9+
* [2,1,3],
10+
* [2,3,1],
11+
* [3,1,2],
12+
* [3,2,1]
13+
* ]
14+
* Created by supercoderx on 2017/8/14.
15+
*/
16+
#include
17+
#include
18+
int** permute(int* nums, int numsSize, int* returnSize) {
19+
*returnSize = 0;
20+
if(numsSize == 0)
21+
return NULL;
22+
*returnSize = 1;
23+
24+
for(int i = 1; i <=numsSize;i++)
25+
*returnSize *= i;
26+
27+
int ** result = malloc(sizeof(int*)**returnSize);
28+
for(int i = 0;i < *returnSize;i++)
29+
result[i] = malloc(sizeof(int)*numsSize);
30+
int start = 0,index = start,cur=*returnSize;
31+
for(int i = 0; i <numsSize;i++){
32+
index = start-1;
33+
cur/=numsSize;
34+
for(int j =0; j<*returnSize;j++){
35+
result[j][i] = nums[++index%numsSize];
36+
}
37+
start++;
38+
}
39+
40+
return result;
41+
}
42+
43+
void testPermute(){
44+
int a[] = {1,2,3},count = 0;
45+
int** result = permute(a,3,&count);
46+
for(int i = 0; i <count;i++){
47+
for(int j =0 ;j<3;j++){
48+
printf("%d,",result[i][j]);
49+
}
50+
printf("\n");
51+
}
52+
}

C/1-50/49-Group Anagrams.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Given an array of strings, group anagrams together.
3+
4+
* For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
5+
* Return:
6+
*
7+
* [
8+
* ["ate", "eat","tea"],
9+
* ["nat","tan"],
10+
* ["bat"]
11+
* ]
12+
* Created by supercoderx on 2017/8/15.
13+
*/
14+
15+
#include
16+
#include
17+
#include
18+
char*** groupAnagrams(char** strs, int strsSize, int** columnSizes, int* returnSize) {
19+
*returnSize = 0;
20+
if(strsSize == 0)
21+
return NULL;
22+
int strSize[strsSize],len=0,**hash = NULL,*hashLen =NULL,*rowLen=NULL,*word,id,*col;
23+
char *s,***result=NULL,**row=NULL,*w;
24+
columnSizes = NULL;
25+
26+
for (int i = 0; i <strsSize ; ++i) {
27+
strSize[i] = 0;
28+
s = strs[i];
29+
while(*s!='\0'){
30+
strSize[i]++;
31+
s++;
32+
}
33+
}
34+
for (int j = 0; j < strsSize; ++j) {
35+
if(hash == NULL){
36+
word = malloc(sizeof(int)*26);
37+
for (int i = 0; i < 26; ++i) {
38+
word[i] = 0;
39+
}
40+
for (int k = 0; k < strSize[j]; ++k) {
41+
word[strs[j][k]-'a']++;
42+
}
43+
hash = malloc(sizeof(int*));
44+
hash[len] = word;
45+
hashLen = malloc(sizeof(int));
46+
hashLen[len++] = strSize[j];
47+
row = malloc(sizeof(char*));
48+
row[0] = strs[j];
49+
result = malloc(sizeof(char**));
50+
result[0] = row;
51+
columnSizes = malloc(sizeof(int*));
52+
col= malloc(sizeof(int));
53+
col[0] = strSize[j];
54+
columnSizes[0] = col;
55+
rowLen = malloc(sizeof(int));
56+
rowLen[0] = 1;
57+
} else{
58+
int i;
59+
for (i = 0; i <len ; ++i) {
60+
if(strSize[j]!=hashLen[i])
61+
continue;
62+
word = malloc(sizeof(int)*26);
63+
for (int p = 0; p < 26; ++p) {
64+
word[p] = 0;
65+
}
66+
for (int k = 0; k < strSize[j]; ++k) {
67+
word[strs[j][k]-'a']++;
68+
}
69+
int l;
70+
for (l = 0; l <26 ; ++l) {
71+
if(word[l]!=hash[i][l])
72+
break;
73+
}
74+
free(word);
75+
if(l != 26 )
76+
continue;
77+
id = 0;
78+
for (int m = 0; m < rowLen[i]-1; ++m) {
79+
if(strcmp(strs[j],result[i][m])>=0 &&strcmp(strs[j],result[i][m])<=0){
80+
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
}

C/51-100/53-Maximum-Subarray.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
3+
4+
* For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
5+
* the contiguous subarray [4,-1,2,1] has the largest sum = 6.
6+
* Created by supercoderx on 2017/8/15.
7+
*/
8+
#include
9+
#include
10+
int maxSubArray(int* nums, int numsSize) {
11+
if(numsSize == 1)
12+
return nums[0];
13+
int sum=0,maxSum = 0;
14+
for (int i = 0; i < numsSize; ++i) {
15+
sum+=nums[i];
16+
if(sum>maxSum)
17+
maxSum = sum;
18+
else if(sum<0)
19+
sum = 0;
20+
}
21+
return maxSum;
22+
}
23+
24+
void testMaxSubArray(){
25+
// int a[] = {-2,1,-3,4,-1,2,1,-5,4};
26+
int a[] = {-2,-1};
27+
printf("%d",maxSubArray(a,sizeof(a)/sizeof(int)));
28+
}

C/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(C)
33

44
set(CMAKE_C_STANDARD 99)
55

6-
set(SOURCE_FILES main.c 301-350/328-Odd-Even-Linked-List.c 201-250/233-Number-of-Digit-One.c 1-50/1-Two-Sum.c 1-50/2-Add-Two_Numbers.c 1-50/3-Longest-Substring-Without-Repeating-Characters.c 1-50/5.-Longest-Palindromic-Substring.c 1-50/7-Reverse-Integer.c 1-50/8-String-to-Integer-atoi.c 1-50/6-ZigZag-Conversion.c 1-50/9-Palindrome-Number.c headers/problem_301_350.h headers/problem_201_250.h 151-200/172.-Factorial-Trailing-Zeroes.c "201-250/224-Basic-Calculator .c" headers/problem_151_200.h 1-50/11.-Container-With-Most-Water.c)
6+
set(SOURCE_FILES main.c 301-350/328-Odd-Even-Linked-List.c 201-250/233-Number-of-Digit-One.c 1-50/1-Two-Sum.c 1-50/2-Add-Two_Numbers.c 1-50/3-Longest-Substring-Without-Repeating-Characters.c 1-50/5.-Longest-Palindromic-Substring.c 1-50/7-Reverse-Integer.c 1-50/8-String-to-Integer-atoi.c 1-50/6-ZigZag-Conversion.c 1-50/9-Palindrome-Number.c headers/problem_301_350.h headers/problem_201_250.h 151-200/172.-Factorial-Trailing-Zeroes.c "201-250/224-Basic-Calculator .c" headers/problem_151_200.h 1-50/11.-Container-With-Most-Water.c 1-50/4-Median-of-Two-Sorted-Arrays.c 1-50/14-Longest-Common-Prefix.c 1-50/15-3Sum.c 1-50/17-Letter-Combinations-of-a-Phone-Number.c 1-50/18-4Sum.c 1-50/20-Valid-Parentheses.c 1-50/19-Remove-Nth-Node-From-End-of-List.c 1-50/21-Merge-Two-Sorted-Lists.c 1-50/22-Generate-Parentheses.c 1-50/23-Merge-k-Sorted-Lists.c 1-50/26-Remove-Duplicates-from-Sorted-Array.c "1-50/28-Implement-strStr().c" 1-50/29-Divide-Two-Integers.c 1-50/13-Roman-to-Integer.c 1-50/10-Regular-Expression-Matching.c 1-50/33-Search-in-Rotated-Sorted-Array.c 1-50/32-Longest-Valid-Parentheses.c 1-50/34-Search-for-a-Range.c 1-50/36-Valid-Sudoku.c 1-50/38-Count-and-Say.c 1-50/41-First-Missing-Positive.c 1-50/42-Trapping-Rain-Water.c 1-50/44-Wildcard-Matching.c 1-50/46-Permutations.c 1-50/48-Rotate-Image.c "1-50/49-Group Anagrams.c" "1-50/50-Pow(x, n).c" 51-100/53-Maximum-Subarray.c headers/problem_51_100.h)
77
add_executable(C ${SOURCE_FILES})

C/headers/problem_1_50.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ void testAddTwoNumbers();
1919
int lengthOfLongestSubstring(char* s);
2020
void testLengthOfLongestSubstring();
2121

22+
// problem 4
23+
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size);
24+
void testFindMedianSortedArrays();
25+
2226
// problem 5
2327
char* longestPalindrome(char* s);
2428
void testLongestPalindrome();
@@ -42,4 +46,96 @@ void testIsPalindrome();
4246
// problem 11
4347
int maxArea(int* height, int heightSize);
4448
void testMaxArea();
49+
50+
// problem 13
51+
int romanToInt(char* s);
52+
void testRomanToInt();
53+
54+
// problem 14
55+
char *longestCommonPrefix(char **strs, int strsSize);
56+
void testLongestCommonPrefix();
57+
58+
// problem 15
59+
int** threeSum(int* nums, int numsSize, int* returnSize);
60+
void testThreeSum();
61+
62+
// problem 17
63+
char **letterCombinations(char *digits, int *returnSize);
64+
void testLetterCombinations();
65+
66+
// problem 18
67+
int **fourSum(int *nums, int numsSize, int target, int *returnSize);
68+
void testFourSum();
69+
70+
// problem 20
71+
bool isValid(char* s);
72+
void testIsValid();
73+
74+
// problem 21
75+
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2);
76+
void testMergeTwoLists();
77+
78+
// problem 22
79+
char** generateParenthesis(int n, int* returnSize);
80+
void testGenerateParenthesis();
81+
82+
// problem 23
83+
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize);
84+
void testMergeKLists();
85+
86+
// problem 26
87+
int removeDuplicates(int* nums, int numsSize);
88+
void testRemoveDuplicates();
89+
90+
// problem 28
91+
int strStr(char* haystack, char* needle);
92+
void testStrStr();
93+
94+
// problem 29
95+
int divide(int dividend, int divisor);
96+
void testDivide();
97+
98+
// problem 32
99+
int longestValidParentheses(char* s);
100+
void testLongestValidParentheses();
101+
102+
// problem 33
103+
int search(int* nums, int numsSize, int target);
104+
void testSearch();
105+
106+
// problem 34
107+
int *searchRange(int *nums, int numsSize, int target, int *returnSize);
108+
void testSearchRange();
109+
110+
// problem 36
111+
bool isValidSudoku(char **board, int boardRowSize, int boardColSize);
112+
void testIsValidSudoku();
113+
114+
// problem
115+
char* countAndSay(int n);
116+
void testCountAndSay();
117+
118+
// problem 41
119+
int firstMissingPositive(int* nums, int numsSize);
120+
void testFirstMissingPositive();
121+
122+
// problem 42
123+
int trap(int* height, int heightSize);
124+
void testTrap();
125+
126+
// problem 44
127+
bool isWildcardMatch(char* s, char* p);
128+
void testIsWildcardMatch();
129+
130+
// problem 46
131+
int** permute(int* nums, int numsSize, int* returnSize);
132+
void testPermute();
133+
134+
// problem 48
135+
void rotate(int** matrix, int matrixRowSize, int matrixColSize);
136+
void testRotate();
137+
138+
// problem 50
139+
double myPow(double x, int n);
140+
void testMyPow();
45141
#endif //C_PROBLEM_1_50_H

C/headers/problem_51_100.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Created by supercoderx on 2017/8/15.
3+
*/
4+
5+
#ifndef C_PROBLEM_51_100_H
6+
#define C_PROBLEM_51_100_H
7+
8+
// problem 53
9+
int maxSubArray(int* nums, int numsSize);
10+
void testMaxSubArray();
11+
#endif //C_PROBLEM_51_100_H

C/main.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include
66
#include "main.h"
77
#include "headers/problem_1_50.h"
8+
#include "headers/problem_51_100.h"
89
#include "headers/problem_151_200.h"
910
#include "headers/problem_201_250.h"
1011
#include "headers/problem_301_350.h"
@@ -41,13 +42,42 @@ void test_1_50() {
4142
// testTwoSum();
4243
// testAddTwoNumbers();
4344
// testLengthOfLongestSubstring();
45+
// testFindMedianSortedArrays();
4446
// testLongestPalindrome();
4547
// testConvert();
4648
// testReverse();
4749
// testMyAtoi();
4850
// testIsPalindrome();
49-
/// testConvert();
50-
testMaxArea();
51+
// testConvert();
52+
// testMaxArea();
53+
// testLongestCommonPrefix();
54+
// testThreeSum();
55+
// testLetterCombinations();
56+
// testFourSum();
57+
// testIsValid();
58+
// testMergeTwoLists();
59+
// testGenerateParenthesis();
60+
// testMergeKLists();
61+
// testRemoveDuplicates();
62+
// testStrStr();
63+
// testDivide();
64+
// testRomanToInt();
65+
// testLongestValidParentheses();
66+
// testSearch();
67+
// testSearchRange();
68+
// testIsValidSudoku();
69+
// testFirstMissingPositive();
70+
// testTrap();
71+
// testIsMatch();
72+
// testIsWildcardMatch();
73+
// testPermute();
74+
// testRotate();
75+
// testCountAndSay();
76+
testMyPow();
77+
}
78+
79+
void test_51_100() {
80+
testMaxSubArray();
5181
}
5282

5383
void test_151_200() {
@@ -64,6 +94,7 @@ void test_301_350() {
6494

6595
//
6696
int main(int argc, char *argv[]) {
67-
test_1_50();
97+
// test_1_50();
98+
test_51_100();
6899
// test_151_200();
69100
}

0 commit comments

Comments
 (0)