Path Sum II

Title: Path Sum II Source: leetcode.com Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. sum = 22 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 1234567               5             / \            4   8           /   / \          11  13  4         ...

Partition List

Title: Partition List Source: leetcode.com Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3->2->5->2 and x = 3, ...

Nim Game

Title: Nim Game Source: leetcode.com You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to ...

Letter Combinations of a Phone Number

Title: Letter Combinations of a Phone Number Source: leetcode.com Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. image source: leetcode.com Input: Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. ...

Linked List Cycle II

Title: Linked List Cycle II Source: leetcode.com Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve it without using extra space? Python solution Python ''' https://leetcode.com/problems/linked-list-cycle-ii/ ''' # Definition for singly-linked list. # class ...

H-Index

Title: H-Index Source: leetcode.com Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index. According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N ...

Gas Station

Title: Gas Station Source: leetcode.com There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an ...

Game of Life

Title: Game of Life Source: leetcode.com According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.” Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell ...

Flatten Binary Tree to Linked List

Title: Flatten Binary Tree to Linked List Source: leetcode.com Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 12345          1        / \       2   5      / \   \     3   4   6 1 \ 2 \ 3 \ 4 \ 5 \ ...

Evaluate Reverse Polish Notation

Title: Evaluate Reverse Polish Notation Source: leetcode.com Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> ...