136. Single NumberLeetCode异或运算符,算法,java

Posted 皓浩浩皓

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了136. Single NumberLeetCode异或运算符,算法,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 NumberLeetCode异或运算符,算法,java的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 136. Single Number

Leetcode-136 Single Number

136. Single Number

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number

136. Single Number