[LeetCode] Remove Element

27. Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
  • Return k.

Custom Judge:

The judge will test your solution with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
>int[] nums = [...]; // Input array
>int val = ...; // Value to remove
>int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.

>int k = removeElement(nums, val); // Calls your implementation

>assert k == expectedNums.length;
>sort(nums, 0, k); // Sort the first k elements of nums
>for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
>}

If all assertions pass, then your solution will be accepted.

Read more
[LeetCode] Valid Palindrome

125. Valid Palindrome

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false

Read more
[LeetCode] Count Substrings with Only One Distinct Letter

1180. Count Substrings with Only One Distinct Letter

Given a string s, return the number of substrings that have only one distinct letter.

Read more
[LeetCode] Armstrong Number

1134. Armstrong Number

Given an integer n, return true if and only if it is an Armstrong number.

The k-digit number n is an Armstrong number if and only if the kth power of each digit sums to n.

Read more
[LeetCode] Design Compressed String Iterator

604. Design Compressed String Iterator

Design and implement a data structure for a compressed string iterator. The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.

Implement the StringIterator class:

  • next()
  • hasNext() Returns true if there is any letter needs to be uncompressed in the original string, otherwise returns false.
Read more
[LeetCode] Missing Number In Arithmetic Progression

1228. Missing Number In Arithmetic Progression

In some array arr, the values were in arithmetic progression: the values arr[i + 1] - arr[i] are all equal for every 0 <= i < arr.length - 1.

A value from arr was removed that was not the first or last value in the array.

Given arr, return the removed value.

Read more
[LeetCode] High Five

1086. High Five

Given a list of the scores of different students, items, where items[i] = [IDi, scorei] represents one score from a student with IDi, calculate each student’s top five average.

Return the answer as an array of pairs result, where result[j] = [IDj, topFiveAveragej] represents the student with IDj and their top five average. Sort result by IDj in increasing order.

A student’s top five average is calculated by taking the sum of their top five scores and dividing it by 5 using integer division.

Read more
[LeetCode] Delete N Nodes After M Nodes of a Linked List

1474. Delete N Nodes After M Nodes of a Linked List

You are given the head of a linked list and two integers m and n.

Traverse the linked list and remove some nodes in the following way:

  • Start with the head as the current node.
  • Keep the first m nodes starting with the current node.
  • Remove the next n nodes
  • Keep repeating steps 2 and 3 until you reach the end of the list.

Return the head of the modified list after removing the mentioned nodes.

Read more
[LeetCode] Moving Average from Data Stream

346. Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Implement the MovingAverage class:

  • MovingAverage(int size) Initializes the object with the size of the window size.
  • double next(int val) Returns the moving average of the last size values of the stream.
Read more
[LeetCode] Meeting Rooms

252. Meeting Rooms

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

Read more