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

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