Compare commits

...

3 Commits

3 changed files with 103 additions and 0 deletions

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

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

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