Skip to content

Commit

Permalink
feat: add solutions to lcci problem: No.05.06
Browse files Browse the repository at this point in the history
No.05.06.Convert Integer
  • Loading branch information
YangFong committed May 26, 2022
1 parent 6d9d4fe commit 8cce607
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lcci/05.06.Convert Integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ class Solution {
}
```

### **TypeScript**

```ts
function convertInteger(A: number, B: number): number {
let res = 0;
while (A !== 0 || B !== 0) {
if ((A & 1) !== (B & 1)) {
res++;
}
A >>>= 1;
B >>>= 1;
}
return res;
}
```

### **Rust**

```rust
impl Solution {
pub fn convert_integer(a: i32, b: i32) -> i32 {
(a ^ b).count_ones() as i32
}
}
```

### **...**

```
Expand Down
26 changes: 26 additions & 0 deletions lcci/05.06.Convert Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ class Solution {
}
```

### **TypeScript**

```ts
function convertInteger(A: number, B: number): number {
let res = 0;
while (A !== 0 || B !== 0) {
if ((A & 1) !== (B & 1)) {
res++;
}
A >>>= 1;
B >>>= 1;
}
return res;
}
```

### **Rust**

```rust
impl Solution {
pub fn convert_integer(a: i32, b: i32) -> i32 {
(a ^ b).count_ones() as i32
}
}
```

### **...**

```
Expand Down
5 changes: 5 additions & 0 deletions lcci/05.06.Convert Integer/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn convert_integer(a: i32, b: i32) -> i32 {
(a ^ b).count_ones() as i32
}
}
11 changes: 11 additions & 0 deletions lcci/05.06.Convert Integer/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function convertInteger(A: number, B: number): number {
let res = 0;
while (A !== 0 || B !== 0) {
if ((A & 1) !== (B & 1)) {
res++;
}
A >>>= 1;
B >>>= 1;
}
return res;
}

0 comments on commit 8cce607

Please sign in to comment.