Skip to content

Commit ee04f98

Browse files
committed
Add solution #1356
1 parent 4b8d346 commit ee04f98

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
1324|[Print Words Vertically](./1324-print-words-vertically.js)|Medium|
126126
1332|[Remove Palindromic Subsequences](./1332-remove-palindromic-subsequences.js)|Easy|
127127
1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](./1333-filter-restaurants-by-vegan-friendly-price-and-distance.js)|Medium|
128+
1356|[Sort Integers by The Number of 1 Bits](./1356-sort-integers-by-the-number-of-1-bits.js)|Easy|
128129
1360|[Number of Days Between Two Dates](./1360-number-of-days-between-two-dates.js)|Easy|
129130
1472|[Design Browser History](./1472-design-browser-history.js)|Medium|
130131
1598|[Crawler Log Folder](./1598-crawler-log-folder.js)|Easy|
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 1356. Sort Integers by The Number of 1 Bits
3+
* https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array arr. You have to sort the integers in the array in
7+
* ascending order by the number of 1's in their binary representation and
8+
* in case of two or more integers have the same number of 1's you have to
9+
* sort them in ascending order.
10+
*
11+
* Return the sorted array.
12+
*/
13+
14+
/**
15+
* @param {number[]} arr
16+
* @return {number[]}
17+
*/
18+
var sortByBits = function(arr) {
19+
const getCount = n => n.toString(2).replace(/0/g, '').length;
20+
21+
return arr.sort((a, b) => getCount(a) - getCount(b) || a - b);
22+
};

0 commit comments

Comments
 (0)