Given an , return all elements of the in spiral order.

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Given a string , you want to use the characters of to form as many instances of the word "balloon" as possible.

You can use each character in at most once. Return the maximum number of instances that can be formed.

Example 1:

Input: text = "nlaebolko"
Output: 1

Example 2:

Given the of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

You are given a string of lowercase English letters and an integer array of the same length.

Call the of a letter, the next letter in the alphabet, (wrapping around so that becomes ).

  • For example, , , and .

Now for each , we want to shift the first letters of , times.

Return the final string after all such shifts to s are applied.

Example 1:

Input: s = "abc", shifts = [3,5,9]
Output: "rpl"
Explanation: We start with "abc".
After shifting the first 1…

Given the of a singly linked list, reverse the list, and return the reversed list.

Example 1:

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Example 2:

Suppose an array of length sorted in ascending order is rotated between and times. For example, the array might become:

  • if it was rotated times.
  • if it was rotated times.

Notice that rotating an array 1 time results in the array .

Given the sorted rotated array of unique elements, return the minimum element of this array.

You must write an algorithm that runs in

Example 1:

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was…

You are given the of a binary tree where each node has a value or . Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is , then this could represent in binary, which is .

For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.

Return the sum of these numbers. The answer is guaranteed to fit in a 32-bits integer.

Example 1:

Input: root = [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) +…

The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given , return the value of Tn.

Example 1:

Input: n = 4
Output: 4
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4

Example 2:

Input: n = 25
Output: 1389537


  • The answer is guaranteed to fit within a 32-bit integer, ie. .


The Fibonacci numbers, commonly denoted form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from and . That is,

F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given , calculate .

Example 1:

Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.



Time Complexity: O(n)

One way to serialize a binary tree is to use preorder traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as .

For example, the above binary tree can be serialized to the string , where represents a null node.

Given a string of comma-separated values , return if it is a correct preorder traversal serialization of a binary tree.

It is guaranteed that each comma-separated value in the string must be either an integer or a character representing null pointer.

Steven Lu

Software Engineer 🖥

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store