문제 Given a string, find the length of the longest substring without repeating characters.
예시는 https://leetcode.com/problems/longest-substring-without-repeating-characters/
링크에서 확인
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
|
class Solution {
public int lengthOfLongestSubstring(String s) {
int resCnt = 0;
if (s.length() > 0) {
char[] a = s.toCharArray();
for (int i = 0; i < a.length; i++) {
int tempCnt = 0;
HashMap<Character, Integer> strArr = new HashMap<Character, Integer>();
tempCnt++;
for (int j = i + 1; j < a.length; j++) {
if (strArr.containsKey(a[j])) {
break;
} else {
tempCnt++;
}
}
resCnt = (tempCnt > resCnt) ? tempCnt : resCnt;
}
}
return resCnt;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
시간복잡도 O(n^2)
1.hashmap에 담았을때가 두번째로 빠르다.
2.첫번째로 빠른것은 int형 배열
3.list방식은 가장 느리다.
'개발 > algorithm' 카테고리의 다른 글
okky에서 본 문제 factorial 100 without biginteger (0) | 2020.04.14 |
---|---|
leetcode 4번문제 Median of Two Sorted Arrays (0) | 2020.04.14 |
leetcode 445번문제 add two numbers 2 (0) | 2020.04.14 |
leetcode 2번문제 add two numbers (0) | 2020.04.14 |
leetcode 653 Two Sum IV - Input is a BST (0) | 2020.04.14 |