lintcode-easy-Single Number

Given 2*n + 1 numbers, every numbers occurs twice except one, find it.

Given [1,2,2,1,3,4,3], return 4

因为所有数都出现了两次,只有一个出现一次,所以只要把所有数做XOR,就可以得到出现一次的数

public class Solution {
    /**
     *@param A : an integer array
     *return : a integer
     */
    public int singleNumber(int[] A) {
        if (A.length == 0) {
            return 0;
        }

        int n = A[0];
        for(int i = 1; i < A.length; i++) {
            n = n ^ A[i];
        }

        return n;
    }
}
时间: 2024-10-10 08:20:50

lintcode-easy-Single Number的相关文章

lintcode easy Happy Number

Happy Number Write an algorithm to determine if a number is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process unt

[LintCode] Single Number 单独的数字

Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in a real interview? Yes Example Given [1,2,2,1,3,4,3], return 4 Challenge One-pass, constant extra space. LeetCode上的原题,请参见我之前的博客Single Number. class So

Single Number II

题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,1,2,3,3,3,2,2,4,1] return 4 Challenge One-pass, constant extra space. 链接地址 http://www.lintcode.com/en/problem/single-number-ii/

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

【59】136. Single Number

136. Single Number Description Submission Solutions Add to List Total Accepted: 191020 Total Submissions: 360448 Difficulty: Easy Contributors: Admin Given an array of integers, every element appears twice except for one. Find that single one. Note:Y

136. Single Number C++ 答案

136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int singleNumber(vector<int>& nums) { int s = 0; for(int i = 0; i < nums.size(); i++) { s = s ^ nums[i]; } return s; } }; 参考 LeetCode Problems' So

Configuring Single Number Reach on Cisco CUCM 7.1.3

Cisco CUCM allows you to configure single number reach (also known as mobility) so that a call destined for your desk phone's extension can ring on a number of other devices such as your cellphone, blackberry, home phone, etc.  This feature also allo

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only o

136.Single Number

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? 思路: 一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only