File tree 3 files changed +129
-0
lines changed
longest-substring-without-repeating-characters
3 files changed +129
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
3
+
4
+ class SolutionLongestSubstring {
5
+
6
+ public int lengthOfLongestSubstring (String s ) {
7
+ // 반복되는 문자열 중 가장 긴 문자열의 길이를 반환해라
8
+
9
+ // s.length = 0 또는 1이면 return s.length()
10
+ // lt = 0, rt = 1
11
+ // s[lt] != s[rt] 이면 SubString 여부를 검사하고 True면 rt++, count++
12
+ // s[lt] == s[rt] 이면 쌓인 카운트 정답에 적용하고 lt++, rt=lt+1, count 초기화
13
+ // rt가 끝에 도달하면 그때까지 쌓인 정답 반환
14
+
15
+ // TC: O(N*M), 전체 문자열 길이 N * 부분 문자열 길이 M
16
+ // SC: O(M), 부분 문자열 생성 공간
17
+
18
+ if (s .length () <= 1 ) {
19
+ return s .length ();
20
+ }
21
+
22
+ int lt = 0 ;
23
+ int rt = lt + 1 ;
24
+ int answer = 0 ;
25
+ int count = 0 ;
26
+ while (rt <= s .length ()) {
27
+ while (rt <= s .length () && isSubstring (s .substring (lt , rt ))) {
28
+ count ++;
29
+ rt ++;
30
+ }
31
+ answer = Math .max (answer , count );
32
+
33
+ lt ++;
34
+ rt = lt + 1 ;
35
+ count = 0 ;
36
+ }
37
+ return answer ;
38
+ }
39
+
40
+ // TC: O(M), 부분 문자열 str에 중복이 없는 경우 str의 길이
41
+ // SC: O(M), 부분 문자열 str의 중복이 없는 경우 str의 길이
42
+ private boolean isSubstring (String str ) {
43
+ Set <Character > set = new HashSet <>();
44
+ set .add (str .charAt (0 )); // 첫번째 문자는 바로 add
45
+ // 두번째 문자부터 중복 검사 대상
46
+ for (int i = 1 ; i < str .length (); i ++) {
47
+ // 중복 문자가 있거나, 공백이면 바로 false 리턴
48
+ if (!set .add (str .charAt (i )) || str .charAt (i ) == ' ' ) {
49
+ return false ;
50
+ }
51
+ }
52
+
53
+ return true ;
54
+ }
55
+ }
Original file line number Diff line number Diff line change
1
+ class SolutionNumberOfIslands {
2
+ char [][] sharedGrid ;
3
+ int [] dx = new int []{0 , 0 , -1 , 1 };
4
+ int [] dy = new int []{-1 , 1 , 0 , 0 };
5
+
6
+ public int numIslands (char [][] grid ) {
7
+ // 풀이
8
+ // 네 모서리가 물로 둘러쌓여있으면 아일랜드
9
+ // 아일랜드의 개수를 반환해라
10
+ // 땅인 경우 DFS 돌려서 순회하자
11
+ // 상하좌우 확인하면서 땅이면 물로 변경하면서 순회한다
12
+ // DFS 1회 당 answer += 1
13
+ // TC: O(N), N은 배열 원소 개수
14
+ // SC: O(N)
15
+ var answer = 0 ;
16
+
17
+ sharedGrid = grid ;
18
+ for (int i =0 ; i <grid .length ; i ++) {
19
+ for (int j =0 ; j <grid [0 ].length ; j ++) {
20
+ if (sharedGrid [i ][j ] == '1' ) {
21
+ dfs (i , j );
22
+ answer ++;
23
+ }
24
+ }
25
+ }
26
+
27
+ return answer ;
28
+ }
29
+
30
+ private void dfs (int i , int j ) {
31
+ sharedGrid [i ][j ] = '0' ;
32
+
33
+ for (int k =0 ; k <4 ; k ++) {
34
+ var x = i +dx [k ];
35
+ var y = j +dy [k ];
36
+ if (x >= 0 && y >= 0 && x < sharedGrid .length && y < sharedGrid [0 ].length && sharedGrid [x ][y ] == '1' ) {
37
+ dfs (x , y );
38
+ }
39
+ }
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+
2
+ //Definition for singly-linked list.
3
+ class ListNode {
4
+ int val ;
5
+ ListNode next ;
6
+ ListNode () {}
7
+ ListNode (int val ) { this .val = val ; }
8
+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
9
+ }
10
+
11
+ class SolutionReverseLinkedList {
12
+ public ListNode reverseList (ListNode head ) {
13
+ // 풀이: 링크드리스트 방향을 현재 기준으로 뒤집고, 노드를 다음으로 옮기며 반복한다
14
+ // next = curr.next
15
+ // prev > curr
16
+ // prev < curr
17
+ // prev = curr
18
+ // curr = next
19
+ // TC: O(N), head 길이 N만큼
20
+ // SC: O(1), prev/curr 2개만 메모리 사용
21
+
22
+ ListNode prev = null ;
23
+ ListNode curr = head ;
24
+ while (curr != null ) {
25
+ ListNode next = curr .next ;
26
+ curr .next = prev ;
27
+ prev = curr ;
28
+ curr = next ;
29
+ }
30
+
31
+ return prev ;
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments