File tree Expand file tree Collapse file tree 3 files changed +97
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change 1+ // 1๋ฒ ํ์ด
2+ function lengthOfLongestSubstring1 ( s : string ) : number {
3+ let seen = new Set < string > ( ) ;
4+ let start = 0 ;
5+ let maxLength = 0 ;
6+
7+ for ( let end = 0 ; end < s . length ; end ++ ) {
8+ const char = s [ end ] ;
9+
10+ // ์ค๋ณต๋ ๋ฌธ์๊ฐ ๋์ค๋ฉด Set์์ ์ ๊ฑฐํ๋ฉด์ start๋ฅผ ์์ผ๋ก ์ด๋
11+ while ( seen . has ( char ) ) {
12+ seen . delete ( s [ start ] ) ;
13+ start ++ ;
14+ }
15+
16+ // ์ค๋ณต์ด ์์ผ๋ฉด ํ์ฌ ์๋์ฐ ๊ธธ์ด ๊ฐฑ์
17+ seen . add ( char ) ;
18+ maxLength = Math . max ( maxLength , end - start + 1 ) ;
19+ }
20+
21+ return maxLength ;
22+ }
23+
24+ // 2๋ฒ ํ์ด
25+ function lengthOfLongestSubstring2 ( s : string ) : number {
26+ let substring = "" ;
27+ let maxLength = 0 ;
28+
29+ for ( let i = 0 ; i < s . length ; i ++ ) {
30+ const char = s [ i ] ;
31+
32+ // ์ค๋ณต ๋ฌธ์๊ฐ ์๋ค๋ฉด, ๊ทธ ๋ฌธ์ ์ดํ๋ถํฐ ์๋ผ๋
33+ if ( substring . includes ( char ) ) {
34+ const index = substring . indexOf ( char ) ;
35+ substring = substring . slice ( index + 1 ) ;
36+ }
37+
38+ substring += char ;
39+ maxLength = Math . max ( maxLength , substring . length ) ;
40+ }
41+
42+ return maxLength ;
43+ }
Original file line number Diff line number Diff line change 1+ function numIslands ( grid : string [ ] [ ] ) : number {
2+ let count = 0 ;
3+
4+ for ( let row = 0 ; row < grid . length ; row ++ ) {
5+ for ( let col = 0 ; col < grid [ 0 ] . length ; col ++ ) {
6+ if ( grid [ row ] [ col ] === "1" ) {
7+ count ++ ; // ์๋ก์ด ์ฌ ๋ฐ๊ฒฌ!
8+ dfs ( row , col ) ; // ์ฐ๊ฒฐ๋ ๋ชจ๋ "1"์ 0์ผ๋ก ๋ฐ๊พธ๊ธฐ
9+ }
10+ }
11+ }
12+
13+ function dfs ( row : number , col : number ) {
14+ // 1. ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฑฐ๋ ์ด๋ฏธ ๋ฌผ(0)์ด๋ฉด return
15+ if ( row < 0 || col < 0 || row >= grid . length || col >= grid [ 0 ] . length )
16+ return ;
17+ if ( grid [ row ] [ col ] === "0" ) return ;
18+
19+ // 2. ํ์ฌ ์ขํ๋ฅผ 0์ผ๋ก ๋ฐ๊พธ๊ณ
20+ grid [ row ] [ col ] = "0" ;
21+
22+ // 3. ์ํ์ข์ฐ๋ก dfs ์ฌ๊ท ํธ์ถ
23+ dfs ( row - 1 , col ) ; // ์
24+ dfs ( row + 1 , col ) ; // ์๋
25+ dfs ( row , col - 1 ) ; // ์ผ์ชฝ
26+ dfs ( row , col + 1 ) ; // ์ค๋ฅธ์ชฝ
27+ }
28+
29+ return count ;
30+ }
Original file line number Diff line number Diff line change 1+ // Definition for singly-linked list.
2+ class ListNode {
3+ val : number ;
4+ next : ListNode | null ;
5+ constructor ( val ?: number , next ?: ListNode | null ) {
6+ this . val = val === undefined ? 0 : val ;
7+ this . next = next === undefined ? null : next ;
8+ }
9+ }
10+
11+ function reverseList ( head : ListNode | null ) : ListNode | null {
12+ let prev : ListNode | null = null ;
13+ let current = head ;
14+ let next = null ;
15+
16+ while ( current !== null ) {
17+ const next = current . next ; // 1. ๋ค์ ๋
ธ๋๋ฅผ ๊ธฐ์ตํด๋๊ณ
18+ current . next = prev ; // 2. ํ์ฌ ๋
ธ๋๊ฐ ์ด์ ๋
ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋๋ก
19+ prev = current ; // 3. ์ด์ ๋
ธ๋๋ฅผ ์ง๊ธ ๋
ธ๋๋ก ์
๋ฐ์ดํธ
20+ current = next ; // 4. ํ์ฌ ๋
ธ๋๋ฅผ ๋ค์ ๋
ธ๋๋ก ์ด๋
21+ }
22+
23+ return prev ;
24+ }
You canโt perform that action at this time.
0 commit comments