Single Number i,ii,iii

136. Single Number

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

Bit: a ^ a = 0;

public class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
        for(int n : nums){
            res ^= n;
        }
        return res;
    }
}

137. Single Number II

public class Solution {
    public int singleNumber(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0 ; i < nums.length ; i++){
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            if(entry.getValue() == 1)
                return entry.getKey();
        }
        return 0;
    }
}

260. Single Number III

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 once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

public class Solution {
    public int[] singleNumber(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for(int n: nums){
            if(!set.add(n)){
                set.remove(n);
            }
        }
        Iterator<Integer> it = set.iterator();
        int[] res = new int[2];
        int start = 0;
        while(it.hasNext() && start < res.length){
            res[start] = it.next();
            start++;
        }
        return res;
    }
}

二刷可以研究bit做法

时间: 2024-10-06 08:06:50

Single Number i,ii,iii的相关文章

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

Leetcode 137. Single Number I/II/III

Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XOR的特性, X^0 = X, X^X = 0, 并且XOR满足交换律. 1 class Solution(object): 2 def singleNumber(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6

LeetCode 【Single Number I II III】

Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经典的方法,利用两个相同的数异或结果为0的性质,则将整个数组进行异或,相同的数俩俩异或,最后得到的就是那个single number,复杂度是O(n) 代码: class Solution { public: int singleNumber(vector<int>& nums) { int

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

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

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 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

LeetCode: Single Number I &amp;&amp; II

I title: 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? 思路:异或 class Solution { public: int