Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Tree
- Counting
- 구현
- 파이썬
- dynamic programming
- Binary Tree
- Math
- geometry
- database
- sorting
- Number Theory
- string
- Class
- Stack
- Matrix
- 코딩테스트
- Method
- 코테
- implement
- greedy
- hash table
- 자바
- Binary Search
- simulation
- bit manipulation
- Data Structure
- array
- SQL
- java
- two pointers
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Medium] 3. Longest Substring Without Repeating Characters 본문
코딩테스트 풀이/JAVA
[LeetCode/Medium] 3. Longest Substring Without Repeating Characters
무지맘 2023. 7. 4. 22:071. Input
1) String s
2. Output
1) s의 부분 문자열 중에서 같은 문자가 없는 가장 긴 부분 문자열의 길이를 반환
3. Constraint
1) 0 <= s.length <= 5 * 10^4
2) s는 영어 대소문자, 숫자, 기호, 공백 문자로 이루어져 있다.
4. Example
Input: s = "abcabcbb" -> Output: 3
Input: s = "bbbbb" -> Output: 1
Input: s = "pwwkew" -> Output: 3
5. Code
1) 첫 코드
import java.util.*;
class Solution {
public int lengthOfLongestSubstring(String s) {
int answer = 0;
for(int i=0 ; i<s.length() ; i++){
List<Character> list = new ArrayList<Character>();
int j = i;
while(j<s.length() && !list.contains(s.charAt(j))){
list.add(s.charAt(j++));
}
answer = Math.max(answer, list.size());
}
return answer;
}
}
- 5%, 11%
2) HashSet을 이용한 코드
import java.util.*;
class Solution {
public int lengthOfLongestSubstring(String s) {
int answer = 0;
for(int i=0 ; i<s.length() ; i++){
HashSet<Character> set = new HashSet<>();
int j = i;
while(j<s.length() && !set.contains(s.charAt(j))){
set.add(s.charAt(j++));
}
answer = Math.max(answer, set.size());
}
return answer;
}
}
- 13%, 16%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Medium] 11. Container With Most Water (0) | 2023.07.06 |
---|---|
[LeetCode/Medium] 1493. Longest Subarray of 1's After Deleting One Element (0) | 2023.07.06 |
[LeetCode/Medium] 2. Add Two Numbers (0) | 2023.07.04 |
[프로그래머스/Lv.0] 정수를 나선형으로 배치하기 (0) | 2023.07.04 |
[프로그래머스/Lv.1] 크레인 인형뽑기 게임 (0) | 2023.07.04 |