Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans = 0;
unordered_map<char, int> 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;
}
};

39 changes: 39 additions & 0 deletions number-of-islands/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public:
int numIslands(vector<vector<char>>& 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<pair<int, int>> 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;
}
};

69 changes: 69 additions & 0 deletions reverse-linked-list/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};

60 changes: 60 additions & 0 deletions set-matrix-zeroes/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// class Solution {
// public:
// void setZeroes(vector<vector<int>>& matrix) {
// vector<pair<int, int>> 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<int, int> 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<vector<int>>& 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;
}
};

19 changes: 19 additions & 0 deletions unique-paths/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int> (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];
}
};