leetcode解题
This commit is contained in:
26
136.single-number.java
Normal file
26
136.single-number.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* @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
|
||||
|
||||
Reference in New Issue
Block a user