Skip to content

Commit dc62cc2

Browse files
committed
feat: add solutions to lc problem: No.1680
No.1680.Concatenation of Consecutive Binary Numbers
1 parent 4792e0a commit dc62cc2

File tree

7 files changed

+167
-33
lines changed

7 files changed

+167
-33
lines changed

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/README.md

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@
5050

5151
观察数字的连接规律,我们可以发现,当连接到第 $i$ 个数时,实际上是将前 $i-1$ 个数连接而成的结果 $ans$ 往左移动一定的位数,然后再加上 $i$ 这个数,移动的位数 $shift$ 是 $i$ 中二进制的位数。由于 $i$ 在不断加 $1$,移动的位数要么与上一次移动的位数保持不变,要么加一。当 $i$ 为 $2$ 的幂次方的时候,也即是说 $i$ 的二进制数中只有一位是 $1$ 时,移动的位数相比于上次加 $1$。
5252

53-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
53+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为给定的整数。
5454

5555
<!-- tabs:start -->
5656

5757
### **Python3**
5858

5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

61+
```python
62+
class Solution:
63+
def concatenatedBinary(self, n: int) -> int:
64+
mod = 10**9 + 7
65+
ans = 0
66+
for i in range(1, n + 1):
67+
ans = (ans << i.bit_length() | i) % mod
68+
return ans
69+
```
70+
6171
```python
6272
class Solution:
6373
def concatenatedBinary(self, n: int) -> int:
@@ -66,7 +76,7 @@ class Solution:
6676
for i in range(1, n + 1):
6777
if (i & (i - 1)) == 0:
6878
shift += 1
69-
ans = ((ans << shift) + i) % mod
79+
ans = (ans << shift | i) % mod
7080
return ans
7181
```
7282

@@ -76,16 +86,28 @@ class Solution:
7686

7787
```java
7888
class Solution {
79-
private static final int MOD = (int) 1e9 + 7;
89+
public int concatenatedBinary(int n) {
90+
final int mod = (int) 1e9 + 7;
91+
long ans = 0;
92+
for (int i = 1; i <= n; ++i) {
93+
ans = (ans << (32 - Integer.numberOfLeadingZeros(i)) | i) % mod;
94+
}
95+
return (int) ans;
96+
}
97+
}
98+
```
8099

100+
```java
101+
class Solution {
81102
public int concatenatedBinary(int n) {
103+
final int mod = (int) 1e9 + 7;
82104
long ans = 0;
83105
int shift = 0;
84106
for (int i = 1; i <= n; ++i) {
85107
if ((i & (i - 1)) == 0) {
86108
++shift;
87109
}
88-
ans = ((ans << shift) + i) % MOD;
110+
ans = (ans << shift | i) % mod;
89111
}
90112
return (int) ans;
91113
}
@@ -97,16 +119,29 @@ class Solution {
97119
```cpp
98120
class Solution {
99121
public:
100-
const int mod = 1e9 + 7;
122+
int concatenatedBinary(int n) {
123+
const int mod = 1e9 + 7;
124+
long ans = 0;
125+
for (int i = 1; i <= n; ++i) {
126+
ans = (ans << (32 - __builtin_clz(i)) | i) % mod;
127+
}
128+
return ans;
129+
}
130+
};
131+
```
101132
133+
```cpp
134+
class Solution {
135+
public:
102136
int concatenatedBinary(int n) {
137+
const int mod = 1e9 + 7;
103138
long ans = 0;
104139
int shift = 0;
105140
for (int i = 1; i <= n; ++i) {
106141
if ((i & (i - 1)) == 0) {
107142
++shift;
108143
}
109-
ans = ((ans << shift) + i) % mod;
144+
ans = (ans << shift | i) % mod;
110145
}
111146
return ans;
112147
}
@@ -116,16 +151,43 @@ public:
116151
### **Go**
117152

118153
```go
119-
func concatenatedBinary(n int) int {
120-
var ans, shift int
121-
const mod int = 1e9 + 7
154+
func concatenatedBinary(n int) (ans int) {
155+
const mod = 1e9 + 7
156+
for i := 1; i <= n; i++ {
157+
ans = (ans<<bits.Len(uint(i)) | i) % mod
158+
}
159+
return
160+
}
161+
```
162+
163+
```go
164+
func concatenatedBinary(n int) (ans int) {
165+
const mod = 1e9 + 7
166+
shift := 0
122167
for i := 1; i <= n; i++ {
123168
if i&(i-1) == 0 {
124169
shift++
125170
}
126-
ans = ((ans << shift) + i) % mod
171+
ans = (ans<<shift | i) % mod
127172
}
128-
return ans
173+
return
174+
}
175+
```
176+
177+
### **TypeScript**
178+
179+
```ts
180+
function concatenatedBinary(n: number): number {
181+
const mod = BigInt(10 ** 9 + 7);
182+
let ans = 0n;
183+
let shift = 0n;
184+
for (let i = 1n; i <= n; ++i) {
185+
if ((i & (i - 1n)) == 0n) {
186+
++shift;
187+
}
188+
ans = ((ans << shift) | i) % mod;
189+
}
190+
return Number(ans);
129191
}
130192
```
131193

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/README_EN.md

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ After modulo 10<sup>9</sup> + 7, the result is 505379714.
4747

4848
### **Python3**
4949

50+
```python
51+
class Solution:
52+
def concatenatedBinary(self, n: int) -> int:
53+
mod = 10**9 + 7
54+
ans = 0
55+
for i in range(1, n + 1):
56+
ans = (ans << i.bit_length() | i) % mod
57+
return ans
58+
```
59+
5060
```python
5161
class Solution:
5262
def concatenatedBinary(self, n: int) -> int:
@@ -55,24 +65,36 @@ class Solution:
5565
for i in range(1, n + 1):
5666
if (i & (i - 1)) == 0:
5767
shift += 1
58-
ans = ((ans << shift) + i) % mod
68+
ans = (ans << shift | i) % mod
5969
return ans
6070
```
6171

6272
### **Java**
6373

6474
```java
6575
class Solution {
66-
private static final int MOD = (int) 1e9 + 7;
76+
public int concatenatedBinary(int n) {
77+
final int mod = (int) 1e9 + 7;
78+
long ans = 0;
79+
for (int i = 1; i <= n; ++i) {
80+
ans = (ans << (32 - Integer.numberOfLeadingZeros(i)) | i) % mod;
81+
}
82+
return (int) ans;
83+
}
84+
}
85+
```
6786

87+
```java
88+
class Solution {
6889
public int concatenatedBinary(int n) {
90+
final int mod = (int) 1e9 + 7;
6991
long ans = 0;
7092
int shift = 0;
7193
for (int i = 1; i <= n; ++i) {
7294
if ((i & (i - 1)) == 0) {
7395
++shift;
7496
}
75-
ans = ((ans << shift) + i) % MOD;
97+
ans = (ans << shift | i) % mod;
7698
}
7799
return (int) ans;
78100
}
@@ -84,16 +106,29 @@ class Solution {
84106
```cpp
85107
class Solution {
86108
public:
87-
const int mod = 1e9 + 7;
109+
int concatenatedBinary(int n) {
110+
const int mod = 1e9 + 7;
111+
long ans = 0;
112+
for (int i = 1; i <= n; ++i) {
113+
ans = (ans << (32 - __builtin_clz(i)) | i) % mod;
114+
}
115+
return ans;
116+
}
117+
};
118+
```
88119
120+
```cpp
121+
class Solution {
122+
public:
89123
int concatenatedBinary(int n) {
124+
const int mod = 1e9 + 7;
90125
long ans = 0;
91126
int shift = 0;
92127
for (int i = 1; i <= n; ++i) {
93128
if ((i & (i - 1)) == 0) {
94129
++shift;
95130
}
96-
ans = ((ans << shift) + i) % mod;
131+
ans = (ans << shift | i) % mod;
97132
}
98133
return ans;
99134
}
@@ -103,16 +138,43 @@ public:
103138
### **Go**
104139

105140
```go
106-
func concatenatedBinary(n int) int {
107-
var ans, shift int
108-
const mod int = 1e9 + 7
141+
func concatenatedBinary(n int) (ans int) {
142+
const mod = 1e9 + 7
143+
for i := 1; i <= n; i++ {
144+
ans = (ans<<bits.Len(uint(i)) | i) % mod
145+
}
146+
return
147+
}
148+
```
149+
150+
```go
151+
func concatenatedBinary(n int) (ans int) {
152+
const mod = 1e9 + 7
153+
shift := 0
109154
for i := 1; i <= n; i++ {
110155
if i&(i-1) == 0 {
111156
shift++
112157
}
113-
ans = ((ans << shift) + i) % mod
158+
ans = (ans<<shift | i) % mod
114159
}
115-
return ans
160+
return
161+
}
162+
```
163+
164+
### **TypeScript**
165+
166+
```ts
167+
function concatenatedBinary(n: number): number {
168+
const mod = BigInt(10 ** 9 + 7);
169+
let ans = 0n;
170+
let shift = 0n;
171+
for (let i = 1n; i <= n; ++i) {
172+
if ((i & (i - 1n)) == 0n) {
173+
++shift;
174+
}
175+
ans = ((ans << shift) | i) % mod;
176+
}
177+
return Number(ans);
116178
}
117179
```
118180

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/Solution.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
class Solution {
22
public:
3-
const int mod = 1e9 + 7;
4-
53
int concatenatedBinary(int n) {
4+
const int mod = 1e9 + 7;
65
long ans = 0;
76
int shift = 0;
87
for (int i = 1; i <= n; ++i) {
98
if ((i & (i - 1)) == 0) {
109
++shift;
1110
}
12-
ans = ((ans << shift) + i) % mod;
11+
ans = (ans << shift | i) % mod;
1312
}
1413
return ans;
1514
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
func concatenatedBinary(n int) int {
2-
var ans, shift int
3-
const mod int = 1e9 + 7
1+
func concatenatedBinary(n int) (ans int) {
2+
const mod = 1e9 + 7
3+
shift := 0
44
for i := 1; i <= n; i++ {
55
if i&(i-1) == 0 {
66
shift++
77
}
8-
ans = ((ans << shift) + i) % mod
8+
ans = (ans<<shift | i) % mod
99
}
10-
return ans
10+
return
1111
}

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/Solution.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
2-
private static final int MOD = (int) 1e9 + 7;
3-
42
public int concatenatedBinary(int n) {
3+
final int mod = (int) 1e9 + 7;
54
long ans = 0;
65
int shift = 0;
76
for (int i = 1; i <= n; ++i) {
87
if ((i & (i - 1)) == 0) {
98
++shift;
109
}
11-
ans = ((ans << shift) + i) % MOD;
10+
ans = (ans << shift | i) % mod;
1211
}
1312
return (int) ans;
1413
}

solution/1600-1699/1680.Concatenation of Consecutive Binary Numbers/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ def concatenatedBinary(self, n: int) -> int:
55
for i in range(1, n + 1):
66
if (i & (i - 1)) == 0:
77
shift += 1
8-
ans = ((ans << shift) + i) % mod
8+
ans = (ans << shift | i) % mod
99
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function concatenatedBinary(n: number): number {
2+
const mod = BigInt(10 ** 9 + 7);
3+
let ans = 0n;
4+
let shift = 0n;
5+
for (let i = 1n; i <= n; ++i) {
6+
if ((i & (i - 1n)) == 0n) {
7+
++shift;
8+
}
9+
ans = ((ans << shift) | i) % mod;
10+
}
11+
return Number(ans);
12+
}

0 commit comments

Comments
 (0)