https://leetcode.com/problems/longest-substring-without-repeating-characters/
해석
Given a string s, find the length of the longest substring without repeating characters.
String s에서 가장 긴 부분 연속 string의 길이를 알파벳의 중복없이 구하라는 내용입니다.
코드
슬라이딩 윈도우로 풀 수 있는 간단한 문제입니다.
class Solution {
string sub;
int p, q, ans, tmp;
bool alpabet[130];
public:
int lengthOfLongestSubstring(string s) {
this->p = -1; this-> q = 0; this->ans = 0; this->tmp = 0;
while (q < s.length()) {
if (alpabet[s[q]]) {
alpabet[s[++p]] = false;
tmp--;
}
else {
alpabet[s[q++]] = true;
ans = ans < ++tmp ? tmp : ans;
}
}
return ans;
}
};
|
cs |
O(n)으로 푸니 98.51%의 코드보다 빠르다고 나옵니다.
아마도 map을 사용해서 푸는 사람이 많아서 그런가 봅니다.
알파벳 소문자만 인풋으로 주어지지 않는 다는 점만 주의해주면 쉬운 문제였습니다.
'Leetcode' 카테고리의 다른 글
leetcode Reverse Integer (0) | 2022.05.29 |
---|---|
Add Two Numbers leetcode 2 (0) | 2022.05.29 |
[MySQL] 176. Second Highest Salary[LeetCode Medium] - 1 (0) | 2022.05.25 |
[MySQL] 175. Combine Two Tables [LeetCode Easy] (0) | 2022.05.25 |
[MySQL] 3. 175. Combine Two Tables [LeetCode Easy] (0) | 2022.05.25 |