Skip to content

Commit fd9c8e5

Browse files
committed
feat: add solutions to lc problem: No.1680
No.1680.Concatenation of Consecutive Binary Numbers
1 parent 7f00e2f commit fd9c8e5

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,87 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:位运算**
50+
51+
观察数字的连接规律,我们可以发现,当连接到第 $i$ 个数时,实际上是将前 $i-1$ 个数连接而成的结果 $ans$ 往左移动一定的位数,然后再加上 $i$ 这个数,移动的位数 $shift$ 是 $i$ 中二进制的位数。由于 $i$ 在不断加 $1$,移动的位数要么与上一次移动的位数保持不变,要么加一。当 $i$ 为 $2$ 的幂次方的时候,也即是说 $i$ 的二进制数中只有一位是 $1$ 时,移动的位数相比于上次加 $1$。
52+
53+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**
5258

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

5561
```python
56-
62+
class Solution:
63+
def concatenatedBinary(self, n: int) -> int:
64+
mod = 10**9 + 7
65+
ans = shift = 0
66+
for i in range(1, n + 1):
67+
if (i & (i - 1)) == 0:
68+
shift += 1
69+
ans = ((ans << shift) + i) % mod
70+
return ans
5771
```
5872

5973
### **Java**
6074

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

6377
```java
78+
class Solution {
79+
private static final int MOD = (int) 1e9 + 7;
80+
81+
public int concatenatedBinary(int n) {
82+
long ans = 0;
83+
int shift = 0;
84+
for (int i = 1; i <= n; ++i) {
85+
if ((i & (i - 1)) == 0) {
86+
++shift;
87+
}
88+
ans = ((ans << shift) + i) % MOD;
89+
}
90+
return (int) ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
const int mod = 1e9 + 7;
101+
102+
int concatenatedBinary(int n) {
103+
long ans = 0;
104+
int shift = 0;
105+
for (int i = 1; i <= n; ++i) {
106+
if ((i & (i - 1)) == 0) {
107+
++shift;
108+
}
109+
ans = ((ans << shift) + i) % mod;
110+
}
111+
return ans;
112+
}
113+
};
114+
```
64115

116+
### **Go**
117+
118+
```go
119+
func concatenatedBinary(n int) int {
120+
var ans, shift int
121+
const mod int = 1e9 + 7
122+
for i := 1; i <= n; i++ {
123+
if i&(i-1) == 0 {
124+
shift++
125+
}
126+
ans = ((ans << shift) + i) % mod
127+
}
128+
return ans
129+
}
65130
```
66131

67132
### **...**

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

+60-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,72 @@ After modulo 10<sup>9</sup> + 7, the result is 505379714.
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def concatenatedBinary(self, n: int) -> int:
53+
mod = 10**9 + 7
54+
ans = shift = 0
55+
for i in range(1, n + 1):
56+
if (i & (i - 1)) == 0:
57+
shift += 1
58+
ans = ((ans << shift) + i) % mod
59+
return ans
5260
```
5361

5462
### **Java**
5563

5664
```java
65+
class Solution {
66+
private static final int MOD = (int) 1e9 + 7;
67+
68+
public int concatenatedBinary(int n) {
69+
long ans = 0;
70+
int shift = 0;
71+
for (int i = 1; i <= n; ++i) {
72+
if ((i & (i - 1)) == 0) {
73+
++shift;
74+
}
75+
ans = ((ans << shift) + i) % MOD;
76+
}
77+
return (int) ans;
78+
}
79+
}
80+
```
81+
82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
const int mod = 1e9 + 7;
88+
89+
int concatenatedBinary(int n) {
90+
long ans = 0;
91+
int shift = 0;
92+
for (int i = 1; i <= n; ++i) {
93+
if ((i & (i - 1)) == 0) {
94+
++shift;
95+
}
96+
ans = ((ans << shift) + i) % mod;
97+
}
98+
return ans;
99+
}
100+
};
101+
```
57102

103+
### **Go**
104+
105+
```go
106+
func concatenatedBinary(n int) int {
107+
var ans, shift int
108+
const mod int = 1e9 + 7
109+
for i := 1; i <= n; i++ {
110+
if i&(i-1) == 0 {
111+
shift++
112+
}
113+
ans = ((ans << shift) + i) % mod
114+
}
115+
return ans
116+
}
58117
```
59118

60119
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
const int mod = 1e9 + 7;
4+
5+
int concatenatedBinary(int n) {
6+
long ans = 0;
7+
int shift = 0;
8+
for (int i = 1; i <= n; ++i) {
9+
if ((i & (i - 1)) == 0) {
10+
++shift;
11+
}
12+
ans = ((ans << shift) + i) % mod;
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func concatenatedBinary(n int) int {
2+
var ans, shift int
3+
const mod int = 1e9 + 7
4+
for i := 1; i <= n; i++ {
5+
if i&(i-1) == 0 {
6+
shift++
7+
}
8+
ans = ((ans << shift) + i) % mod
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int concatenatedBinary(int n) {
5+
long ans = 0;
6+
int shift = 0;
7+
for (int i = 1; i <= n; ++i) {
8+
if ((i & (i - 1)) == 0) {
9+
++shift;
10+
}
11+
ans = ((ans << shift) + i) % MOD;
12+
}
13+
return (int) ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def concatenatedBinary(self, n: int) -> int:
3+
mod = 10**9 + 7
4+
ans = shift = 0
5+
for i in range(1, n + 1):
6+
if (i & (i - 1)) == 0:
7+
shift += 1
8+
ans = ((ans << shift) + i) % mod
9+
return ans

0 commit comments

Comments
 (0)