add new solutions
This commit is contained in:
parent
0f5f9e331c
commit
a7ff717b7a
7 changed files with 221 additions and 0 deletions
26
solutions/13/q1356/solution.go
Normal file
26
solutions/13/q1356/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q1356
|
||||
|
||||
import "slices"
|
||||
|
||||
func count1s(n int) int {
|
||||
c := 0
|
||||
for n > 0 {
|
||||
c++
|
||||
n = n & (n - 1)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func sortByBits(arr []int) []int {
|
||||
for i := range arr {
|
||||
arr[i] += count1s(arr[i]) << 16
|
||||
}
|
||||
slices.Sort(arr)
|
||||
|
||||
for i := range arr {
|
||||
arr[i] &= 1<<16 - 1
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
var _ = sortByBits
|
||||
Loading…
Add table
Add a link
Reference in a new issue