Skip to content

Commit

Permalink
Decode ways
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 14, 2017
1 parent d429234 commit d79e238
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 091_decode_ways/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test decode_ways.c
70 changes: 70 additions & 0 deletions 091_decode_ways/decode_ways.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//static void recursive(char *s, char *stack, int len, char **results, int *count)
//{
// if (*s == '\0') {
// printf("%s\n", stack);
// results[*count] = malloc(len + 1);
// strcpy(results[*count], stack);
// (*count)++;
// } else {
// //while (*s != '\0') {
// stack[len++] = *s - '0' - 1 + 'A';
// recursive(s + 1, stack, len, results, count);
// stack[--len] = '\0';
// if (*(s + 1) != '\0') {
// int value = (*s - '0') * 10 + (*(s + 1) - '0');
// char c = (value - 1) + 'A';
// if (c >= 'A' && c <= 'Z') {
// stack[len++] = c;
// recursive(s + 2, stack, len, results, count);
// stack[--len] = '\0';
// //s++;
// }
// }
// //s++;
// //}
// }
//}

static void recursive(char *s, int *count)
{
int value;
char c;
if (*s == '\0') {
(*count)++;
} else {
value = *s - '0';
c = (value - 1) + 'A';
if (c >= 'A' && c <= 'Z') {
recursive(s + 1, count);
}
if (*(s + 1) != '\0' && *s != '0') {
value = (*s - '0') * 10 + (*(s + 1) - '0');
c = (value - 1) + 'A';
if (c >= 'A' && c <= 'Z') {
recursive(s + 2, count);
}
}
}
}

static int numDecodings(char* s) {
int count = 0;
if (*s != '\0' && *s != '0') {
recursive(s, &count);
}
return count;
}

int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: ./test number\n");
exit(-1);
}
printf("%d\n", numDecodings(argv[1]));
return 0;
}

0 comments on commit d79e238

Please sign in to comment.