Compare commits
3 Commits
37cab2ead6
...
691ff44f91
Author | SHA1 | Date | |
---|---|---|---|
691ff44f91 | |||
e463d172c8 | |||
1c8c5ed92d |
49
13_roman_to_integer/main.cpp
Normal file
49
13_roman_to_integer/main.cpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int romanToInt(string s) {
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
switch (s[i]) {
|
||||||
|
case 'M':
|
||||||
|
result += 1000;
|
||||||
|
break;
|
||||||
|
case 'D':
|
||||||
|
result += 500;
|
||||||
|
break;
|
||||||
|
case 'C':
|
||||||
|
if (i < s.length() - 1 && (s[i + 1] == 'M' || s[i + 1] == 'D')) {
|
||||||
|
result -= 100;
|
||||||
|
} else {
|
||||||
|
result += 100;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
result += 50;
|
||||||
|
break;
|
||||||
|
case 'X':
|
||||||
|
if (i < s.length() - 1 && (s[i + 1] == 'C' || s[i + 1] == 'L')) {
|
||||||
|
result -= 10;
|
||||||
|
} else {
|
||||||
|
result += 10;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'V':
|
||||||
|
result += 5;
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
if (i < s.length() - 1 && (s[i + 1] == 'X' || s[i + 1] == 'V')) {
|
||||||
|
result -= 1;
|
||||||
|
} else {
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
35
3_longest_substring_without_repeating_characters/main.cpp
Normal file
35
3_longest_substring_without_repeating_characters/main.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int lengthOfLongestSubstring(string s) {
|
||||||
|
int maxLen = 0, currentLen = 0;
|
||||||
|
queue<int> substr;
|
||||||
|
unordered_set<int> charset;
|
||||||
|
|
||||||
|
for (int index = 0; index < s.length(); index++) {
|
||||||
|
char currentChar = s[index];
|
||||||
|
|
||||||
|
// has repeat
|
||||||
|
if (charset.find(currentChar) != charset.end()) {
|
||||||
|
while (substr.front() != currentChar) {
|
||||||
|
charset.erase(substr.front());
|
||||||
|
substr.pop();
|
||||||
|
currentLen--;
|
||||||
|
}
|
||||||
|
charset.erase(currentChar);
|
||||||
|
substr.pop();
|
||||||
|
currentLen--;
|
||||||
|
}
|
||||||
|
|
||||||
|
charset.emplace(currentChar);
|
||||||
|
substr.emplace(currentChar);
|
||||||
|
currentLen++;
|
||||||
|
|
||||||
|
maxLen = max(maxLen, currentLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxLen;
|
||||||
|
}
|
||||||
|
};
|
19
9_palindrome_number/main.cpp
Normal file
19
9_palindrome_number/main.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
bool isPalindrome(int x) {
|
||||||
|
if (x < 0) return false;
|
||||||
|
|
||||||
|
string num = to_string(x);
|
||||||
|
int len = num.length();
|
||||||
|
for (int i = 0; i < len / 2; i++) {
|
||||||
|
if (num[i] != num[len - i - 1]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user