1 字符串
Leetcode 3. Longest Substring Without Repeating Characters
Given a string s
, find the length of the longest substring without repeating characters.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
const int n = s.length();
int ans = 0;
for (int i = 0; i < n; ++i ){
vector<int> seen(128);
int j = i;
while(j < n && !seen[s[j]])
seen[s[j++]]++;
ans = max(ans, j - i);
}
return ans;
}
};
Leetcode 13 Roman to Integer
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, 2
is written as II
in Roman numeral, just two one's added together. 12
is written as XII
, which is simply X + II
. The number 27
is written as XXVII
, which is XX + V + II
class Solution {
public:
int romanToInt(string s) {
map<char, int> m{
{'I', 1}, {'V', 5},
{'X', 10}, {'L', 50},
{'C', 100}, {'D', 500},
{'M', 1000}};
char p = 0;
int ans = 0;
for(char c : s){
ans += m[c];
if (p && m[c] > m[p])
ans -= 2 * m[p];
p = c;
}
return ans;
}
};
58. Length of Last Word
Given a string s
consisting of some words separated by some number of spaces, return