**Stack** and **queue** are dynamic data structures, both support delete operation.

# Month: July 2019

Source URL: https://leetcode.com/problems/relative-sort-array/

Given two arrays `arr1`

and `arr2`

, the elements of `arr2`

are distinct, and all elements in `arr2`

are also in `arr1`

.

Sort the elements of `arr1`

such that the relative ordering of items in `arr1`

are the same as in `arr2`

. Elements that don’t appear in `arr2`

should be placed at the end of `arr1`

in **ascending** order.

**Example 1:**

Input:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]Output:[2,2,2,1,4,3,3,9,6,7,19]

**Constraints:**

`arr1.length, arr2.length <= 1000`

`0 <= arr1[i], arr2[i] <= 1000`

- Each
`arr2[i]`

is distinct. - Each
`arr2[i]`

is in`arr1`

.

Source URL: https://leetcode.com/problems/k-closest-points-to-origin/

We have a list of `points`

on the plane. Find the `K`

closest points to the origin `(0, 0)`

.

(Here, the distance between two points on a plane is the Euclidean distance.)

You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)

**Example 1:**

Input:points = [[1,3],[-2,2]], K = 1Output:[[-2,2]]Explanation:The distance between (1, 3) and the origin is sqrt(10). The distance between (-2, 2) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].

**Example 2:**

Input:points = [[3,3],[5,-1],[-2,4]], K = 2Output:[[3,3],[-2,4]] (The answer [[-2,4],[3,3]] would also be accepted.)

**Note:**

`1 <= K <= points.length <= 10000`

`-10000 < points[i][0] < 10000`

`-10000 < points[i][1] < 10000`

Source URL: https://leetcode.com/problems/sort-array-by-parity-ii/

Given an array `A`

of non-negative integers, half of the integers in A are odd, and half of the integers are even.

Sort the array so that whenever `A[i]`

is odd, `i`

is odd; and whenever `A[i]`

is even, `i`

is even.

You may return any answer array that satisfies this condition.

**Example 1:**

Input:[4,2,5,7]Output:[4,5,2,7]Explanation:[4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Source URL: https://leetcode.com/problems/intersection-of-two-arrays-ii/

Given two arrays, write a function to compute their intersection.

**Example 1:**

Input:nums1 = [1,2,2,1], nums2 = [2,2]Output:[2,2]

**Example 2:**

Input:nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output:[4,9]

**Note:**

- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.

**Follow up:**

- What if the given array is already sorted? How would you optimize your algorithm?
- What if
*nums1*‘s size is small compared to*nums2*‘s size? Which algorithm is better? - What if elements of
*nums2*are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

Source address: https://leetcode.com/problems/intersection-of-two-arrays/

Given two arrays, write a function to compute their intersection.

**Example 1:**

Input:nums1 = [1,2,2,1], nums2 = [2,2]Output:[2]

**Example 2:**

Input:nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output:[9,4]

**Note:**

- Each element in the result must be unique.
- The result can be in any order.

## LeetCode 242 Valid Anagram [sort] java

Source URL: https://leetcode.com/problems/valid-anagram/

Given two strings *s* and *t *, write a function to determine if *t* is an anagram of *s*.

**Example 1:**

Input:s= "anagram",t= "nagaram"Output:true

**Example 2:**

Input:s= "rat",t= "car"Output:false

**Note:**

You may assume the string contains only lowercase alphabets.

**Follow up:**

What if the inputs contain unicode characters? How would you adapt your solution to such case?

## LeetCode 179 Largest Number [sort] java

Source URL: https://leetcode.com/problems/largest-number/

Given a list of non negative integers, arrange them such that they form the largest number.

**Example 1:**

Input:`[10,2]`

Output:"`210"`

**Example 2:**

Input:`[3,30,34,5,9]`

Output:"`9534330"`

**Note:** The result may be very large, so you need to return a string instead of an integer.

## LeetCode 164 Maximum Gap [sort] java

Source address: https://leetcode.com/problems/maximum-gap/

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Return 0 if the array contains less than 2 elements.

**Example 1:**

Input:[3,6,9,1]Output:3Explanation:The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.

**Example 2:**

Input:[10]Output:0Explanation:The array contains less than 2 elements, therefore return 0.

Source URL: https://leetcode.com/problems/insertion-sort-list/

Sort a linked list using insertion sort.

A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.

With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

**Algorithm of Insertion Sort:**

- Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
- At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
- It repeats until no input elements remain.

**Example 1:**

Input:4->2->1->3Output:1->2->3->4

**Example 2:**

Input:-1->5->3->4->0Output:-1->0->3->4->5