当前位置: 代码迷 >> 综合 >> Single Number - Leetcode
  详细解决方案

Single Number - Leetcode

热度:7   发布时间:2023-12-17 05:33:09.0

public class Solution {public int singleNumber(int[] A) {for(int i=1; i<A.length; i++){A[i]^=A[i-1];}return A[A.length-1];}
}

分析:这个题目限制了空间复杂度和时间复杂度, 考点是位操作里面的异或消除偶数对的特征。(^ 按位异或运算符:如果一个位是1,另一个位是0,结果位就是1。如果相应的位相同,结果位就是0;

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?


  相关解决方案