Skip to content

Commit 63815b8

Browse files
add some algorithms
1 parent 4f22935 commit 63815b8

13 files changed

+312
-4
lines changed

C/51-100/55-Jump-Game.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Given an array of non-negative integers, you are initially positioned at the first index of the array.
3+
4+
* Each element in the array represents your maximum jump length at that position.
5+
6+
* Determine if you are able to reach the last index.
7+
8+
* For example:
9+
* A = [2,3,1,1,4], return true.
10+
11+
* A = [3,2,1,0,4], return false.
12+
* Created by supercoderhawk on 2017/8/17.
13+
*/
14+
15+
#include "../main.h"
16+
#include
17+
18+
bool canJump(int *nums, int numsSize) {
19+
if (numsSize == 0)
20+
return false;
21+
22+
int i = 0, maxJump;
23+
for (int reach = 0; i < numsSize && i <= reach; ++i) {
24+
maxJump = nums[i] + i;
25+
reach = reach > maxJump ? reach : maxJump;
26+
}
27+
return i == numsSize;
28+
}
29+
30+
void testCanJump() {
31+
int nums[] = {2, 3, 1, 1, 4};
32+
int nums1[] = {3, 2, 1, 0, 4};
33+
printf("%d", canJump(nums, sizeof(nums) / sizeof(int)));
34+
}

C/51-100/56-Merge-Intervals.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Given a collection of intervals, merge all overlapping intervals.
3+
4+
* For example,
5+
* Given [1,3],[2,6],[8,10],[15,18],
6+
* return [1,6],[8,10],[15,18].
7+
* Created by supercoderhawk on 2017/8/18.
8+
*/
9+
10+
#include
11+
#include
12+
#include "../main.h"
13+
14+
struct Interval* merge(struct Interval* intervals, int intervalsSize, int* returnSize) {
15+
16+
}
17+
18+
void testMerge(){
19+
20+
}

C/51-100/62-Unique-Paths.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
3+
4+
* The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
5+
6+
* How many possible unique paths are there?
7+
* Note: m and n will be at most 100.
8+
* Created by supercoderhawk on 2017/8/18.
9+
*/
10+
#include
11+
void trace(int x, int y,int m,int n, int *count){
12+
if(x == m && y == n){
13+
(*count)++;
14+
return;
15+
}
16+
if(x<m)
17+
trace(++x,y,m,n,count);
18+
if(y<n)
19+
trace(x,++y,m,n,count);
20+
}
21+
22+
int uniquePaths(int m, int n) {
23+
/*int count = 0;
24+
trace(1,1,m,n,&count);
25+
return count;*/
26+
int grid[m][n];
27+
for (int i = 0; i < m; ++i) {
28+
for (int j = 0; j <n ; ++j) {
29+
if(i == 0 || j == 0)
30+
grid[i][j] = 1;
31+
else
32+
grid[i][j] = grid[i-1][j]+grid[i][j-1];
33+
}
34+
}
35+
return grid[m-1][n-1];
36+
}
37+
38+
void testUniquePaths(){
39+
printf("%d",uniquePaths(3,3));
40+
}

C/51-100/66-Plus-One.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
3+
4+
* You may assume the integer do not contain any leading zero, except the number 0 itself.
5+
6+
* The digits are stored such that the most significant digit is at the head of the list.
7+
* Created by supercoderhawk on 2017/8/18.
8+
*/
9+
#include
10+
#include
11+
12+
int* plusOne(int* digits, int digitsSize, int* returnSize) {
13+
*returnSize = digitsSize;
14+
int *result = malloc(sizeof(int)*digitsSize), plus;
15+
result[digitsSize-1] = (digits[digitsSize-1]+1)%10;
16+
plus = (digits[digitsSize-1]+1)/10;
17+
for (int i = digitsSize-2; i >=0 ; --i) {
18+
if(plus!=0){
19+
result[i] = (digits[i]+1)%10;
20+
plus = (digits[i]+1)/10;
21+
}else{
22+
result[i] = digits[i];
23+
}
24+
}
25+
if(plus!=0){
26+
result = realloc(result, sizeof(int)*(digitsSize+1));
27+
for (int i = digitsSize-1; i >=0 ; --i) {
28+
result[i+1] = result[i];
29+
}
30+
result[0] = plus;
31+
(*returnSize)++;
32+
}
33+
34+
return result;
35+
}
36+
37+
void testPlusOne(){
38+
int a[] = {1,1,9},count=0;
39+
int* res = plusOne(a,sizeof(a)/ sizeof(int),&count);
40+
for (int i = 0; i <count ; ++i) {
41+
printf("%d,",res[i]);
42+
}
43+
}

C/51-100/69-Sqrt(x).c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Implement int sqrt(int x).
3+
4+
* Compute and return the square root of x.
5+
* Created by supercoderhawk on 2017/8/18.
6+
*/
7+
#include
8+
#include
9+
int mySqrt(int x) {
10+
if(x == 0)
11+
return 0;
12+
int left = 0,right = INT_MAX,mid;
13+
while (1){
14+
mid = (left+right)/2;
15+
if(mid>x/mid){
16+
right = mid -1;
17+
}else{
18+
if(mid+1>x/(mid+1))
19+
return mid;
20+
left = mid+1;
21+
}
22+
}
23+
}
24+
25+
void testMySqrt(){
26+
printf("%d",mySqrt(0));
27+
}

C/51-100/70-Climbing-Stairs.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* You are climbing a stair case. It takes n steps to reach to the top.
3+
4+
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5+
6+
* Note: Given n will be a positive integer.
7+
* Created by supercoderhawk on 2017/8/18.
8+
*/
9+
#include
10+
int climbStairs(int n) {
11+
if (n <= 1)
12+
return n;
13+
int curr_ways = 1, next_ways = 1;
14+
for (int i = 1; i <= n; ++i) {
15+
curr_ways = (next_ways += curr_ways) - curr_ways;
16+
}
17+
return curr_ways;
18+
}
19+
20+
void testClimbStairs(){
21+
printf("%d",climbStairs(2));
22+
}

C/51-100/73-Set-Matrix-Zeroes.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
3+
* Created by supercoderhawk on 2017/8/21.
4+
*/
5+
#include
6+
#include
7+
8+
void setZeroes(int **matrix, int matrixRowSize, int matrixColSize) {
9+
int isRow = 0;
10+
for (int i = 0; i < matrixRowSize; ++i) {
11+
for (int j = 0; j < matrixColSize; ++j) {
12+
if (matrix[i][j] == 0) {
13+
if (i == 0) {
14+
isRow = 1;
15+
matrix[0][j] = 0;
16+
} else {
17+
matrix[i][0] = matrix[0][j] = 0;
18+
}
19+
}
20+
}
21+
}
22+
23+
for (int k = 1; k < matrixRowSize; ++k) {
24+
if (matrix[k][0] == 0)
25+
for (int i = 0; i < matrixColSize; ++i) {
26+
matrix[k][i] = 0;
27+
}
28+
}
29+
30+
for (int l = 1; l < matrixColSize; ++l) {
31+
if (matrix[0][l] == 0) {
32+
for (int i = 1; i < matrixRowSize; ++i) {
33+
matrix[i][l] = 0;
34+
}
35+
}
36+
}
37+
if (matrix[0][0] == 0)
38+
for (int m = 0; m < matrixRowSize; ++m) {
39+
matrix[m][0] = 0;
40+
}
41+
if (isRow)
42+
for (int n = 0; n < matrixColSize; ++n) {
43+
matrix[0][n] = 0;
44+
}
45+
}
46+
47+
void testSetZeroes() {
48+
int **m = malloc(sizeof(int *) * 2);
49+
m[0] = malloc(sizeof(int) * 3);
50+
m[1] = malloc(sizeof(int) * 3);
51+
m[0][0] = 1;
52+
m[0][1] = 1;
53+
m[0][2] = 1;
54+
m[1][0] = 0;
55+
m[1][1] = 1;
56+
m[1][2] = 2;
57+
58+
setZeroes(m, 2, 3);
59+
for (int i = 0; i < 2; ++i) {
60+
for (int j = 0; j < 3; ++j) {
61+
printf("%d,", m[i][j]);
62+
}
63+
}
64+
}

C/51-100/75-Sort-Colors.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
3+
4+
* Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
5+
6+
* Note:
7+
* You are not suppose to use the library's sort function for this problem.
8+
* Created by supercoderhawk on 2017/8/21.
9+
*/
10+
11+
void sortColors(int* nums, int numsSize) {
12+
13+
}

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 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)
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 51-100/54-Spiral-Matrix.c 51-100/55-Jump-Game.c 51-100/56-Merge-Intervals.c 51-100/62-Unique-Paths.c 51-100/66-Plus-One.c "51-100/69-Sqrt(x).c" 51-100/70-Climbing-Stairs.c 51-100/73-Set-Matrix-Zeroes.c 51-100/75-Sort-Colors.c)
77
add_executable(C ${SOURCE_FILES})

C/headers/problem_1_50.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ void testMyAtoi();
4343
bool isPalindrome(int x);
4444
void testIsPalindrome();
4545

46+
// problem 11
47+
bool isMatch(char* s, char* p);
48+
void testIsMatch();
49+
4650
// problem 11
4751
int maxArea(int* height, int heightSize);
4852
void testMaxArea();
@@ -111,7 +115,7 @@ void testSearchRange();
111115
bool isValidSudoku(char **board, int boardRowSize, int boardColSize);
112116
void testIsValidSudoku();
113117

114-
// problem
118+
// problem 38
115119
char* countAndSay(int n);
116120
void testCountAndSay();
117121

C/headers/problem_51_100.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,32 @@
88
// problem 53
99
int maxSubArray(int* nums, int numsSize);
1010
void testMaxSubArray();
11+
12+
// problem 54
13+
int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize);
14+
void testSpiralOrder();
15+
16+
// problem 55
17+
bool canJump(int* nums, int numsSize);
18+
void testCanJump();
19+
20+
// problem 62
21+
int uniquePaths(int m, int n);
22+
void testUniquePaths();
23+
24+
// problem 66
25+
int* plusOne(int* digits, int digitsSize, int* returnSize);
26+
void testPlusOne();
27+
28+
// problem 69
29+
int mySqrt(int x);
30+
void testMySqrt();
31+
32+
// problem 70
33+
int climbStairs(int n);
34+
void testClimbStairs();
35+
36+
// problem 73
37+
void setZeroes(int **matrix, int matrixRowSize, int matrixColSize);
38+
void testSetZeroes();
1139
#endif //C_PROBLEM_51_100_H

C/main.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ void test_1_50() {
7373
// testPermute();
7474
// testRotate();
7575
// testCountAndSay();
76-
testMyPow();
76+
// testMyPow();
77+
testIsMatch();
7778
}
7879

7980
void test_51_100() {
80-
testMaxSubArray();
81+
// testMaxSubArray();
82+
// testSpiralOrder();
83+
// testCanJump();
84+
// testUniquePaths();
85+
// testPlusOne();
86+
// testMySqrt();
87+
// testClimbStairs();
88+
testSetZeroes();
8189
}
8290

8391
void test_151_200() {

C/main.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ struct ListNode *createList(const int *vals, int count);
1717

1818
void printList(struct ListNode *l);
1919

20+
struct Interval {
21+
int start;
22+
int end;
23+
};
24+
2025
#endif //C_MAIN_H

0 commit comments

Comments
 (0)