Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#474 from dedsinQ/master
Browse files Browse the repository at this point in the history
Solution for Leedcode problem 13, 29, 66 and 201
  • Loading branch information
danghai authored Oct 26, 2019
2 parents 11ea4be + 2215758 commit 44049cd
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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|
Expand Down
49 changes: 49 additions & 0 deletions leetcode/src/13.c
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions leetcode/src/201.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int rangeBitwiseAnd(int m, int n){
while (m < n) {
n &= n-1;
}
return n;
}
35 changes: 35 additions & 0 deletions leetcode/src/29.c
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions leetcode/src/66.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 44049cd

Please sign in to comment.