From 0a8d29d5e71efc04dce54cb5693b7ac3e3ae0238 Mon Sep 17 00:00:00 2001 From: begeekmyfriend Date: Tue, 11 Jul 2017 16:15:44 +0800 Subject: [PATCH] Rename Signed-off-by: begeekmyfriend --- {1_two_sum => 001_two_sum}/two_sum.c | 0 .../Makefile | 0 .../add_two_numbers.c | 0 .../Makefile | 0 .../longest_substring_without_repeat.c | 0 .../Makefile | 0 .../median_of_two_sorted_array.c | 0 .../Makefile | 0 .../longest_palindromic_substring.c | 0 {8_atoi => 008_atoi}/atoi.c | 0 .../Makefile | 0 .../roman_numeral.c | 0 .../Makefile | 0 .../letter_combinations.c | 0 {51_n_queens => 051_n_queens}/Makefile | 0 051_n_queens/n_queens.c | 129 ++++++++++++++++++ 51_n_queens/n_queens.c | 129 ------------------ 17 files changed, 129 insertions(+), 129 deletions(-) rename {1_two_sum => 001_two_sum}/two_sum.c (100%) rename {2_add_two_numbers => 002_add_two_numbers}/Makefile (100%) rename {2_add_two_numbers => 002_add_two_numbers}/add_two_numbers.c (100%) rename {3_longest_substring_without_repeat => 003_longest_substring_without_repeat}/Makefile (100%) rename {3_longest_substring_without_repeat => 003_longest_substring_without_repeat}/longest_substring_without_repeat.c (100%) rename {4_median_of_two_sorted_array => 004_median_of_two_sorted_array}/Makefile (100%) rename {4_median_of_two_sorted_array => 004_median_of_two_sorted_array}/median_of_two_sorted_array.c (100%) rename {5_longest_palindromic_substring => 005_longest_palindromic_substring}/Makefile (100%) rename {5_longest_palindromic_substring => 005_longest_palindromic_substring}/longest_palindromic_substring.c (100%) rename {8_atoi => 008_atoi}/atoi.c (100%) rename {13_roman_numeral => 013_roman_numeral}/Makefile (100%) rename {13_roman_numeral => 013_roman_numeral}/roman_numeral.c (100%) rename {17_letter_combinations_of_a_phone_number => 017_letter_combinations_of_a_phone_number}/Makefile (100%) rename {17_letter_combinations_of_a_phone_number => 017_letter_combinations_of_a_phone_number}/letter_combinations.c (100%) rename {51_n_queens => 051_n_queens}/Makefile (100%) create mode 100644 051_n_queens/n_queens.c delete mode 100644 51_n_queens/n_queens.c diff --git a/1_two_sum/two_sum.c b/001_two_sum/two_sum.c similarity index 100% rename from 1_two_sum/two_sum.c rename to 001_two_sum/two_sum.c diff --git a/2_add_two_numbers/Makefile b/002_add_two_numbers/Makefile similarity index 100% rename from 2_add_two_numbers/Makefile rename to 002_add_two_numbers/Makefile diff --git a/2_add_two_numbers/add_two_numbers.c b/002_add_two_numbers/add_two_numbers.c similarity index 100% rename from 2_add_two_numbers/add_two_numbers.c rename to 002_add_two_numbers/add_two_numbers.c diff --git a/3_longest_substring_without_repeat/Makefile b/003_longest_substring_without_repeat/Makefile similarity index 100% rename from 3_longest_substring_without_repeat/Makefile rename to 003_longest_substring_without_repeat/Makefile diff --git a/3_longest_substring_without_repeat/longest_substring_without_repeat.c b/003_longest_substring_without_repeat/longest_substring_without_repeat.c similarity index 100% rename from 3_longest_substring_without_repeat/longest_substring_without_repeat.c rename to 003_longest_substring_without_repeat/longest_substring_without_repeat.c diff --git a/4_median_of_two_sorted_array/Makefile b/004_median_of_two_sorted_array/Makefile similarity index 100% rename from 4_median_of_two_sorted_array/Makefile rename to 004_median_of_two_sorted_array/Makefile diff --git a/4_median_of_two_sorted_array/median_of_two_sorted_array.c b/004_median_of_two_sorted_array/median_of_two_sorted_array.c similarity index 100% rename from 4_median_of_two_sorted_array/median_of_two_sorted_array.c rename to 004_median_of_two_sorted_array/median_of_two_sorted_array.c diff --git a/5_longest_palindromic_substring/Makefile b/005_longest_palindromic_substring/Makefile similarity index 100% rename from 5_longest_palindromic_substring/Makefile rename to 005_longest_palindromic_substring/Makefile diff --git a/5_longest_palindromic_substring/longest_palindromic_substring.c b/005_longest_palindromic_substring/longest_palindromic_substring.c similarity index 100% rename from 5_longest_palindromic_substring/longest_palindromic_substring.c rename to 005_longest_palindromic_substring/longest_palindromic_substring.c diff --git a/8_atoi/atoi.c b/008_atoi/atoi.c similarity index 100% rename from 8_atoi/atoi.c rename to 008_atoi/atoi.c diff --git a/13_roman_numeral/Makefile b/013_roman_numeral/Makefile similarity index 100% rename from 13_roman_numeral/Makefile rename to 013_roman_numeral/Makefile diff --git a/13_roman_numeral/roman_numeral.c b/013_roman_numeral/roman_numeral.c similarity index 100% rename from 13_roman_numeral/roman_numeral.c rename to 013_roman_numeral/roman_numeral.c diff --git a/17_letter_combinations_of_a_phone_number/Makefile b/017_letter_combinations_of_a_phone_number/Makefile similarity index 100% rename from 17_letter_combinations_of_a_phone_number/Makefile rename to 017_letter_combinations_of_a_phone_number/Makefile diff --git a/17_letter_combinations_of_a_phone_number/letter_combinations.c b/017_letter_combinations_of_a_phone_number/letter_combinations.c similarity index 100% rename from 17_letter_combinations_of_a_phone_number/letter_combinations.c rename to 017_letter_combinations_of_a_phone_number/letter_combinations.c diff --git a/51_n_queens/Makefile b/051_n_queens/Makefile similarity index 100% rename from 51_n_queens/Makefile rename to 051_n_queens/Makefile diff --git a/051_n_queens/n_queens.c b/051_n_queens/n_queens.c new file mode 100644 index 0000000..8f7f9ba --- /dev/null +++ b/051_n_queens/n_queens.c @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2015, Leo Ma + */ + +#include +#include +#include + +static int N; + +static inline int conflict(int *stack, int i, int j) +{ + int k; + for (k = 0; k < i; k++) { + if (j == stack[k] || abs(i - k) == abs(j - stack[k])) { + return 1; + } + } + + return 0; +} + +static inline void push(int *stack, int i, int j) +{ + stack[i] = j; + // printf("push %d %d\n", i, j); +} + +static inline int pop(int *stack, int i) +{ + int j = stack[i]; + stack[i] = -1; + // printf("pop %d %d\n", i, j); + return j; +} + +static inline int top(int *stack) +{ + int i; + for (i = N - 1; i >= 0; i--) { + if (stack[i] != -1) { + return i; + } + } + + return 0; +} + +void show(int *stack) +{ + int i, j; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + if (j == stack[i]) { + printf("Q"); + } else { + printf("."); + } + } + printf("\n"); + } +} + +int main(int argc, char **argv) +{ + int i, j, sum = 0; + int *stack; + + if (argc != 2) { + printf("Usage: ./queen 8\n"); + exit(-1); + } + + N = atoi(argv[1]); + if (N <= 0) { + exit(0); + } + + stack = malloc(N * sizeof(*stack)); + if (stack == NULL) { + exit(-1); + } + + if (N == 1) { + stack[0] = 0; + show(stack); + printf("This is the %dth solution.\n", ++sum); + exit(0); + } + + for (i = 0; i < N; i++) { + stack[i] = -1; + } + + i = j = 0; + for (; ;) { + for (; i < N; i++) { + while (j < N) { + if (conflict(stack, i, j)) { + while (j == N - 1) { + /* No space for row[i] so backtracking is needed */ + if (--i < 0) { + free(stack); + exit(0); + } + j = pop(stack, i); + } + j++; + } else { + push(stack, i, j); + break; + } + } + j = 0; + } + + i = top(stack); + if (i == N - 1) { + show(stack); + printf("This is the %dth solution.\n", ++sum); + } + + j = pop(stack, i); + j++; + } + + assert(0); + return 0; +} diff --git a/51_n_queens/n_queens.c b/51_n_queens/n_queens.c deleted file mode 100644 index ede9369..0000000 --- a/51_n_queens/n_queens.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2015, Leo Ma - */ - -#include -#include -#include - -static int N; - -int conflict(int *stack, int i, int j) -{ - int k; - for (k = 0; k < i; k++) { - if (j == stack[k] || abs(i - k) == abs(j - stack[k])) { - return 1; - } - } - - return 0; -} - -void push(int *stack, int i, int j) -{ - stack[i] = j; - // printf("push %d %d\n", i, j); -} - -int pop(int *stack, int i) -{ - int j = stack[i]; - stack[i] = -1; - // printf("pop %d %d\n", i, j); - return j; -} - -int top(int *stack) -{ - int i; - for (i = N - 1; i >= 0; i--) { - if (stack[i] != -1) { - return i; - } - } - - return 0; -} - -void show(int *stack) -{ - int i, j; - for (i = 0; i < N; i++) { - for (j = 0; j < N; j++) { - if (j == stack[i]) { - printf("Q"); - } else { - printf("."); - } - } - printf("\n"); - } -} - -int main(int argc, char **argv) -{ - int i, j, sum = 0; - int *stack; - - if (argc != 2) { - printf("Usage: ./queen 8\n"); - exit(-1); - } - - N = atoi(argv[1]); - if (N <= 0) { - exit(0); - } - - stack = malloc(N * sizeof(*stack)); - if (stack == NULL) { - exit(-1); - } - - if (N == 1) { - stack[0] = 0; - show(stack); - printf("This is the %dth solution.\n", ++sum); - exit(0); - } - - for (i = 0; i < N; i++) { - stack[i] = -1; - } - - i = j = 0; - for (; ;) { - for (; i < N; i++) { - while (j < N) { - if (conflict(stack, i, j)) { - while (j == N - 1) { - /* No space for row[i] so backtracking is needed */ - if (--i < 0) { - free(stack); - exit(0); - } - j = pop(stack, i); - } - j++; - } else { - push(stack, i, j); - break; - } - } - j = 0; - } - - i = top(stack); - if (i == N - 1) { - show(stack); - printf("This is the %dth solution.\n", ++sum); - } - - j = pop(stack, i); - j++; - } - - assert(0); - return 0; -}