Advertisement
1988coder

136. Single Number

Jan 28th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/single-number/
  2.  
  3. /**
  4.  * XOR of all numbers. XOR result is the answer.
  5.  *
  6.  * Time Complexity: O(N)
  7.  *
  8.  * Space Complexity: O(1)
  9.  *
  10.  * N = Length of the input array.
  11.  */
  12. class Solution {
  13.     public int singleNumber(int[] nums) throws IllegalArgumentException {
  14.         if (nums == null || nums.length == 0) {
  15.             throw new IllegalArgumentException("Invalid Input");
  16.         }
  17.  
  18.         int result = 0;
  19.  
  20.         for (int num : nums) {
  21.             result ^= num;
  22.         }
  23.  
  24.         return result;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement