Single Number

来源:互联网 发布:smtp.163.com ssl端口 编辑:程序博客网 时间:2024/05/21 22:35

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?


这是leetcode上面的一道原题,不是很难但是十分的有趣 https://leetcode.com/problems/single-number/

题目的描述实在是太简陋了,给出的信息很少 大概就是给定一个整数的数组,数组除了一个数字只出现一次其他的都是出现两次, 这里使用^就是异或

有a^b^c=a^c^b所以:

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

0 0
原创粉丝点击