首页 > 代码库 > 136. Single Number【LeetCode】异或运算符,算法,java
136. Single Number【LeetCode】异或运算符,算法,java
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
题目分析:数字每一位存在数组中,每个数字承兑出现,只有一个数字只出现一次,我们知道异或运算符相同为0不同为1.
/
/
按位与运算&
System.out.println(
0
&
0
);
/
/
0
System.out.println(
0
&
1
);
/
/
0
System.out.println(
1
&
1
);
/
/
1
System.out.println(
"==========="
);
/
/
按位或运算符|
System.out.println(
0
|
0
);
/
/
0
System.out.println(
0
|
1
);
/
/
1
System.out.println(
1
|
1
);
/
/
1
System.out.println(
"==========="
);
/
/
异或运算符^
System.out.println(
0
^
0
);
/
/
0
System.out.println(
0
^
1
);
/
/
1
System.out.println(
1
^
1
);
/
/
0
System.out.println(
"==========="
);
public class Solution { public int singleNumber(int[] nums) { int result = 0; int n =nums.length; for (int i = 0; i<n; i++) { result ^=nums[i]; } return result; } }
136. Single Number【LeetCode】异或运算符,算法,java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。