You are given anContinue reading “[Leetcode]48. Rotate Image”nxn2D`matrix`

representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the imagein-place, which means you have to modify the input 2D matrix directly.DO NOTallocate another 2D matrix and do the rotation.

## [Leetcode]1590. Make Sum Divisible by P

Given an array of positive integersContinue reading “[Leetcode]1590. Make Sum Divisible by P”`nums`

, remove thesmallestsubarray (possiblyempty) such that thesumof the remaining elements is divisible by`p`

. It isnotallowed to remove the whole array. Returnthe length of the smallest subarray that you need to remove, or`-1`

if it's impossible. Asubarrayis defined as a contiguous block of elements in the array.

## [Leetcode]31. Next Permutation

ImplementContinue reading “[Leetcode]31. Next Permutation”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 bein-placeand 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.`1,2,3`

→`1,3,2`

`3,2,1`

→`1,2,3`

`1,1,5`

→`1,5,1`

## [Leetcode]1513. Number of Substrings With Only 1s

Given a binary stringContinue 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.Example 1:Input:s = "0110111"Output:9Explanation:There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time.

## [Leetcode]1497. Check If Array Pairs Are Divisible by k

Given an array of integersContinue reading “[Leetcode]1497. Check If Array Pairs Are Divisible by k”`arr`

of even length`n`

and an integer`k`

. We want to divide the array into exactly`n / 2`

pairs such that the sum of each pair is divisible by`k`

. ReturnTrueIf you can find a way to do that orFalseotherwise.Example 1:Input:arr = [1,2,3,4,5,10,6,7,8,9], k = 5Output:trueExplanation:Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).