diff --git a/110-balanced-binary-tree/README.md b/110-balanced-binary-tree/README.md new file mode 100644 index 0000000000000..3e689bfce0357 --- /dev/null +++ b/110-balanced-binary-tree/README.md @@ -0,0 +1,31 @@ +
Given a binary tree, determine if it is height-balanced.
+ ++
Example 1:
++Input: root = [3,9,20,null,null,15,7] +Output: true ++ +
Example 2:
++Input: root = [1,2,2,3,3,null,null,4,4] +Output: false ++ +
Example 3:
+ ++Input: root = [] +Output: true ++ +
+
Constraints:
+ +[0, 5000]
.-104 <= Node.val <= 104
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 n
, return the value of Tn.
+
Example 1:
+ ++Input: n = 4 +Output: 4 +Explanation: +T_3 = 0 + 1 + 1 = 2 +T_4 = 1 + 1 + 2 = 4 ++ +
Example 2:
+ ++Input: n = 25 +Output: 1389537 ++ +
+
Constraints:
+ +0 <= n <= 37
answer <= 2^31 - 1
.A bus has n
stops numbered from 0
to n - 1
that form a circle. We know the distance between all pairs of neighboring stops where distance[i]
is the distance between the stops number i
and (i + 1) % n
.
The bus goes along both directions i.e. clockwise and counterclockwise.
+ +Return the shortest distance between the given start
and destination
stops.
+
Example 1:
+ ++Input: distance = [1,2,3,4], start = 0, destination = 1 +Output: 1 +Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.+ +
+ +
Example 2:
+ ++Input: distance = [1,2,3,4], start = 0, destination = 2 +Output: 3 +Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3. ++ +
+ +
Example 3:
+ ++Input: distance = [1,2,3,4], start = 0, destination = 3 +Output: 4 +Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4. ++ +
+
Constraints:
+ +1 <= n <= 10^4
distance.length == n
0 <= start, destination < n
0 <= distance[i] <= 10^4
Given an array of distinct integers arr
, find all pairs of elements with the minimum absolute difference of any two elements.
Return a list of pairs in ascending order(with respect to pairs), each pair [a, b]
follows
a, b
are from arr
a < b
b - a
equals to the minimum absolute difference of any two elements in arr
+
Example 1:
+ ++Input: arr = [4,2,1,3] +Output: [[1,2],[2,3],[3,4]] +Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.+ +
Example 2:
+ ++Input: arr = [1,3,6,10,15] +Output: [[1,3]] ++ +
Example 3:
+ ++Input: arr = [3,8,-10,23,19,-4,-14,27] +Output: [[-14,-10],[19,23],[23,27]] ++ +
+
Constraints:
+ +2 <= arr.length <= 105
-106 <= arr[i] <= 106
Given a 2D grid
of size m x n
and an integer k
. You need to shift the grid
k
times.
In one shift operation:
+ +grid[i][j]
moves to grid[i][j + 1]
.grid[i][n - 1]
moves to grid[i + 1][0]
.grid[m - 1][n - 1]
moves to grid[0][0]
.Return the 2D grid after applying shift operation k
times.
+
Example 1:
+
+Input: grid
= [[1,2,3],[4,5,6],[7,8,9]], k = 1
+Output: [[9,1,2],[3,4,5],[6,7,8]]
+
+
+Example 2:
+
+Input: grid
= [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
+Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
+
+
+Example 3:
+ +
+Input: grid
= [[1,2,3],[4,5,6],[7,8,9]], k = 9
+Output: [[1,2,3],[4,5,6],[7,8,9]]
+
+
++
Constraints:
+ +m == grid.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= k <= 100
Given a square matrix mat
, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
+ ++
Example 1:
++Input: mat = [[1,2,3], + [4,5,6], + [7,8,9]] +Output: 25 +Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25 +Notice that element mat[1][1] = 5 is counted only once. ++ +
Example 2:
+ ++Input: mat = [[1,1,1,1], + [1,1,1,1], + [1,1,1,1], + [1,1,1,1]] +Output: 8 ++ +
Example 3:
+ ++Input: mat = [[5]] +Output: 5 ++ +
+
Constraints:
+ +n == mat.length == mat[i].length
1 <= n <= 100
1 <= mat[i][j] <= 100
You are assigned to put some amount of boxes onto one truck. You are given a 2D array boxTypes
, where boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]
:
numberOfBoxesi
is the number of boxes of type i
.numberOfUnitsPerBoxi
is the number of units in each box of the type i
.You are also given an integer truckSize
, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize
.
Return the maximum total number of units that can be put on the truck.
+ ++
Example 1:
+ ++Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4 +Output: 8 +Explanation: There are: +- 1 box of the first type that contains 3 units. +- 2 boxes of the second type that contain 2 units each. +- 3 boxes of the third type that contain 1 unit each. +You can take all the boxes of the first and second types, and one box of the third type. +The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8. ++ +
Example 2:
+ ++Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10 +Output: 91 ++ +
+
Constraints:
+ +1 <= boxTypes.length <= 1000
1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000
1 <= truckSize <= 106
There is an undirected star graph consisting of n
nodes labeled from 1
to n
. A star graph is a graph where there is one center node and exactly n - 1
edges that connect the center node with every other node.
You are given a 2D integer array edges
where each edges[i] = [ui, vi]
indicates that there is an edge between the nodes ui
and vi
. Return the center of the given star graph.
+
Example 1:
++Input: edges = [[1,2],[2,3],[4,2]] +Output: 2 +Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center. ++ +
Example 2:
+ ++Input: edges = [[1,2],[5,1],[1,3],[1,4]] +Output: 1 ++ +
+
Constraints:
+ +3 <= n <= 105
edges.length == n - 1
edges[i].length == 2
1 <= ui, vi <= n
ui != vi
edges
represent a valid star graph.A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of only uppercase and lowercase English letters (no punctuation).
+ +"Hello World"
, "HELLO"
, and "hello world hello world"
are all sentences.You are given a sentence s
and an integer k
. You want to truncate s
such that it contains only the first k
words. Return s
after truncating it.
+
Example 1:
+ ++Input: s = "Hello how are you Contestant", k = 4 +Output: "Hello how are you" +Explanation: +The words in s are ["Hello", "how" "are", "you", "Contestant"]. +The first 4 words are ["Hello", "how", "are", "you"]. +Hence, you should return "Hello how are you". ++ +
Example 2:
+ ++Input: s = "What is the solution to this problem", k = 4 +Output: "What is the solution" +Explanation: +The words in s are ["What", "is" "the", "solution", "to", "this", "problem"]. +The first 4 words are ["What", "is", "the", "solution"]. +Hence, you should return "What is the solution".+ +
Example 3:
+ ++Input: s = "chopper is not a tanuki", k = 5 +Output: "chopper is not a tanuki" ++ +
+
Constraints:
+ +1 <= s.length <= 500
k
is in the range [1, the number of words in s]
.s
consist of only lowercase and uppercase English letters and spaces.s
are separated by a single space.Given an integer n
, return true
if n
has exactly three positive divisors. Otherwise, return false
.
An integer m
is a divisor of n
if there exists an integer k
such that n = k * m
.
+
Example 1:
+ ++Input: n = 2 +Output: false +Explantion: 2 has only two divisors: 1 and 2. ++ +
Example 2:
+ ++Input: n = 4 +Output: true +Explantion: 4 has three divisors: 1, 2, and 4. ++ +
+
Constraints:
+ +1 <= n <= 104
A shop is selling candies at a discount. For every two candies sold, the shop gives a third candy for free.
+ +The customer can choose any candy to take away for free as long as the cost of the chosen candy is less than or equal to the minimum cost of the two candies bought.
+ +4
candies with costs 1
, 2
, 3
, and 4
, and the customer buys candies with costs 2
and 3
, they can take the candy with cost 1
for free, but not the candy with cost 4
.Given a 0-indexed integer array cost
, where cost[i]
denotes the cost of the ith
candy, return the minimum cost of buying all the candies.
+
Example 1:
+ ++Input: cost = [1,2,3] +Output: 5 +Explanation: We buy the candies with costs 2 and 3, and take the candy with cost 1 for free. +The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies. +Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free. +The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies. ++ +
Example 2:
+ ++Input: cost = [6,5,7,9,2,2] +Output: 23 +Explanation: The way in which we can get the minimum cost is described below: +- Buy candies with costs 9 and 7 +- Take the candy with cost 6 for free +- We buy candies with costs 5 and 2 +- Take the last remaining candy with cost 2 for free +Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23. ++ +
Example 3:
+ ++Input: cost = [5,5] +Output: 10 +Explanation: Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free. +Hence, the minimum cost to buy all candies is 5 + 5 = 10. ++ +
+
Constraints:
+ +1 <= cost.length <= 100
1 <= cost[i] <= 100
You are given an array of integers nums
. You are also given an integer original
which is the first number that needs to be searched for in nums
.
You then do the following steps:
+ +original
is found in nums
, multiply it by two (i.e., set original = 2 * original
).Return the final value of original
.
+
Example 1:
+ ++Input: nums = [5,3,6,1,12], original = 3 +Output: 24 +Explanation: +- 3 is found in nums. 3 is multiplied by 2 to obtain 6. +- 6 is found in nums. 6 is multiplied by 2 to obtain 12. +- 12 is found in nums. 12 is multiplied by 2 to obtain 24. +- 24 is not found in nums. Thus, 24 is returned. ++ +
Example 2:
+ ++Input: nums = [2,7,9], original = 4 +Output: 4 +Explanation: +- 4 is not found in nums. Thus, 4 is returned. ++ +
+
Constraints:
+ +1 <= nums.length <= 1000
1 <= nums[i], original <= 1000
nums
of length n
and an integer k
, return the number of pairs (i, j)
where 0 <= i < j < n
, such that nums[i] == nums[j]
and (i * j)
is divisible by k
.
++
Example 1:
+ ++Input: nums = [3,1,2,2,2,1,3], k = 2 +Output: 4 +Explanation: +There are 4 pairs that meet all the requirements: +- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2. +- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2. +- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2. +- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2. ++ +
Example 2:
+ ++Input: nums = [1,2,3,4], k = 1 +Output: 0 +Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements. ++ +
+
Constraints:
+ +1 <= nums.length <= 100
1 <= nums[i], k <= 100
Given an integer n
, return true
if it is a power of two. Otherwise, return false
.
An integer n
is a power of two, if there exists an integer x
such that n == 2x
.
+
Example 1:
+ ++Input: n = 1 +Output: true +Explanation: 20 = 1 ++ +
Example 2:
+ ++Input: n = 16 +Output: true +Explanation: 24 = 16 ++ +
Example 3:
+ ++Input: n = 3 +Output: false ++ +
+
Constraints:
+ +-231 <= n <= 231 - 1
+Follow up: Could you solve it without loops/recursion? \ No newline at end of file diff --git a/231-power-of-two/power-of-two.cpp b/231-power-of-two/power-of-two.cpp new file mode 100644 index 0000000000000..9dd90bd7e2bbd --- /dev/null +++ b/231-power-of-two/power-of-two.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool isPowerOfTwo(int n) { + if(n == 0) return false; + if(n == 1) return true; + return (n%2==0)?isPowerOfTwo(n/2):false; + } +}; \ No newline at end of file diff --git a/2507-number-of-common-factors/README.md b/2507-number-of-common-factors/README.md new file mode 100644 index 0000000000000..e648382e2a825 --- /dev/null +++ b/2507-number-of-common-factors/README.md @@ -0,0 +1,27 @@ +
Given two positive integers a
and b
, return the number of common factors of a
and b
.
An integer x
is a common factor of a
and b
if x
divides both a
and b
.
+
Example 1:
+ ++Input: a = 12, b = 6 +Output: 4 +Explanation: The common factors of 12 and 6 are 1, 2, 3, 6. ++ +
Example 2:
+ ++Input: a = 25, b = 30 +Output: 2 +Explanation: The common factors of 25 and 30 are 1, 5. ++ +
+
Constraints:
+ +1 <= a, b <= 1000
You are given an m x n
matrix grid
consisting of positive integers.
Perform the following operation until grid
becomes empty:
Note that the number of columns decreases by one after each operation.
+ +Return the answer after performing the operations described above.
+ ++
Example 1:
++Input: grid = [[1,2,4],[3,3,1]] +Output: 8 +Explanation: The diagram above shows the removed values in each step. +- In the first operation, we remove 4 from the first row and 3 from the second row (notice that, there are two cells with value 3 and we can remove any of them). We add 4 to the answer. +- In the second operation, we remove 2 from the first row and 3 from the second row. We add 3 to the answer. +- In the third operation, we remove 1 from the first row and 1 from the second row. We add 1 to the answer. +The final answer = 4 + 3 + 1 = 8. ++ +
Example 2:
++Input: grid = [[10]] +Output: 10 +Explanation: The diagram above shows the removed values in each step. +- In the first operation, we remove 10 from the first row. We add 10 to the answer. +The final answer = 10. ++ +
+
Constraints:
+ +m == grid.length
n == grid[i].length
1 <= m, n <= 50
1 <= grid[i][j] <= 100
You are given a positive integer n
. Each digit of n
has a sign according to the following rules:
Return the sum of all digits with their corresponding sign.
+ ++
Example 1:
+ ++Input: n = 521 +Output: 4 +Explanation: (+5) + (-2) + (+1) = 4. ++ +
Example 2:
+ ++Input: n = 111 +Output: 1 +Explanation: (+1) + (-1) + (+1) = 1. ++ +
Example 3:
+ ++Input: n = 886996 +Output: 0 +Explanation: (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0. ++ +
+
Constraints:
+ +1 <= n <= 109
+ diff --git a/2630-alternating-digit-sum/alternating-digit-sum.cpp b/2630-alternating-digit-sum/alternating-digit-sum.cpp new file mode 100644 index 0000000000000..f3b2aa3649156 --- /dev/null +++ b/2630-alternating-digit-sum/alternating-digit-sum.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int alternateDigitSum(int n) { + string s = to_string(n); + int sum=0, fl=1; + for(int i=0; i
Given a m x n
binary matrix mat
, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row.
In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.
+ +Return an array containing the index of the row, and the number of ones in it.
+ ++
Example 1:
+ +
+Input: mat = [[0,1],[1,0]]
+Output: [0,1]
+Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1)
. So, the answer is [0,1].
+
+
+Example 2:
+ ++Input: mat = [[0,0,0],[0,1,1]] +Output: [1,2] +Explanation: The row indexed 1 has the maximum count of ones+ +(2)
. So we return its index,1
, and the count. So, the answer is [1,2]. +
Example 3:
+ ++Input: mat = [[0,0],[1,1],[0,0]] +Output: [1,2] +Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2]. ++ +
+
Constraints:
+ +m == mat.length
n == mat[i].length
1 <= m, n <= 100
mat[i][j]
is either 0
or 1
.You are given an integer array prices
representing the prices of various chocolates in a store. You are also given a single integer money
, which represents your initial amount of money.
You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.
+ +Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money
. Note that the leftover must be non-negative.
+
Example 1:
+ ++Input: prices = [1,2,2], money = 3 +Output: 0 +Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0. ++ +
Example 2:
+ ++Input: prices = [3,2,3], money = 3 +Output: 3 +Explanation: You cannot buy 2 chocolates without going in debt, so we return 3. ++ +
+
Constraints:
+ +2 <= prices.length <= 50
1 <= prices[i] <= 100
1 <= money <= 100
You are given a 0-indexed integer array nums
of even length and there is also an empty array arr
. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:
nums
, and then Bob does the same.arr
, and then Alice does the same.nums
becomes empty.Return the resulting array arr
.
+
Example 1:
+ ++Input: nums = [5,4,2,3] +Output: [3,2,5,4] +Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2]. +At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4]. ++ +
Example 2:
+ ++Input: nums = [2,5] +Output: [5,2] +Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2]. ++ +
+
Constraints:
+ +2 <= nums.length <= 100
1 <= nums[i] <= 100
nums.length % 2 == 0
Given an integer n
, return true
if it is a power of three. Otherwise, return false
.
An integer n
is a power of three, if there exists an integer x
such that n == 3x
.
+
Example 1:
+ ++Input: n = 27 +Output: true +Explanation: 27 = 33 ++ +
Example 2:
+ ++Input: n = 0 +Output: false +Explanation: There is no x where 3x = 0. ++ +
Example 3:
+ ++Input: n = -1 +Output: false +Explanation: There is no x where 3x = (-1). ++ +
+
Constraints:
+ +-231 <= n <= 231 - 1
+Follow up: Could you solve it without loops/recursion? \ No newline at end of file diff --git a/326-power-of-three/power-of-three.cpp b/326-power-of-three/power-of-three.cpp new file mode 100644 index 0000000000000..074936abf1613 --- /dev/null +++ b/326-power-of-three/power-of-three.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool isPowerOfThree(int n) { + if(n == 0) return false; + if(n == 1) return true; + return (n%3==0)?isPowerOfThree(n/3):false; + } +}; \ No newline at end of file diff --git a/3334-apple-redistribution-into-boxes/README.md b/3334-apple-redistribution-into-boxes/README.md new file mode 100644 index 0000000000000..a40d8ab07bd76 --- /dev/null +++ b/3334-apple-redistribution-into-boxes/README.md @@ -0,0 +1,35 @@ +
You are given an array apple
of size n
and an array capacity
of size m
.
There are n
packs where the ith
pack contains apple[i]
apples. There are m
boxes as well, and the ith
box has a capacity of capacity[i]
apples.
Return the minimum number of boxes you need to select to redistribute these n
packs of apples into boxes.
Note that, apples from the same pack can be distributed into different boxes.
+ ++
Example 1:
+ ++Input: apple = [1,3,2], capacity = [4,3,1,5,2] +Output: 2 +Explanation: We will use boxes with capacities 4 and 5. +It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples. ++ +
Example 2:
+ ++Input: apple = [5,5,5], capacity = [2,4,2,7] +Output: 4 +Explanation: We will need to use all the boxes. ++ +
+
Constraints:
+ +1 <= n == apple.length <= 50
1 <= m == capacity.length <= 50
1 <= apple[i], capacity[i] <= 50
Given an integer n
, return true
if it is a power of four. Otherwise, return false
.
An integer n
is a power of four, if there exists an integer x
such that n == 4x
.
+
Example 1:
+Input: n = 16 +Output: true +
Example 2:
+Input: n = 5 +Output: false +
Example 3:
+Input: n = 1 +Output: true ++
+
Constraints:
+ +-231 <= n <= 231 - 1
+Follow up: Could you solve it without loops/recursion? \ No newline at end of file diff --git a/342-power-of-four/power-of-four.cpp b/342-power-of-four/power-of-four.cpp new file mode 100644 index 0000000000000..65ace3e322c5d --- /dev/null +++ b/342-power-of-four/power-of-four.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + bool isPowerOfFour(int n) { + if(n == 0) return false; + if(n == 1) return true; + return (n%4==0)?isPowerOfFour(n/4):false; + + } +}; \ No newline at end of file diff --git a/3484-lexicographically-smallest-string-after-a-swap/README.md b/3484-lexicographically-smallest-string-after-a-swap/README.md new file mode 100644 index 0000000000000..8bbc6c0cfad86 --- /dev/null +++ b/3484-lexicographically-smallest-string-after-a-swap/README.md @@ -0,0 +1,36 @@ +
Given a string s
containing only digits, return the lexicographically smallest string that can be obtained after swapping adjacent digits in s
with the same parity at most once.
Digits have the same parity if both are odd or both are even. For example, 5 and 9, as well as 2 and 4, have the same parity, while 6 and 9 do not.
+ ++
Example 1:
+ +Input: s = "45320"
+ +Output: "43520"
+ +Explanation:
+ +s[1] == '5'
and s[2] == '3'
both have the same parity, and swapping them results in the lexicographically smallest string.
Example 2:
+ +Input: s = "001"
+ +Output: "001"
+ +Explanation:
+ +There is no need to perform a swap because s
is already the lexicographically smallest.
+
Constraints:
+ +2 <= s.length <= 100
s
consists only of digits.You are given a positive integer n
.
Return the maximum product of any two digits in n
.
Note: You may use the same digit twice if it appears more than once in n
.
+
Example 1:
+ +Input: n = 31
+ +Output: 3
+ +Explanation:
+ +n
are [3, 1]
.3 * 1 = 3
.Example 2:
+ +Input: n = 22
+ +Output: 4
+ +Explanation:
+ +n
are [2, 2]
.2 * 2 = 4
.Example 3:
+ +Input: n = 124
+ +Output: 8
+ +Explanation:
+ +n
are [1, 2, 4]
.1 * 2 = 2
, 1 * 4 = 4
, 2 * 4 = 8
.+
Constraints:
+ +10 <= n <= 109
The complement of an integer is the integer you get when you flip all the 0
's to 1
's and all the 1
's to 0
's in its binary representation.
5
is "101"
in binary and its complement is "010"
which is the integer 2
.Given an integer num
, return its complement.
+
Example 1:
+ ++Input: num = 5 +Output: 2 +Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. ++ +
Example 2:
+ ++Input: num = 1 +Output: 0 +Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. ++ +
+
Constraints:
+ +1 <= num < 231
+
Note: This question is the same as 1009: https://leetcode.com/problems/complement-of-base-10-integer/
diff --git a/476-number-complement/number-complement.cpp b/476-number-complement/number-complement.cpp new file mode 100644 index 0000000000000..e2c8c2c760f28 --- /dev/null +++ b/476-number-complement/number-complement.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int findComplement(int num) { + return (pow(2,floor(log2(num))+1)-1)-num; + } +}; \ No newline at end of file diff --git a/637-average-of-levels-in-binary-tree/README.md b/637-average-of-levels-in-binary-tree/README.md new file mode 100644 index 0000000000000..518e225e81dfb --- /dev/null +++ b/637-average-of-levels-in-binary-tree/README.md @@ -0,0 +1,25 @@ +root
of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5
of the actual answer will be accepted.
++
Example 1:
++Input: root = [3,9,20,null,null,15,7] +Output: [3.00000,14.50000,11.00000] +Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. +Hence return [3, 14.5, 11]. ++ +
Example 2:
++Input: root = [3,9,20,15,7] +Output: [3.00000,14.50000,11.00000] ++ +
+
Constraints:
+ +[1, 104]
.-231 <= Node.val <= 231 - 1
Given a signed 32-bit integer x
, return x
with its digits reversed. If reversing x
causes the value to go outside the signed 32-bit integer range [-231, 231 - 1]
, then return 0
.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
+ ++
Example 1:
+ ++Input: x = 123 +Output: 321 ++ +
Example 2:
+ ++Input: x = -123 +Output: -321 ++ +
Example 3:
+ ++Input: x = 120 +Output: 21 ++ +
+
Constraints:
+ +-231 <= x <= 231 - 1
Given an m x n
matrix
, return true
if the matrix is Toeplitz. Otherwise, return false
.
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
+ ++
Example 1:
++Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] +Output: true +Explanation: +In the above grid, the diagonals are: +"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". +In each diagonal all elements are the same, so the answer is True. ++ +
Example 2:
++Input: matrix = [[1,2],[2,2]] +Output: false +Explanation: +The diagonal "[1, 2]" has different elements. ++ +
+
Constraints:
+ +m == matrix.length
n == matrix[i].length
1 <= m, n <= 20
0 <= matrix[i][j] <= 99
+
Follow up:
+ +matrix
is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?matrix
is so large that you can only load up a partial row into the memory at once?Given an integer x
, return true
if x
is a palindrome, and false
otherwise.
+
Example 1:
+ ++Input: x = 121 +Output: true +Explanation: 121 reads as 121 from left to right and from right to left. ++ +
Example 2:
+ ++Input: x = -121 +Output: false +Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. ++ +
Example 3:
+ ++Input: x = 10 +Output: false +Explanation: Reads 01 from right to left. Therefore it is not a palindrome. ++ +
+
Constraints:
+ +-231 <= x <= 231 - 1
+Follow up: Could you solve it without converting the integer to a string? \ No newline at end of file diff --git a/9-palindrome-number/palindrome-number.cpp b/9-palindrome-number/palindrome-number.cpp new file mode 100644 index 0000000000000..5327f8a794c33 --- /dev/null +++ b/9-palindrome-number/palindrome-number.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool isPalindrome(int x) { + if(x<0) + return false; + long rev = 0; + int y=x; + while(y>0){ + rev=rev*10+y%10; + y/=10; + } + return rev == x; + } +}; \ No newline at end of file diff --git a/932-monotonic-array/README.md b/932-monotonic-array/README.md new file mode 100644 index 0000000000000..8d85bee36e3ec --- /dev/null +++ b/932-monotonic-array/README.md @@ -0,0 +1,35 @@ +
An array is monotonic if it is either monotone increasing or monotone decreasing.
+ +An array nums
is monotone increasing if for all i <= j
, nums[i] <= nums[j]
. An array nums
is monotone decreasing if for all i <= j
, nums[i] >= nums[j]
.
Given an integer array nums
, return true
if the given array is monotonic, or false
otherwise.
+
Example 1:
+ ++Input: nums = [1,2,2,3] +Output: true ++ +
Example 2:
+ ++Input: nums = [6,5,4,4] +Output: true ++ +
Example 3:
+ ++Input: nums = [1,3,2] +Output: false ++ +
+
Constraints:
+ +1 <= nums.length <= 105
-105 <= nums[i] <= 105