This file contains 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
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, val=0, left=None, right=None): | |
# self.val = val | |
# self.left = left | |
# self.right = right | |
class Solution: | |
def pruneTree(self, root: TreeNode) -> TreeNode: | |
def helper(n: TreeNode) -> int: | |
if not n: return 0 |
This file contains 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
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode() {} | |
* TreeNode(int val) { this.val = val; } | |
* TreeNode(int val, TreeNode left, TreeNode right) { | |
* this.val = val; |
This file contains 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[] findRedundantConnection(int[][] edges) { | |
int[] root = new int[edges.length + 1]; | |
Arrays.fill(root, -1); | |
for (int[] edge : edges) { | |
int x = find(root, edge[0]), y = find(root, edge[1]); | |
if (x == y) return edge; | |
root[x] = y; | |
} | |
return new int[]{}; |
This file contains 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 findRedundantConnection(self, edges: List[List[int]]) -> List[int]: | |
def find(root: list, i: int) -> int: | |
while root[i] != -1: i = root[i] | |
return i | |
root = [-1] * (len(edges) + 1) | |
for edge in edges: | |
x, y = find(root, edge[0]), find(root, edge[1]) | |
if (x == y): return edge |
This file contains 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 boolean validateStackSequences(int[] pushed, int[] popped) { | |
Stack<Integer> st = new Stack<>(); | |
int i = 0; | |
for (int num : pushed) { | |
st.push(num); | |
while (!st.isEmpty() && st.peek() == popped[i]) { | |
st.pop(); | |
++i; | |
} |
This file contains 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 validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: | |
st = [] | |
i = 0 | |
for num in pushed: | |
st.append(num) | |
while st and st[-1] == popped[i]: | |
st.pop() | |
i += 1 | |
return not st |
This file contains 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 shortestPathBinaryMatrix(int[][] grid) { | |
if (grid == null || grid[0] == null || grid[0][0] == 1 || grid[grid.length - 1][grid[0].length - 1] == 1) { | |
return -1; | |
} | |
int n = grid.length; | |
if (n == 1) return 1; | |
Queue<int[]> q = new LinkedList<>(); | |
q.add(new int[]{0, 0}); | |
int cnt = 1; |
This file contains 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 shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int: | |
if not grid or not grid[0] or grid[0][0] == 1 or grid[len(grid) - 1][len(grid[0]) - 1] == 1: | |
return -1 | |
r, c = len(grid), len(grid[0]) | |
if r == 1 and c == 1: return 1 | |
q = collections.deque([(0, 0)]) | |
cnt = 1 | |
d = [(di, dj) for di in range(-1, 2) for dj in range(-1, 2) if (di != 0 or dj != 0)] | |
grid[0][0] = 1 |
This file contains 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 boolean isPerfectSquare(int num) { | |
long x = num; | |
while (x * x > num) { | |
x = (x + num / x) / 2; | |
} | |
return x * x == num; | |
} | |
} |
This file contains 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 isPerfectSquare(self, num: int) -> bool: | |
x = num | |
while x * x > num: | |
x = (x + num // x) // 2 | |
return x * x == num |