-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI0506-ConvertInteger.go
More file actions
59 lines (49 loc) · 1.35 KB
/
LCCI0506-ConvertInteger.go
File metadata and controls
59 lines (49 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
// 面试题 05.06. Convert Integer LCCI
// Write a function to determine the number of bits you would need to flip to convert integer A to integer B.
// Example1:
// Input: A = 29 (0b11101), B = 15 (0b01111)
// Output: 2
// Example2:
// Input: A = 1,B = 2
// Output: 2
// Note:
// -2147483648 <= A, B <= 2147483647
import "fmt"
func convertInteger(A int, B int) int {
res := 0
for i := 0; i < 32; i++ {
res += (A & 1) ^ (B & 1)
A >>= 1
B >>= 1
}
return res
}
func convertInteger1(A int, B int) int {
res := 0
for i := 0; i < 32; i++ {
a, b := 0, 0
if A != 0 { a = A & (1 << i) }
if B != 0 { b = B & (1 << i) }
if a != b { res++ }
}
return res
}
func main() {
// Example1:
// Input: A = 29 (0b11101), B = 15 (0b01111)
// Output: 2
fmt.Println(convertInteger(29, 15)) // 2
// Example2:
// Input: A = 1,B = 2
// Output: 2
fmt.Println(convertInteger(1, 2)) // 2
fmt.Println(convertInteger(1024, 2046)) // 9
fmt.Println(convertInteger(-1, 1)) // 31
fmt.Println(convertInteger(-1, -1)) // 0
fmt.Println(convertInteger1(29, 15)) // 2
fmt.Println(convertInteger1(1, 2)) // 2
fmt.Println(convertInteger1(1024, 2046)) // 9
fmt.Println(convertInteger1(-1, 1)) // 31
fmt.Println(convertInteger1(-1, -1)) // 0
}