File tree 5 files changed +245
-0
lines changed
longest-substring-without-repeating-characters
5 files changed +245
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ ํ์ด :
3
+ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๊ธฐ๋ฒ์ ์ฌ์ฉํด์ ํ์ด
4
+ right๊ฐ ํ์ฌ๊น์ง ๋ฌธ์์ด์ ํฌํจ๋์ง ์๋ ๋ฌธ์๋ฉด right์ ๋ฌธ์๋ฅผ set์ ์ถ๊ฐ ํ right ์ฆ๊ฐ, ๋ฌธ์์ด๊ธธ์ด๋ฅผ ๋๋ฆฌ๊ณ ์ต๋๊ธธ์ด์ ๋น๊ตํด์ ์
๋ฐ์ดํธ
5
+ ์ด๋ฏธ ๋ฌธ์์ด์ ํฌํจ๋๋ ๋ฌธ์๋ฉด left์ ๋ฌธ์๋ฅผ set์์ ์ ๊ฑฐ ํ left ์ฆ๊ฐ
6
+
7
+ ๋ฌธ์์ด ๊ธธ์ด : N
8
+
9
+ TC : O(N)
10
+ ๋ฌธ์์ด ์ ์ฒด์ ๋ํด 1ํ ์ํ
11
+
12
+ SC : O(N)
13
+ ํด์ํ
์ด๋ธ unordered_set์ ํฌ๊ธฐ๋ ๋ฌธ์์ด ๊ธธ์ด์ ๋น๋ก
14
+
15
+ ์ถ๊ฐ์ ์ธ ์ต์ ํ ๋ฐฉ๋ฒ :
16
+ int lastIdx[256] ์ ์ธํ๊ณ ์์คํค์ฝ๋ ์์น์ ๋ง์ง๋ง์ผ๋ก ํด๋น ๋ฌธ์๊ฐ ๋์จ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ๋ค
17
+ ex) a๊ฐ 10๋ฒ์จฐ ์ธ๋ฑ์ค์์ ๋์ค๋ฉด lastIdx['a'] = 10;
18
+ ๋์ค์ ์ค๋ณต๋๋ ๋ฌธ์๋ฅผ ๋ง๋๋ ๊ฒฝ์ฐ left๋ฅผ 1์ฉ ์ ์ง์ํค๋ ๊ฒ์ด ์๋๋ผ ์ค๋ณต๋ ๋ฌธ์์ ๋ง์ง๋ง ๋ฐ๋ก ๋ค์์ผ๋ก left๋ฅผ ์
๋ฐ์ดํธ
19
+ */
20
+
21
+ #include < unordered_set>
22
+
23
+ class Solution {
24
+ public:
25
+ int lengthOfLongestSubstring (string s) {
26
+ int ans = 0 ;
27
+ int left = 0 , right = 0 ;
28
+ unordered_set<char > lookup;
29
+
30
+ while (right < s.size ())
31
+ {
32
+ if (lookup.find (s[right]) == lookup.end ()) {
33
+ ans = max (ans, right - left + 1 );
34
+ lookup.insert (s[right]);
35
+ right++;
36
+ }
37
+ else {
38
+ lookup.erase (s[left]);
39
+ left++;
40
+ }
41
+ }
42
+
43
+ return ans;
44
+ }
45
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ ํ์ด :
3
+ grid๋ฅผ ์ํํ๋ฉด์ '1'์ ๋ง๋๋ฉด ์ํ์ข์ฐ๋ฅผ dfs๋ฅผ ํตํด ์ด๋ฏธ ํ์ํ ๋
์ด๋ผ๋ ๋ป์ผ๋ก '$'๋ก ๋ณ๊ฒฝํ๊ณ
4
+ ์๋ก์ด ๋
์ ๋ง๋ ๋๋ง๋ค cnt ์ฆ๊ฐ
5
+
6
+ grid ํฌ๊ธฐ M * N
7
+
8
+ TC : O(M * N)
9
+ dfs ํธ์ถ์ grid์ ํฌ๊ธฐ์ ๋น๋ก
10
+
11
+ SC : O(M * N)
12
+ dfs ํธ์ถ ์คํ๋ grid์ ํฌ๊ธฐ์ ๋น๋ก๋ก
13
+ */
14
+
15
+ #include < vector>
16
+ using namesapce std;
17
+
18
+ class Solution {
19
+ public:
20
+ int numIslands (vector<vector<char >>& grid) {
21
+ int cnt = 0 ;
22
+
23
+ for (int i = 0 ; i < grid.size (); i++) {
24
+ for (int j = 0 ; j < grid[0 ].size (); j++) {
25
+ if (grid[i][j] == ' 1' ) {
26
+ dfs (i, j, grid);
27
+ cnt++;
28
+ }
29
+ }
30
+ }
31
+ return (cnt);
32
+ }
33
+
34
+ void dfs (int row, int col, vector<vector<char >>& grid) {
35
+ if (row < 0 || row >= grid.size () || col < 0 || col >= grid[0 ].size () || grid[row][col] != ' 1' )
36
+ return ;
37
+ grid[row][col] = ' $' ;
38
+ dfs (row + 1 , col, grid);
39
+ dfs (row - 1 , col, grid);
40
+ dfs (row, col + 1 , grid);
41
+ dfs (row, col - 1 , grid);
42
+ }
43
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ ํ์ด :
3
+ linked-list๋ฅผ ์ํํ๋ฉฐ stack์ ์ฐจ๋ก๋๋ก pushํ๊ณ ๋ค ๋ฃ์ ํ
4
+ popํ๋ฉด์ ์ญ์์ผ๋ก ์ฐ๊ฒฐํด์ค๋ค
5
+
6
+ ๋
ธ๋์ ๊ฐ์ : N
7
+
8
+ TC : O(N)
9
+ ์ด 2ํ while ๋ฐ๋ณต๋ฌธ O(N + N)
10
+
11
+ SC : O(N)
12
+ ์คํ์ ๋
ธ๋์ ๊ฐ์์ ๋น๋ก
13
+ */
14
+
15
+ /* *
16
+ * Definition for singly-linked list.
17
+ * struct ListNode {
18
+ * int val;
19
+ * ListNode *next;
20
+ * ListNode() : val(0), next(nullptr) {}
21
+ * ListNode(int x) : val(x), next(nullptr) {}
22
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
23
+ * };
24
+ */
25
+
26
+ #include < stack>
27
+ using namespace std ;
28
+
29
+ struct ListNode {
30
+ int val;
31
+ ListNode *next;
32
+ ListNode () : val(0 ), next(nullptr ) {}
33
+ ListNode (int x) : val(x), next(nullptr ) {}
34
+ ListNode (int x, ListNode *next) : val(x), next(next) {}
35
+ };
36
+
37
+ class Solution {
38
+ public:
39
+ ListNode* reverseList (ListNode* head) {
40
+ stack<ListNode*> st;
41
+ ListNode dummy;
42
+ ListNode* prv = &dummy;
43
+ ListNode* cur;
44
+
45
+ while (head)
46
+ {
47
+ st.push (head);
48
+ head = head->next ;
49
+ }
50
+
51
+ while (!st.empty ())
52
+ {
53
+ cur = st.top ();
54
+ prv->next = cur;
55
+ prv = cur;
56
+ st.pop ();
57
+ }
58
+ prv->next = nullptr ;
59
+
60
+ return dummy.next ;
61
+ }
62
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ ํ์ด :
3
+ row0๊ณผ col0 ์ ํด๋น ์ค์ด 0์ด ๋๋์ง ์งํ๋ก ์ฌ์ฉํด์ ๊ณต๊ฐ๋ณต์ก๋ O(1)์ ๋ฌ์ฑํ ๊ฒ์ด๋ค
4
+
5
+ 1. row0๊ณผ col0 ์ค์ 0์ด ์กด์ฌํ๋์ง ๋ฏธ๋ฆฌ ํ์ธํ๊ณ ๋ณ์์ ์ ์ฅ
6
+
7
+ 2. matrix๋ฅผ ์ํ(์ฒซํ, ์ฒซ์ด ์ ์ธ)ํ๋ฉด์ 0์ธ ์นธ์ ๋ง๋๋ฉด ํด๋น ์ ๋ณด๋ฅผ ์ฒซํ, ์ฒซ์ด์ ์ ์ฅ
8
+
9
+ 3. ์ ์ฅ๋ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก matrix ์
๋ฐ์ดํธ
10
+
11
+ 4. ๋ณ์์ ๋ด์๋ ์ฒซํ, ์ฒซ์ด์ 0์ด ์กด์ฌํ๋๊ฐ ๋ฐํ์ผ๋ก ์
๋ฐ์ดํธ
12
+
13
+ matrix ํฌ๊ธฐ : M * N
14
+
15
+ TC : O(M * N)
16
+
17
+ SC : O(1)
18
+ */
19
+
20
+ #include < vector>
21
+ using namespace std ;
22
+
23
+ class Solution {
24
+ public:
25
+ void setZeroes (vector<vector<int >>& matrix) {
26
+ bool firstRowZero = false , firstColZero = false ;
27
+
28
+ int nRows = matrix.size ();
29
+ int nCols = matrix[0 ].size ();
30
+
31
+ for (int i = 0 ; i < nRows; i++)
32
+ if (matrix[i][0 ] == 0 )
33
+ firstColZero = true ;
34
+
35
+ for (int j = 0 ; j < nCols; j++)
36
+ if (matrix[0 ][j] == 0 )
37
+ firstRowZero = true ;
38
+
39
+ for (int i = 1 ; i <nRows; i++) {
40
+ for (int j = 1 ; j < nCols; j++) {
41
+ if (matrix[i][j] == 0 ) {
42
+ matrix[i][0 ] = 0 ;
43
+ matrix[0 ][j] = 0 ;
44
+ }
45
+ }
46
+ }
47
+
48
+ for (int i = 1 ; i < nRows; i++) {
49
+ for (int j = 1 ; j < nCols; j++)
50
+ if (matrix[i][0 ] == 0 || matrix[0 ][j] == 0 )
51
+ matrix[i][j] = 0 ;
52
+ }
53
+
54
+ if (firstRowZero) {
55
+ for (int i = 0 ; i < nCols; i++)
56
+ matrix[0 ][i] = 0 ;
57
+ }
58
+
59
+ if (firstColZero) {
60
+ for (int i = 0 ; i < nRows; i++)
61
+ matrix[i][0 ] = 0 ;
62
+ }
63
+ }
64
+ };
Original file line number Diff line number Diff line change
1
+ /*
2
+ ํ์ด :
3
+ ํน์ ์นธ์ ์ค๋ ๋ฐฉ๋ฒ์ ์ผ์ชฝ์์ ์ค๊ฑฐ๋ ์์์ ์ค๊ฑฐ๋ ๋ ์ค ํ๋์ด๋ฏ๋ก ๋ ๊ณณ์ unique path ๊ฐ์๋ฅผ ๋ํ๋ฉด ๊ตฌํ ์ ์๋ค
4
+ ํ row ๋ง๋ค๊ณ ๋๋ฒ์จฐ ์ค๋ถํฐ ๊ทธ ์ด์ ๊ฐ(์์์ ์ค๋ ๊ฐ์)๊ณผ ๊ทธ ์ index์ ๊ฐ(์ผ์ชฝ์์ ์ค๋ ๊ฐ์)๋ฅผ ๋ํ๋ฉด์ ๋ฐ๋ณตํ๋ค
5
+ ์ต์ข
์ ์ผ๋ก row์ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ๊ฐ return
6
+
7
+ rowํฌ๊ธฐ = M, col ํฌ๊ธฐ = N
8
+
9
+ TC : O(M * N)
10
+ ๋ชจ๋ ์นธ์ ๋ํด ์ํ
11
+
12
+ SC : O(N)
13
+ ํ row๋งํผ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
14
+ */
15
+
16
+ #include < vector>
17
+ using namespace std ;
18
+
19
+ class Solution {
20
+ public:
21
+ int uniquePaths (int m, int n) {
22
+ vector<int > row (n, 1 );
23
+
24
+ for (int i = 1 ; i < m; i++) {
25
+ for (int j = 1 ; j < n; j++) {
26
+ row[j] = row[j] + row[j - 1 ];
27
+ }
28
+ }
29
+ return row[n - 1 ];
30
+ }
31
+ };
You canโt perform that action at this time.
0 commit comments