mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
添加 problem 268
This commit is contained in:
9
Algorithms/0268. Missing Number/268. Missing Number.go
Normal file
9
Algorithms/0268. Missing Number/268. Missing Number.go
Normal file
@ -0,0 +1,9 @@
|
||||
package leetcode
|
||||
|
||||
func missingNumber(nums []int) int {
|
||||
xor, i := 0, 0
|
||||
for i = 0; i < len(nums); i++ {
|
||||
xor = xor ^ i ^ nums[i]
|
||||
}
|
||||
return xor ^ i
|
||||
}
|
47
Algorithms/0268. Missing Number/268. Missing Number_test.go
Normal file
47
Algorithms/0268. Missing Number/268. Missing Number_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question268 struct {
|
||||
para268
|
||||
ans268
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para268 struct {
|
||||
s []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans268 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem268(t *testing.T) {
|
||||
|
||||
qs := []question268{
|
||||
|
||||
question268{
|
||||
para268{[]int{3, 0, 1}},
|
||||
ans268{2},
|
||||
},
|
||||
|
||||
question268{
|
||||
para268{[]int{9, 6, 4, 2, 3, 5, 7, 0, 1}},
|
||||
ans268{8},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 268------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans268, q.para268
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, missingNumber(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
Reference in New Issue
Block a user