zoukankan      html  css  js  c++  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;
        }
    }
    不积跬步无以至千里,千里之堤毁于蚁穴。 你是点滴积累成就你,你的丝丝懒惰毁掉你。 与诸君共勉
  • 相关阅读:
    洛谷P1762 偶数
    复习1
    zoj3329 One Person Game
    poj2096 Collecting Bugs
    hdu4035 Maze
    Cogs 2856. [洛谷U14475]部落冲突
    洛谷P2474 [SCOI2008]天平
    洛谷P3802 小魔女帕琪
    清北刷题冲刺 11-03 p.m
    清北刷题冲刺 11-03 a.m
  • 原文地址:https://www.cnblogs.com/haoHaoStudyShare/p/7350644.html
Copyright © 2011-2022 走看看