Files
leetcode/top-100-liked/96/leetcode_test.go
T
2026-07-23 16:56:50 +08:00

50 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package top100liked
import (
"testing"
)
// https://leetcode.cn/problems/single-number/?envType=study-plan-v2&envId=top-100-liked
// 136. 只出现一次的数字
//
// 给你一个 非空 整数数组 `nums` ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
// 你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。
// 示例 1
// 输入:nums = [2,2,1]
// 输出:1
// 示例 2
// 输入:nums = [4,1,2,1,2]
// 输出:4
// 示例 3
// 输入:nums = [1]
// 输出:1
//
// 提示:
// - `1 <= nums.length <= 3 * 10^4`
// - `-3 * 10^4 <= nums[i] <= 3 * 10^4`
// - 除了某个元素只出现一次以外,其余每个元素均出现两次。
//
func singleNumber(nums []int) int {
}
func TestSingleNumber(t *testing.T) {
cases := []struct {
name string
nums []int
want int
}{
{"example1", []int{2, 2, 1}, 1},
{"example2", []int{4, 1, 2, 1, 2}, 4},
{"example3", []int{1}, 1},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := singleNumber(c.nums)
if got != c.want {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}