Files
leetcode/136.single-number.java
2025-10-22 16:35:11 +08:00

27 lines
569 B
Java
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.

/*
* @lc app=leetcode id=136 lang=java
*
* [136] Single Number
*/
// @lc code=start
class Solution {
/**
* 可以通过对数组中所有整数进行异或来找出该数,
* 因为对于出现两次的整数异或之后都为0
* 而对于只出现过一次的整数和0异或之后为其本身
*
* @param nums
* @return
*/
public int singleNumber(int[] nums) {
int r = 0;
for(int i = 0; i < nums.length; i++) {
r = r ^ nums[i];
}
return r;
}
}
// @lc code=end