From cd679fd4613d8fb3c9745bdd666520ba7413b276 Mon Sep 17 00:00:00 2001 From: Paul Fugmann Date: Sat, 26 Oct 2019 16:04:24 +0200 Subject: [PATCH] Add leetcode String to Integer (atoi) (8.c) and Integer to Roman (12.c) --- leetcode/README.md | 2 + leetcode/src/12.c | 164 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/src/8.c | 60 +++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 leetcode/src/12.c create mode 100644 leetcode/src/8.c diff --git a/leetcode/README.md b/leetcode/README.md index fe701f66..015f1ae5 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,6 +10,8 @@ LeetCode |2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c)|Medium| |3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium| |4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard| +|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c)|Medium| +|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [C](./src/12.c)|Medium| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy| |21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c)|Easy| |24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium| diff --git a/leetcode/src/12.c b/leetcode/src/12.c new file mode 100644 index 00000000..ec33b979 --- /dev/null +++ b/leetcode/src/12.c @@ -0,0 +1,164 @@ +char *getOne(char c){ + switch (c) { + case '9': + return "IX"; + + case '8': + return "VIII"; + + case '7': + return "VII"; + + case '6': + return "VI"; + + case '5': + return "V"; + + case '4': + return "IV"; + + case '3': + return "III"; + + case '2': + return "II"; + + case '1': + return "I"; + + case '0': + return ""; + + default: + return NULL; + } +} + +char *getTen(char c){ + switch (c) { + case '9': + return "XC"; + + case '8': + return "LXXX"; + + case '7': + return "LXX"; + + case '6': + return "LX"; + + case '5': + return "L"; + + case '4': + return "XL"; + + case '3': + return "XXX"; + + case '2': + return "XX"; + + case '1': + return "X"; + + case '0': + return ""; + + default: + return NULL; + } + +} + +char *getHundred(char c){ + switch (c) { + case '9': + return "CM"; + + case '8': + return "DCCC"; + + case '7': + return "DCC"; + + case '6': + return "DC"; + + case '5': + return "D"; + + case '4': + return "CD"; + + case '3': + return "CCC"; + + case '2': + return "CC"; + + case '1': + return "C"; + + case '0': + return ""; + + default: + return NULL; + } +} + +char *getThousand(char c){ + switch (c) { + case '3': + return "MMM"; + + case '2': + return "MM"; + + case '1': + return "M"; + + default: + return NULL; + } +} + + + + +char * intToRoman(int num){ + int length; + char number[5]; + char *s = malloc(16*sizeof(char)); + + sprintf(number, "%i", num); + + length = strlen(number); + + switch (length){ + case 4: + sprintf(s,"%s%s%s%s", getThousand(number[0]), getHundred(number[1]), getTen(number[2]), getOne(number[3])); + break; + + case 3: + sprintf(s,"%s%s%s", getHundred(number[0]), getTen(number[1]), getOne(number[2])); + + break; + + case 2: + sprintf(s,"%s%s", getTen(number[0]), getOne(number[1])); + + break; + + case 1: + s = getOne(number[0]); + break; + + default: + break; + } + return s; +} diff --git a/leetcode/src/8.c b/leetcode/src/8.c new file mode 100644 index 00000000..91c4d736 --- /dev/null +++ b/leetcode/src/8.c @@ -0,0 +1,60 @@ +int myAtoi(char * str){ + int minusFlag = 0; + int length = strlen(str); + long int result = 0; + char numberBuffer[11]; + int counter = 0; + while(str[counter] == ' '){ + counter++; + } + str = &str[counter]; + counter = 0; + + + for(int i=0; i10){ + if(minusFlag){ + return __INT_MAX__*-1-1; + } else { + return __INT_MAX__; + } + } + + if(str[i] < '0' || str[i] > '9'){ + break; + } + if(counter == 0 && str[i] == '0'){ + continue; + } + + numberBuffer[counter]= str[i]; + counter++; + } + + int i = 0; + while(counter > 0) { + if(minusFlag){ + result -= (numberBuffer[i] - '0')*pow(10.0, counter-1); + }else{ + result += (numberBuffer[i] - '0')*pow(10.0, counter-1); + } + i++; + counter--; + } + + if(result > __INT_MAX__){ + return __INT_MAX__; + } else if(result < __INT_MAX__*-1-1){ + return __INT_MAX__*-1-1; + } + return result; +} +