mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge pull request #475 from Paul-Fugmann/master
Add leetcode String to Integer (atoi) (8.c) and Integer to Roman (12.c)
This commit is contained in:
commit
ff9877410b
@ -11,7 +11,9 @@ LeetCode
|
||||
|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|
|
||||
|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|
|
||||
|
164
leetcode/src/12.c
Normal file
164
leetcode/src/12.c
Normal 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;
|
||||
}
|
60
leetcode/src/8.c
Normal file
60
leetcode/src/8.c
Normal 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user