forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#474 from dedsinQ/master
Solution for Leedcode problem 13, 29, 66 and 201
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |