Skip to content

Instantly share code, notes, and snippets.

@Desolve
Desolve / 0814 Binary Tree Pruning.py
Created July 24, 2021 05:55
0814 Binary Tree Pruning
# 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
@Desolve
Desolve / 0814 Binary Tree Pruning.java
Created July 24, 2021 05:55
0814 Binary Tree Pruning
/**
* 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;
@Desolve
Desolve / 0684 Redundant Connection.java
Created July 4, 2021 05:52
0684 Redundant Connection
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[]{};
@Desolve
Desolve / 0684 Redundant Connection.py
Created July 4, 2021 05:52
0684 Redundant Connection
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
@Desolve
Desolve / 0946 Validate Stack Sequences.java
Created February 27, 2021 12:02
0946 Validate Stack Sequences
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;
}
@Desolve
Desolve / 0946 Validate Stack Sequences.py
Created February 27, 2021 12:02
0946 Validate Stack Sequences
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
@Desolve
Desolve / 1091 Shortest Path in Binary Matrix.java
Created February 16, 2021 14:47
1091 Shortest Path in Binary Matrix
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;
@Desolve
Desolve / 1091 Shortest Path in Binary Matrix.py
Created February 16, 2021 14:47
1091 Shortest Path in Binary Matrix
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
@Desolve
Desolve / 0367 Valid Perfect Square.java
Created January 31, 2021 15:59
0367 Valid Perfect Square
class Solution {
public boolean isPerfectSquare(int num) {
long x = num;
while (x * x > num) {
x = (x + num / x) / 2;
}
return x * x == num;
}
}
@Desolve
Desolve / 0367 Valid Perfect Square.py
Created January 31, 2021 15:59
0367 Valid Perfect Square
class Solution:
def isPerfectSquare(self, num: int) -> bool:
x = num
while x * x > num:
x = (x + num // x) // 2
return x * x == num