From cd679fd4613d8fb3c9745bdd666520ba7413b276 Mon Sep 17 00:00:00 2001 From: Paul Fugmann Date: Sat, 26 Oct 2019 16:04:24 +0200 Subject: [PATCH 1/5] 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; +} + From 2215758b3b323fa52b7c292e794160683434e7e2 Mon Sep 17 00:00:00 2001 From: dedsinQ Date: Sat, 26 Oct 2019 16:06:02 +0200 Subject: [PATCH 2/5] - Adding Roman to Integer (13.c) - Adding Divide Two Integers (29.c) - Adding Plus One (66.c) - Adding Bitwise AND of Numbers Range (201.c) - Readme updated --- leetcode/README.md | 4 ++++ leetcode/src/13.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ leetcode/src/201.c | 6 ++++++ leetcode/src/29.c | 35 +++++++++++++++++++++++++++++++++ leetcode/src/66.c | 22 +++++++++++++++++++++ 5 files changed, 116 insertions(+) create mode 100644 leetcode/src/13.c create mode 100644 leetcode/src/201.c create mode 100644 leetcode/src/29.c create mode 100644 leetcode/src/66.c diff --git a/leetcode/README.md b/leetcode/README.md index fe701f66..7bbd3722 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,14 +10,17 @@ 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| +|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c)|Easy| |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| |26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy| |27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy| |28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [C](./src/28.c)|Easy| +|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [C](./src/29.c)|Medium| |35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./src/35.c)|Easy| |53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C](./src/53.c)|Easy| +|66|[Plus One](https://leetcode.com/problems/plus-one/) | [C](./src/66.c)|Easy| |82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [C](./src/82.c)|Medium| |83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [C](./src/83.c)|Easy| |94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [C](./src/94.c)|Medium| @@ -38,6 +41,7 @@ LeetCode |173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c)|Medium| |190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C](./src/190.c)|Easy| |191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C](./src/191.c)|Easy| +|201|[Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C](./src/201.c)|Medium| |203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy| |206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy| |215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium| diff --git a/leetcode/src/13.c b/leetcode/src/13.c new file mode 100644 index 00000000..0da82cf9 --- /dev/null +++ b/leetcode/src/13.c @@ -0,0 +1,49 @@ +int romanToInt(char * s){ + int romanToInt = 0; + for (int i = 0; i < strlen(s); i++) { + switch(s[i]) { + case 'I': + if (i+1 < strlen(s)) { + if (s[i + 1] == 'V' || s[i + 1] == 'X') { + romanToInt -= 1; + break; + } + } + romanToInt += 1; + break; + case 'V': + romanToInt += 5; + break; + case 'X': + if (i+1 < strlen(s)) { + if (s[i + 1] == 'L' || s[i + 1] == 'C') { + romanToInt -= 10; + break; + } + } + romanToInt += 10; + break; + case 'L': + romanToInt += 50; + break; + case 'C': + if (i+1 < strlen(s)) { + if (s[i + 1] == 'D' || s[i + 1] == 'M') { + romanToInt -= 100; + break; + } + } + romanToInt += 100; + break; + case 'D': + romanToInt += 500; + break; + case 'M': + romanToInt += 1000; + break; + default: + break; + } + } + return romanToInt; +} \ No newline at end of file diff --git a/leetcode/src/201.c b/leetcode/src/201.c new file mode 100644 index 00000000..74a8e77c --- /dev/null +++ b/leetcode/src/201.c @@ -0,0 +1,6 @@ +int rangeBitwiseAnd(int m, int n){ + while (m < n) { + n &= n-1; + } + return n; +} \ No newline at end of file diff --git a/leetcode/src/29.c b/leetcode/src/29.c new file mode 100644 index 00000000..d80bd8bb --- /dev/null +++ b/leetcode/src/29.c @@ -0,0 +1,35 @@ +int divide(int dividend, int divisor){ + int sign = 1; + long int output = 0; + if (dividend < 0) { + sign *= -1; + + } else { + dividend *= -1; + } + if (divisor < 0) { + sign *= -1; + + } else { + divisor *= -1; + } + while (dividend <= divisor) { + long int tmp = 0; + long int div = divisor; + while (dividend <= div) { + tmp += (tmp+1); + dividend -= div; + div += div; + } + if (output >= INT_MAX) { + if (sign == -1) { + return INT_MIN; + } else { + return INT_MAX; + } + } + output += tmp; + } + + return output * sign; +} diff --git a/leetcode/src/66.c b/leetcode/src/66.c new file mode 100644 index 00000000..bfeec04e --- /dev/null +++ b/leetcode/src/66.c @@ -0,0 +1,22 @@ +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* plusOne(int* digits, int digitsSize, int* returnSize){ + for (int i = digitsSize-1; i >= 0; i--) { + if (digits[i] < 9) { + digits[i]++; + *returnSize = digitsSize; + return digits; + } else { + digits[i] = 0; + } + } + + int* newdigit = (int*)malloc((digitsSize+1) * sizeof(int)); + newdigit[0] = 1; + for (int i = 1; i < (digitsSize+1); i++) { + newdigit[i] = digits[i-1]; + } + *returnSize = digitsSize+1; + return newdigit; +} \ No newline at end of file From 95d7f25175418638432bb14f20ceb521046fc8bc Mon Sep 17 00:00:00 2001 From: Keval Date: Sun, 27 Oct 2019 21:22:09 +0000 Subject: [PATCH 3/5] leetcode question 7 --- leetcode/src/7.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 leetcode/src/7.c diff --git a/leetcode/src/7.c b/leetcode/src/7.c new file mode 100644 index 00000000..1bc03434 --- /dev/null +++ b/leetcode/src/7.c @@ -0,0 +1,13 @@ +#include + +int reverse(int x){ + int rev = 0; + while (x != 0) { + int pop = x % 10; + x /= 10; + if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0; + if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0; + rev = rev * 10 + pop; + } + return rev; +} From af273adef07cd43ff9aefd75688875ccce2843d7 Mon Sep 17 00:00:00 2001 From: Keval Date: Sun, 27 Oct 2019 21:24:08 +0000 Subject: [PATCH 4/5] readme for leetcode-7 --- leetcode/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/leetcode/README.md b/leetcode/README.md index 7bbd3722..f6709006 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,6 +10,7 @@ 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| +|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c)|Easy| |13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c)|Easy| |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| From 138da16aafd682fa38ee3baade4d806f363808f4 Mon Sep 17 00:00:00 2001 From: Keval Date: Sun, 27 Oct 2019 21:57:49 +0000 Subject: [PATCH 5/5] leetcode 9 --- leetcode/README.md | 1 + leetcode/src/9.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 leetcode/src/9.c diff --git a/leetcode/README.md b/leetcode/README.md index 7bbd3722..13aee908 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,6 +10,7 @@ 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| +|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c)|Easy| |13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c)|Easy| |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| diff --git a/leetcode/src/9.c b/leetcode/src/9.c new file mode 100644 index 00000000..28d05a0c --- /dev/null +++ b/leetcode/src/9.c @@ -0,0 +1,13 @@ +bool isPalindrome(int x){ + if(x < 0 || (x % 10 == 0 && x != 0)) { + return false; + } + + int revertedNumber = 0; + while(x > revertedNumber) { + revertedNumber = revertedNumber * 10 + x % 10; + x /= 10; + } + + return x == revertedNumber || x == revertedNumber/10; +}