Skip to content

Commit a06d77d

Browse files
committed
feat: add solutions to lc problem: No.0868
No.0868.Binary Gap
1 parent bdefe39 commit a06d77d

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

solution/0800-0899/0868.Binary Gap/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,48 @@
7474

7575
```
7676

77+
### **TypeScript**
78+
79+
```ts
80+
function binaryGap(n: number): number {
81+
let res = 0;
82+
let j = -1;
83+
for (let i = 0; n !== 0; i++) {
84+
if (n & 1) {
85+
if (j !== -1) {
86+
res = Math.max(res, i - j);
87+
}
88+
j = i;
89+
}
90+
n >>= 1;
91+
}
92+
return res;
93+
}
94+
```
95+
96+
### **Rust**
97+
98+
```rust
99+
impl Solution {
100+
pub fn binary_gap(mut n: i32) -> i32 {
101+
let mut res = 0;
102+
let mut i = 0;
103+
let mut j = -1;
104+
while n != 0 {
105+
if n & 1 == 1 {
106+
if j != -1 {
107+
res = res.max(i - j);
108+
}
109+
j = i;
110+
}
111+
n >>= 1;
112+
i += 1;
113+
}
114+
res
115+
}
116+
}
117+
```
118+
77119
### **...**
78120

79121
```

solution/0800-0899/0868.Binary Gap/README_EN.md

+42
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,48 @@ There are not any adjacent pairs of 1's in the binary representation of 8, s
6161

6262
```
6363

64+
### **TypeScript**
65+
66+
```ts
67+
function binaryGap(n: number): number {
68+
let res = 0;
69+
let j = -1;
70+
for (let i = 0; n !== 0; i++) {
71+
if (n & 1) {
72+
if (j !== -1) {
73+
res = Math.max(res, i - j);
74+
}
75+
j = i;
76+
}
77+
n >>= 1;
78+
}
79+
return res;
80+
}
81+
```
82+
83+
### **Rust**
84+
85+
```rust
86+
impl Solution {
87+
pub fn binary_gap(mut n: i32) -> i32 {
88+
let mut res = 0;
89+
let mut i = 0;
90+
let mut j = -1;
91+
while n != 0 {
92+
if n & 1 == 1 {
93+
if j != -1 {
94+
res = res.max(i - j);
95+
}
96+
j = i;
97+
}
98+
n >>= 1;
99+
i += 1;
100+
}
101+
res
102+
}
103+
}
104+
```
105+
64106
### **...**
65107

66108
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn binary_gap(mut n: i32) -> i32 {
3+
let mut res = 0;
4+
let mut i = 0;
5+
let mut j = -1;
6+
while n != 0 {
7+
if n & 1 == 1 {
8+
if j != -1 {
9+
res = res.max(i - j);
10+
}
11+
j = i;
12+
}
13+
n >>= 1;
14+
i += 1;
15+
}
16+
res
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function binaryGap(n: number): number {
2+
let res = 0;
3+
let j = -1;
4+
for (let i = 0; n !== 0; i++) {
5+
if (n & 1) {
6+
if (j !== -1) {
7+
res = Math.max(res, i - j);
8+
}
9+
j = i;
10+
}
11+
n >>= 1;
12+
}
13+
return res;
14+
}

0 commit comments

Comments
 (0)