Merge branch 'master' into leetcode-algorithm-problem11

This commit is contained in:
Hai Hoang Dang 2019-10-28 09:25:47 -07:00 committed by GitHub
commit d2f3cfa6bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 370 additions and 0 deletions

View File

@ -10,15 +10,22 @@ 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|
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c)|Medium|
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c)|Easy|
|11| [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [C](./src/11.c)|Medium|
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [C](./src/12.c)|Medium|
|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|
@ -39,6 +46,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|

164
leetcode/src/12.c Normal file
View File

@ -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;
}

49
leetcode/src/13.c Normal file
View File

@ -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
leetcode/src/201.c Normal file
View File

@ -0,0 +1,6 @@
int rangeBitwiseAnd(int m, int n){
while (m < n) {
n &= n-1;
}
return n;
}

35
leetcode/src/29.c Normal file
View File

@ -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
leetcode/src/66.c Normal file
View File

@ -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;
}

13
leetcode/src/7.c Normal file
View File

@ -0,0 +1,13 @@
#include <limits.h>
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;
}

60
leetcode/src/8.c Normal file
View File

@ -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; i<length; i++){
if(i == 0) {
if(str[0] == '-'){
minusFlag = 1;
i++;
} else if(str[0] == '+'){
i++;
}
}
if(counter>10){
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;
}

13
leetcode/src/9.c Normal file
View File

@ -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;
}