Sum Root to Leaf Numbers 1

Title: Sum Root to Leaf Numbers Source: leetcode.com Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / \ 2 3 123     1   / \  2   ...

Simplify Path

Title: Simplify Path Source: leetcode.com Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where path = "/../"? In this case, you should return "/". Another corner case is the ...

Reverse Linked List II

Title: Reverse Linked List II Source: leetcode.com Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. Python solution Python ...

Remove Duplicates from Sorted List II

Title: Remove Duplicates from Sorted List II Source: leetcode.com Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3. Python solution Python ''' https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ ''' # Definition for singly-linked list. # class ListNode(object): # def ...

Range Sum Query 2D – Immutable

Title: Range Sum Query 2D – Immutable Source: leetcode.com Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) ...

Range Sum Query – Immutable

Title: Range Sum Query – Immutable Source: leetcode.com Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 12345 Given nums = [-2, ...

Permutations

Title: Permutations Source: leetcode.com Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. Python solution Python ''' https://leetcode.com/problems/permutations/ ''' class Solution(object): def __init__(self): self.lst = [] def helper(self, curr, rem): if not rem: self.lst.append(curr) else: ln = len(rem) for i in ...

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 ...