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 | |
| DIRECTIONS = (-1, 0, 1, 0, -1) | |
| class Solution: | |
| def exist(self, board, word): | |
| visited = set() | |
| def explore(i, j, start): | |
| if start == len(word): |
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<int> rightSideView(TreeNode* root) { | |
| if (root == nullptr) return {}; | |
| vector<int> view; | |
| deque<TreeNode*> current, next; | |
| current.push_back(root); | |
| while (!current.empty()) { | |
| auto node = current.front(); | |
| if (current.size() == 1) view.push_back(node->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: | |
| def merge(self, head1, head2): | |
| merged = ListNode(-1) | |
| prev = merged | |
| while head1 and head2: | |
| if head1.val <= head2.val: | |
| temp = head1.next | |
| prev.next = head1 | |
| prev, head1 = head1, temp |
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
| use std::collections::HashSet; | |
| impl Solution { | |
| pub fn powerful_integers(x: i32, y: i32, bound: i32) -> Vec<i32> { | |
| let xs = if x != 1 { | |
| let mut xs = Vec::new(); | |
| let mut v = 1; | |
| while v <= bound { | |
| xs.push(v); | |
| v *= x; |
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
| struct TreeNode* pruneTree(struct TreeNode* root) { | |
| if (!root) return root; | |
| root->left = pruneTree(root->left); | |
| root->right = pruneTree(root->right); | |
| return (!root->val && !root->left && !root->right) ? NULL : 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
| class Solution { | |
| private: | |
| vector<int> nums; | |
| vector<int> indices; | |
| public: | |
| Solution(const vector<int> &nums): nums(nums), indices(nums.size()) { | |
| iota(indices.begin(), indices.end(), 0); | |
| sort(indices.begin(), indices.end(), [&nums](int x, int y) { | |
| return nums[x] < nums[y]; | |
| }); |
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
| import collections | |
| import random | |
| class Solution: | |
| def __init__(self, nums: 'List[int]'): | |
| self.index: 'Dict[int, List[int]]' = collections.defaultdict(list) | |
| for idx, num in enumerate(nums): | |
| self.index[num].append(idx) |
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: | |
| def addToArrayForm(self, A: 'List[int]', K: int) -> 'List[int]': | |
| carry, idx, result = 0, len(A) - 1, [] | |
| while K > 0 or idx >= 0 or carry: | |
| old_digit, idx = A[idx] if idx >= 0 else 0, idx - 1 | |
| sum, K = old_digit + (K % 10) + carry, K // 10 | |
| carry = sum // 10 | |
| result.append(sum % 10) | |
| return list(reversed(result)) |
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 | |
| import itertools | |
| DIRECTIONS = (-1, 0, 1, 0, -1) | |
| class Solution: | |
| def numEnclaves(self, A: List[List[int]]) -> int: | |
| q = deque(itertools.chain(((row, col) for row in (0, len(A)-1) for col in range(len(A[row])) if A[row][col] == 1), ((row, col) for col in (0, len(A[0])-1) for row in range(1, len(A)-1) if A[row][col] == 1))) | |
| for x, y in q: | |
| A[x][y] = 0 # mark seen |
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
| impl Solution { | |
| pub fn find_circle_num(m: Vec<Vec<i32>>) -> i32 { | |
| let mut seen = vec![false; m.len()]; | |
| fn dfs(m: &Vec<Vec<i32>>, i: usize, seen: &mut [bool]) { | |
| for j in 0..m.len() { | |
| if m[i][j] == 1 && !seen[j] { | |
| seen[j] = true; | |
| dfs(&m, j, seen); | |
| } |