Single Number i and ii

Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

c++版:

class Solution {
public:
   int singleNumber(int arr[] , int length)
{
    int result=arr[0];
    for(int i = 1 ; i < length ; ++i)
        result = result ^ arr[i];
     return result;
 }
};

 Java版:

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

  

Single Number II

Given an array of integers, every element appears three times except for one. Find that single one.

C++版:

class Solution {
public:
  int singleNumber(int A[], int n) {
    if(n < 0 || n%3 != 1)    return -1;
    map<int, int> mp;
    map<int, int>::iterator it;
    for(int i = 0; i<n; i++) {
        it = mp.find(A[i]);
        if(it == mp.end())
            mp[A[i]] = 1;
        else
           mp[A[i]] += 1;
    }
    for(it = mp.begin(); it != mp.end(); it++) {
        if((*it).second != 3)   return (*it).first;
    }
    }
};

  

时间: 2024-08-07 04:13:38

Single Number i and ii的相关文章

leetcode136:single number I&II 及拓展

Single Number I 给定一个数组,除了一个元素出现一次之外,其余的元素都出现两次.找出这个元素 分析: 只要将所有的元素都异或一次,最后的结果就是这个出现一次数. 代码: int singleNumber(int A[], int n) { int num; int i; num=A[0]; for(i=1;i<n;i++) { num=num^A[i]; } return num; } Single Number II 给定一个数组,除了一个元素出现一次之外,其余的元素都出现三次.

LeetCode 136、137:Single Number I &amp; II

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? Single Number II Given an arr

Single Number,Single Number II

Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium 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 imp

[LeetCode][JavaScript]Single Number II

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? https://leetcode.com/

LeetCode Single Number I II Python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. def singleNumber(self, A): l = len(A) if l < 2: return A[0] A.sort() for i in range(0,l-1,2): if A[i] != A[i+1]: return A[i] return A[l-1] 思路:

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times 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】Single Number I &amp; II

Single Number I : 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? Solution: 解法不少,贴一种: 1 cla

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? 分析: 第一问属于技巧题,做过就会,

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz