From d8982edd063bf67cdef42900c2764718126d8850 Mon Sep 17 00:00:00 2001 From: ys-han00 Date: Mon, 22 Dec 2025 21:44:51 +0900 Subject: [PATCH] Solutions #223 & #243 & #258 & #273 & #283 --- .../ys-han00.cpp | 28 ++++++++ number-of-islands/ys-han00.cpp | 39 +++++++++++ reverse-linked-list/ys-han00.cpp | 69 +++++++++++++++++++ set-matrix-zeroes/ys-han00.cpp | 60 ++++++++++++++++ unique-paths/ys-han00.cpp | 19 +++++ 5 files changed, 215 insertions(+) create mode 100644 longest-substring-without-repeating-characters/ys-han00.cpp create mode 100644 number-of-islands/ys-han00.cpp create mode 100644 reverse-linked-list/ys-han00.cpp create mode 100644 set-matrix-zeroes/ys-han00.cpp create mode 100644 unique-paths/ys-han00.cpp diff --git a/longest-substring-without-repeating-characters/ys-han00.cpp b/longest-substring-without-repeating-characters/ys-han00.cpp new file mode 100644 index 0000000000..acd55d3b80 --- /dev/null +++ b/longest-substring-without-repeating-characters/ys-han00.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int ans = 0; + unordered_map mp; + + for(int i = 0; i < s.size(); i++) { + if(mp[s[i]] == 0) { + mp[s[i]] = i; + ans = max(ans, (int)mp.size()); + } + else { + int tmp = mp[s[i]]; + for (auto it = mp.begin(); it != mp.end(); ) { + it->second -= tmp; + if (it->second < 0) + it = mp.erase(it); + else + it++; + } + mp[s[i]] = i; + } + } + + return ans; + } +}; + diff --git a/number-of-islands/ys-han00.cpp b/number-of-islands/ys-han00.cpp new file mode 100644 index 0000000000..1b287a6a45 --- /dev/null +++ b/number-of-islands/ys-han00.cpp @@ -0,0 +1,39 @@ +class Solution { +public: + int numIslands(vector>& grid) { + int n = grid.size(), m = grid[0].size(); + int ans = 0; + int dx[] = {-1, 0, 1, 0}; + int dy[] = {0, 1, 0, -1}; + + for(int i = 0; i < n; i++) { + for(int j = 0; j < m; j++) { + if(grid[i][j] == '0') + continue; + + queue> que; + que.push({i, j}); + grid[i][j] = '0'; + while(!que.empty()) { + int x = que.front().first; + int y = que.front().second; + que.pop(); + + for(int k = 0; k < 4; k++) { + int new_x = x + dx[k]; + int new_y = y + dy[k]; + if(new_x < 0 || new_x >= n || new_y < 0 || new_y >=m || grid[new_x][new_y] == '0') + continue; + + que.push({new_x, new_y}); + grid[new_x][new_y] = '0'; + } + } + ans++; + } + } + + return ans; + } +}; + diff --git a/reverse-linked-list/ys-han00.cpp b/reverse-linked-list/ys-han00.cpp new file mode 100644 index 0000000000..68c7ad9394 --- /dev/null +++ b/reverse-linked-list/ys-han00.cpp @@ -0,0 +1,69 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +// class Solution { +// public: +// ListNode* reverseList(ListNode* head) { +// if(!head || !head->next) +// return head; + +// ListNode* curr = head; +// ListNode* next = head->next; +// curr->next = nullptr; + +// while(next) { +// ListNode* temp = next->next; +// next->next = curr; + +// curr = next; +// next = temp; +// } + +// return curr; +// } +// }; + +// class Solution { +// public: +// ListNode* rec(ListNode* old, ListNode* now) { +// if(!now->next) { +// now->next = old; +// return now; +// } +// ListNode* tmp = now->next; +// now->next = old; +// return rec(now, tmp); +// } +// ListNode* reverseList(ListNode* head) { +// if(!head || !head->next) +// return head; + +// ListNode* ans = rec(head, head->next); +// head->next = nullptr; + +// return ans; +// } +// }; + +class Solution { +public: + ListNode* reverseList(ListNode* head) { + if(!head || !head->next) + return head; + + ListNode* new_tail = head->next; + ListNode* new_head = reverseList(new_tail); + new_tail->next = head; + head->next = nullptr; + + return new_head; + } +}; + diff --git a/set-matrix-zeroes/ys-han00.cpp b/set-matrix-zeroes/ys-han00.cpp new file mode 100644 index 0000000000..bde4cb1fe3 --- /dev/null +++ b/set-matrix-zeroes/ys-han00.cpp @@ -0,0 +1,60 @@ +// class Solution { +// public: +// void setZeroes(vector>& matrix) { +// vector> t; +// for(int i = 0; i < matrix.size(); i++) +// for(int j = 0; j < matrix[0].size(); j++) +// if(matrix[i][j] == 0) +// t.push_back({i, j}); + + +// for(pair x : t) { +// for(int k = 0; k < matrix.size(); k++) +// matrix[k][x.second] = 0; +// for(int k = 0; k < matrix[0].size(); k++) +// matrix[x.first][k] = 0; +// } + +// return; +// } +// }; + +class Solution { +public: + void setZeroes(vector>& matrix) { + bool first_row = 0, first_col = 0; + + for(int i = 0; i < matrix.size(); i++) + if(matrix[i][0] == 0) + first_col = true; + + for(int i = 0; i < matrix[0].size(); i++) + if(matrix[0][i] == 0) + first_row = true; + + for(int i = 1; i < matrix.size(); i++) { + for(int j = 1; j < matrix[0].size(); j++) { + if(matrix[i][j] == 0) { + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + + for(int i = 1; i < matrix.size(); i++) + for(int j = 1; j < matrix[0].size(); j++) + if(matrix[i][0] == 0 || matrix[0][j] == 0) + matrix[i][j] = 0; + + if(first_row) + for(int k = 0; k < matrix[0].size(); k++) + matrix[0][k] = 0; + + if(first_col) + for(int k = 0; k < matrix.size(); k++) + matrix[k][0] = 0; + + return; + } +}; + diff --git a/unique-paths/ys-han00.cpp b/unique-paths/ys-han00.cpp new file mode 100644 index 0000000000..30883d22db --- /dev/null +++ b/unique-paths/ys-han00.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> dp(m, vector (n, 0)); + + for(int i = 0; i < m; i++) + dp[i][0] = 1; + + for(int j = 0; j < n; j++) + dp[0][j] = 1; + + for(int i = 1; i < m; i++) + for(int j = 1; j < n; j++) + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + + return dp[m-1][n-1]; + } +}; +