[Leetcode]136.single number

Source: Internet
Author: User

Topic

Given a non-empty array of integers, every element appears twice except for one. Find the single one.

Note:

Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,1]
Output:1
Example 2:

Input: [4,1,2,1,2]
Output:4

A thought of solution

Brute force solution, direct two for loop, use a count array to save the number of occurrences of each element of the Nums array, then iterate through the Count array, find the subscript res with a value of 1, and return to Nums[i]. The time complexity is O (N2).

Code
class Solution {    public int singleNumber(int[] nums) {        int[] count = new int[nums.length];        int res = -1;        for(int i = 0; i < nums.length; i++)            for(int j = 0; j < nums.length; j++) {                if(nums[i] == nums[j])                    count[i]++;            }        for(int i = 0; i < count.length; i++)            if(count[i] == 1) res = i;        return nums[res];    }}
Two ways to solve the solution

The nums array is sorted first, then the array is iterated once, and the time complexity depends on the sorting algorithm used.

Code
class Solution {    public int singleNumber(int[] nums) {        Arrays.sort(nums);        int res = -1;        for(int i = 0; i < nums.length; i+=2) {            if(i == nums.length-1 || nums[i] != nums[i+1]) {                res = nums[i];                break;            }        }        return res;    }}
Three ways to solve the solution

This solution is true! Of Too! Fine! Color! The Ah! The XOR (XOR) operator ^= is used.

XOR or a binary-based bitwise operation, denoted by a symbol XOR or ^, whose algorithm is for each bits of the operator's sides, the same value takes 0, and the value is 1.
Simple understanding is not carrying the addition, such as 1+1=0,,0+0=0,1+0=1.
Properties
1. Exchange Law
2, the Binding law (ie (a^b) ^c = = a^ (b^c))
3, for any number x, there are x^x=0,x^0=x
4. Reflexive a xor B xor B = A xor 0 = a

That is, for any one number n,n^n=0,0^n=n, all of this problem, as long as all elements are different or can be, res initialized to 0 is because of 0^n=n, will not affect the results.

Code
class Solution {    public int singleNumber(int[] nums) {        int res = 0;        for(int i = 0; i < nums.length; i++) {            res ^= nums[i];        }        return res;    }}

[Leetcode]136.single number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.