classSolution { public: intsingleNumber(vector<int>& nums){ int result = nums[0]; for(int i = 1; i < nums.size(); i++) { result = result ^ nums[i]; } return result; } };
解决方案2(Java)
1 2 3 4 5 6 7 8 9
classSolution{ publicintsingleNumber(int[] nums){ int result = 0; for (int num: nums) { result ^= num; } return result; } }
解决方案3(Golang)
1 2 3 4 5 6 7
funcsingleNumber(nums []int)int { var result int for _, n := range nums { result = (n ^ result) } return result }