[Leetcode 138]: Copy List with Random Pointer

Question Leetcode 138: Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. Note: You must return the copy of the given head as a reference to the cloned list. Node definition: class Node: def __init__(self, val, next, random): self.val = val self.

[Leetcode 450] Delete Node in a BST

Question Leetcode 450: Delete Node in a BST Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the node is found, delete the node. Note: Time complexity should be O(height of tree).

[Leetcode 785] Is Graph Bipartite

Question Leetcode 785: Is Graph Bipartite? Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B. The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists.

[Leetcode 560] Subarray Sum Equals K

Question Leetcode 560: Subarray Sum Equals K Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. Examples Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Solution Method 1 (TLE) 我一开始想到的解法是使用一个存了dict的array,记录下每个位置上出现的所有可能的sum的数量。这样进行到每一位的时候只需要看一下他前面那个位置的dict,就能得到包含当前位置数字所有可能出现的sum。

[Leetcode 975] Odd Even Jump

Question Leetcode 975: Odd Even Jump You are given an integer array A. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, …) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, …) jumps in the series are called even numbered jumps. You may from index i jump forward to index j (with i < j) in the following way: