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 smallestFromLeaf(self, root: TreeNode) -> str: | |
| smallest_so_far: 'List[Tuple[int]]' = [(27,)] # sentinel larger than any lowercase letter | |
| def explore(node: TreeNode, stack: 'List[int]') -> None: | |
| stack.append(node.val) | |
| if not node.left and not node.right: # leaf | |
| smallest_so_far[0] = min(smallest_so_far[0], tuple(reversed(stack))) | |
| else: | |
| for child in (node.left, node.right): | |
| if child: |
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 smallestFromLeaf(self, root: TreeNode) -> str: | |
| if not root: | |
| return "{" # sentinel larger than any lowercase letter | |
| string = chr(root.val + ord('a')) | |
| return string if root.left == root.right else min(self.smallestFromLeaf(root.left) + string, self.smallestFromLeaf(root.right) + string) |
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 num_islands(grid: Vec<Vec<char>>) -> i32 { | |
| if grid.is_empty() { | |
| return 0; | |
| } | |
| let mut visited = vec![vec![false; grid[0].len()]; grid.len()]; | |
| const directions: [i32; 5] = [-1, 0, 1, 0, -1]; | |
| let mut islands = 0; | |
| for x in 0..grid.len() { | |
| for y in 0..grid[x].len() { |
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 integer_break(n: i32) -> i32 { | |
| if n == 2 { | |
| return 1; // 1 * 1 | |
| } | |
| if n == 3 { | |
| return 2; // 1 * 2 | |
| } | |
| let mut product = 1; | |
| let mut tmp = n; |
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: | |
| ListNode* rotateRight(ListNode* head, int k) { | |
| if (head == nullptr || k == 0) return head; | |
| int length = 0; | |
| ListNode* last; | |
| for (auto temp = head; temp != nullptr; temp = temp->next, length += 1) { | |
| last = temp; | |
| } | |
| if (k > length) k %= length; |
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>> fourSum(vector<int>& nums, int target) { | |
| if (nums.size() < 4) return {}; | |
| sort(nums.begin(), nums.end()); | |
| set<vector<int>> solutions; | |
| int left = 0; | |
| int right = nums.size() - 1; | |
| for (int left = 0; left <= nums.size() - 4; ++left) { | |
| for (int right = left + 3; right < nums.size(); ++right) { |
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 flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool: | |
| if root1 is root2: | |
| return True | |
| if root1 is None or root2 is None or root1.val != root2.val: | |
| return False | |
| return ( | |
| self.flipEquiv(root1.left, root2.left) | |
| and self.flipEquiv(root1.right, root2.right) | |
| ) or ( |
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 longestValidParentheses(const string &s) { | |
| if (s.size() < 2) return 0; | |
| int d[s.size()]; | |
| d[s.size()-1] = 0; | |
| d[s.size()-2] = (s[s.size()-2] == '(' && s[s.size()-1] == ')') ? 2 : 0; | |
| int longest_so_far = d[s.size()-2]; | |
| for (int i = s.size()-3; i >= 0; --i) { | |
| d[i] = 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 longestValidParentheses(string s) { | |
| if (s.size() < 2) return 0; | |
| int d[s.size()]; | |
| d[s.size()-1] = 0; | |
| d[s.size()-2] = (s[s.size()-2] == '(' && s.back() == ')') ? 2 : 0; | |
| int longest_so_far = d[s.size()-2]; | |
| for (int i = s.size()-3; i >= 0; --i) { | |
| d[i] = 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 lengthOfLIS(const vector<int>& nums) { | |
| if (nums.empty()) return 0; | |
| vector<int> d(nums.size(), 1); | |
| for (int i = 1; i < nums.size(); ++i) { | |
| for (int j = 0; j < i; ++j) { | |
| if (nums[j] < nums[i] && d[j] + 1 > d[i]) { | |
| d[i] = d[j] + 1; | |
| } |