You are given an n x n 2D
Continue reading “[Leetcode]48. Rotate Image”
matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Given an array of positive integers
Continue reading “[Leetcode]1590. Make Sum Divisible by P”
nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by
p. It is not allowed to remove the whole array.
Return the length of the smallest subarray that you need to remove, or
-1 if it's impossible.
A subarray is defined as a contiguous block of elements in the array.
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
Continue reading “[Leetcode]31. Next Permutation”
Given a binary string
Continue reading “[Leetcode]1513. Number of Substrings With Only 1s”
s (a string consisting only of '0' and '1's).
Return the number of substrings with all characters 1's.
Since the answer may be too large, return it modulo 10^9 + 7.
Input: s = "0110111" Output: 9 Explanation: There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time.
Given an array of integers
Continue reading “[Leetcode]1497. Check If Array Pairs Are Divisible by k”
arr of even length
n and an integer
We want to divide the array into exactly
n / 2 pairs such that the sum of each pair is divisible by
Return True If you can find a way to do that or False otherwise.
Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 Output: true
Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).