Skip to content

Instantly share code, notes, and snippets.

@CollinShoop
CollinShoop / Cracking the Coding Interview: URLIfy (solution)
Last active February 13, 2022 22:56
Cracking the Coding Interview: URLIfy (solution)
func URLify(input []rune, l int) {
i := len(input)-1 // insert pointer, start as the last index of the input array
l-- // reading pointer, start at the end of the input string
for l >= 0 {
if input[l] == ' ' {
input[i-2] = '%'
input[i-1] = '2'
input[i] = '0'
i -= 3
} else {
@CollinShoop
CollinShoop / Cracking the Coding Interview: URLIfy (unsolved)
Last active February 13, 2022 22:54
Cracking the Coding Interview: URLIfy (unsolved)
package main
import "fmt"
/**
URLIfy: Write a method to replace all spaces in a string with '%20'. You may assume that the string
has sufficient space at the end to hold the additional characters, and that you are given the "true" length
of the string.
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findTarget(root *TreeNode, k int) bool {
vals := map[int]bool{}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidBST(root *TreeNode) bool {
if root == nil {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func hasPathSum(root *TreeNode, targetSum int) bool {
var helper func(root *TreeNode, sum int) bool
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSymmetric(root *TreeNode) bool {
if root == nil {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {