0. 문제
https://leetcode.com/problems/longest-substring-without-repeating-characters/
Longest Substring Without Repeating Characters - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문자열이 주어지면 문자를 반복하지 않고 가장 긴 부분 문자열s 의 길이를 찾습니다.
#1
Input: s = "abcabcbb"
Output: 3
#2
Input: s = "bbbbb"
Output: 1
#3
Input: s = "pwwkew"
Output: 3
1.언어
자바스크립트(JavaScript)
2. 문제 풀이
var lengthOfLongestSubstring = function(s) {
let str="", max=0, count=0;
for (let i of s){
num=str.indexOf(i);
if (num !== -1){
if (max < count) max = count;
str=str.slice(num+1,str.length)+i;
count -= num;
}else{
str += i;
count++;
}
}
return max > count ? max : count
};
Runtime: 108 ms, faster than 82.83% of JavaScript online submissions for Longest Substring Without Repeating Characters.
Memory Usage: 47.6 MB, less than 60.03% of JavaScript online submissions for Longest Substring Without Repeating Characters.
변수 str에 하나씩 저장 -> str.indexOf(n)으로 같은 문자 검사 -> 같은 문자가 있으면 splice로 자른 후 현재 문자 추가
변수명을 맨날 안 예쁘게 짓는 거 같아 걱정이다. 다른 코드들도 찾아보고 많이 배워야겠다.
'LeetCode > 코딩 테스트 스터디 1주차' 카테고리의 다른 글
[LeetCode] 2. Add Two Numbers 풀이 (0) | 2022.07.10 |
---|---|
[LeetCode] 14. Longest Common Prefix 풀이 (0) | 2022.07.10 |
[LeetCode] 13. Roman to Integer 풀이 (0) | 2022.07.10 |
[LeetCode] 9. Palindrome-Number 풀이 (0) | 2022.07.10 |
[LeetCode] 1. Two Sum 풀이 (0) | 2022.07.10 |