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