forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
d7b492a
commit f5b3995
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test plus_one.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
/** | ||
** Return an array of size *returnSize. | ||
** Note: The returned array must be malloced, assume caller calls free(). | ||
**/ | ||
static int* plusOne(int* digits, int digitsSize, int* returnSize) | ||
{ | ||
int i, j, len = 0, carry = 1; | ||
int *result = malloc((digitsSize + 1) * sizeof(int)); | ||
for (i = digitsSize - 1; i >= 0 || carry; i--) { | ||
int n = digits[i] + carry; | ||
result[len++] = n % 10; | ||
carry = n / 10; | ||
} | ||
|
||
for (i = 0, j = len - 1; i < j; i++, j--) { | ||
int tmp = result[i]; | ||
result[i] = result[j]; | ||
result[j] = tmp; | ||
} | ||
|
||
*returnSize = len; | ||
return result; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 2) { | ||
fprintf(stderr, "Usage: ./test num\n"); | ||
exit(-1); | ||
} | ||
|
||
int i, count = strlen(argv[1]); | ||
int *digits = malloc(count * sizeof(int)); | ||
for (i = 0; i < count; i++) { | ||
digits[i] = argv[1][i] - '0'; | ||
} | ||
|
||
int len = 0; | ||
int *res = plusOne(digits, count, &len); | ||
for (i = 0; i < len; i++) { | ||
printf("%c", res[i] + '0'); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test add_binary.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
static char* addBinary(char* a, char* b) | ||
{ | ||
int len1 = strlen(a); | ||
int len2 = strlen(b); | ||
int len = len1 > len2 ? len1 + 1 : len2 + 1; | ||
char *result = malloc(len + 1); | ||
result[len] = '\0'; | ||
result[len - 1] = '\0'; | ||
|
||
int i, j, carry = 0; | ||
len = 0; | ||
for (i = len1 - 1, j = len2 - 1; carry || i >= 0 || j >= 0; i--, j--) { | ||
int na = i >= 0 ? a[i] - '0' : 0; | ||
int nb = j >= 0 ? b[j] - '0' : 0; | ||
result[len++] = (na ^ nb ^ carry) + '0'; | ||
carry = carry + na + nb >= 2 ? 1 : 0; | ||
} | ||
|
||
for (i = 0, j = len - 1; i < j; i++, j--) { | ||
char c = result[i]; | ||
result[i] = result[j]; | ||
result[j] = c; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 3) { | ||
fprintf(stderr, "Usage: ./test s1 s2\n"); | ||
exit(-1); | ||
} | ||
printf("%s\n", addBinary(argv[1], argv[2])); | ||
return 0; | ||
} |