From 0e58f812796db7c03f6de970d4f45ed58b0d8c5c Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Fri, 10 Dec 2021 17:08:12 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.1356 1356.Sort Integers by The Number of 1 Bits --- .../README.md | 19 +++++++++++++++++-- .../README_EN.md | 19 +++++++++++++++++-- .../Solution.java | 13 +++++++++++++ .../Solution.py | 4 ++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java create mode 100644 solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.py diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md index d5408ae9809d9..03005f4abbb4b 100644 --- a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md @@ -71,7 +71,10 @@ ```python - +class Solution: + def sortByBits(self, arr: List[int]) -> List[int]: + arr.sort(key=lambda x: (bin(x).count("1"), x)) + return arr ``` ### **Java** @@ -79,7 +82,19 @@ ```java - +class Solution { + public int[] sortByBits(int[] arr) { + int n = arr.length; + for (int i = 0; i < n; i++) { + arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i]; + } + Arrays.sort(arr); + for (int i = 0; i < n; i++) { + arr[i] %= 100000; + } + return arr; + } +} ``` ### **...** diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md index 47f4e416d0c6b..7480bb9d291c4 100644 --- a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md @@ -66,13 +66,28 @@ The sorted array by bits is [0,1,2,4,8,3,5,6,7] ### **Python3** ```python - +class Solution: + def sortByBits(self, arr: List[int]) -> List[int]: + arr.sort(key=lambda x: (bin(x).count("1"), x)) + return arr ``` ### **Java** ```java - +class Solution { + public int[] sortByBits(int[] arr) { + int n = arr.length; + for (int i = 0; i < n; i++) { + arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i]; + } + Arrays.sort(arr); + for (int i = 0; i < n; i++) { + arr[i] %= 100000; + } + return arr; + } +} ``` ### **...** diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java new file mode 100644 index 0000000000000..40b95462b1cba --- /dev/null +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int[] sortByBits(int[] arr) { + int n = arr.length; + for (int i = 0; i < n; i++) { + arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i]; + } + Arrays.sort(arr); + for (int i = 0; i < n; i++) { + arr[i] %= 100000; + } + return arr; + } +} \ No newline at end of file diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.py b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.py new file mode 100644 index 0000000000000..09df16ec4b81a --- /dev/null +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def sortByBits(self, arr: List[int]) -> List[int]: + arr.sort(key=lambda x: (bin(x).count("1"), x)) + return arr \ No newline at end of file From ba8feb95695a9b8d1bde4fb4bd89f62976ac019e Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Fri, 10 Dec 2021 17:16:59 +0800 Subject: [PATCH 2/2] feat: add solutions to lc problem: No.1356 1356.Sort Integers by The Number of 1 Bits --- .../1356.Sort Integers by The Number of 1 Bits/README.md | 2 +- .../1356.Sort Integers by The Number of 1 Bits/README_EN.md | 2 +- .../1356.Sort Integers by The Number of 1 Bits/Solution.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md index 03005f4abbb4b..d76480bf8b462 100644 --- a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md @@ -86,7 +86,7 @@ class Solution { public int[] sortByBits(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { - arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i]; + arr[i] += Integer.bitCount(arr[i]) * 100000; } Arrays.sort(arr); for (int i = 0; i < n; i++) { diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md index 7480bb9d291c4..aa9a8e6cec868 100644 --- a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md @@ -79,7 +79,7 @@ class Solution { public int[] sortByBits(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { - arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i]; + arr[i] += Integer.bitCount(arr[i]) * 100000; } Arrays.sort(arr); for (int i = 0; i < n; i++) { diff --git a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java index 40b95462b1cba..0fd703bf45a77 100644 --- a/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java +++ b/solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/Solution.java @@ -2,7 +2,7 @@ class Solution { public int[] sortByBits(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { - arr[i] = Integer.bitCount(arr[i]) * 100000 + arr[i]; + arr[i] += Integer.bitCount(arr[i]) * 100000; } Arrays.sort(arr); for (int i = 0; i < n; i++) {