This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int lengthOfLongestSubstring(string s) { | |
| unordered_map<char, int> seen; | |
| int begin = 0, end = 0; | |
| int len = 0; | |
| string ans = ""; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| string minWindow(string s, string t) { | |
| unordered_map<char, int> table; | |
| // initialize frequency table for t | |
| for(char c : t){ | |
| table[c]++; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import deque | |
| class Solution(object): | |
| def levelOrder(self, root): | |
| q = deque() | |
| if(root != None): | |
| q.append(root) | |
| levels = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) { | |
| queue<vector<int>> bfs; | |
| int dist = 0; | |
| int m = matrix.size(), n = matrix[0].size(); | |
| for(int i = 0; i < matrix.size(); i++){ | |
| for(int j = 0; j < matrix[i].size(); j++){ | |
| if(matrix[i][j] == 0) bfs.push(vector<int>({i, j})); // keep track of all 0's |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<vector<int>> levelOrder(TreeNode* root) { | |
| queue<TreeNode*> even; | |
| queue<TreeNode*> odd; | |
| vector<vector<int>> result; | |
| even.push(root); | |
| while(!even.empty() || !odd.empty()){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<vector<int>> levelOrder(TreeNode* root) { | |
| vector<vector<int>> result; // array of arrays to store final result | |
| vector<int> levelresult; // array storing result for each level temporarily | |
| queue<TreeNode*> bfs; // bfs queue | |
| TreeNode* marker = new TreeNode(INT_MIN); // dummy node to mark end of level | |
| bfs.push(root); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void levelOrder(TreeNode* root) { | |
| queue<TreeNode*> bfs; | |
| bfs.push(root); | |
| while(!bfs.empty()){ | |
| TreeNode* current = bfs.front(); bfs.pop(); | |
| if(current){ | |
| cout << current->val << " "; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int countComponents(int n, vector<pair<int, int>>& edges) { | |
| vector<bool> visited(n, false); | |
| vector<vector<int>> adjList(n, vector<int>(0)); | |
| stack<int> dfs; | |
| int count = 0; | |
| int ans = 0; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int numIslands(vector<vector<char>>& grid) { | |
| int ans = 0; // number of groups | |
| // iterating through given grid to find a '1' | |
| for(int i = 0; i < grid.size(); i++){ | |
| for(int j = 0; j < grid[0].size(); j++){ | |
| if(grid[i][j] == '1'){ | |
| ans++; // start a group and visit all members of this group using dfs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| // DFS magic : initialize stack and do the following | |
| // pop top, retrieve neighbours for top, push unvisited neighbours to stack | repeat while stack not empty | |
| // because this is a tree no need to keep track of visited as no cycles possible. | |
| vector<int> preorderTraversal(TreeNode* root) { | |
| stack<TreeNode*> s; | |
| vector<int> result; | |