python3 40

[Coderust] Move All Zeros to the Beginning of the Array

Problem Solution class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ # z keeps track of latest 0 encountered, i keeps track first of nonzero after z z = 0 for i in range(len(nums)): if nums[i] != 0: nums[i], nums[z] = nums[z], nums[i] z += 1 # next element is always zero since i checks for nonzero elements Time Complexity O..

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal

Problem Solution First element of preorder will always be the root of the tree. Inorder traversal searches left tree and then right tree, so we can find the root element in the list and divide the list up into left and right side(trees). Now we can build those trees. Repeat this process with the root of left tree, which is the second element in the preorder list. Then use the third element as th..