[LeetCode] 136.Single Number Java

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

题意及分析:一个数组中,有一个数只出现了一次,其他的出现了两次。要求给出只出现一次的数。这道题,最简单的方法是用一个hashtable来储存,然后得出结果,但是效率比较低。也可以用位运算求解。直接使用异或运算,因为对于异或运算有:n^0=n,n^n=0,所以相同的数异或为0,最后得出的结果就为出现一次的数。

位运算常用技巧:

(1)n & (n-1)能够消灭n中最右侧的一个1。

(2) 右移:除以2, 左移:乘以2。

(3)异或性质:交换律,0^a=a, a^a=0;

使用hashtable,主要是看hashtable怎么遍历:

import java.util.Enumeration;
import java.util.Hashtable;
public class Solution {
    public int singleNumber(int[] nums) {
        if(nums.length==0) return 0;
        Hashtable<Integer,Integer> res = new Hashtable<>();
        for(int i=0;i<nums.length;i++){
            if(!res.containsKey(nums[i])){
                res.put(nums[i],1);
            } else{
                res.remove(nums[i]);
                res.put(nums[i],2);
            }
        }

        Enumeration e = res.keys();
        while( e.hasMoreElements() ){
            Object x =e.nextElement();
            if(res.get(x)==1){
                return (int)x;
            }
        }
        return 0;
    }
}

 使用位运算:

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

  

时间: 2024-11-10 01:27:58

[LeetCode] 136.Single Number Java的相关文章

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

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

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

Java for LeetCode 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? 解题思路: 按位异或运算,出现两次的都归零了,剩下的就是出现一次的了,JAVA实现如下

LeetCode - 136. Single Number - ( c++ ) - 解题报告

1.题目大意 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 136 Single Number bBt Option

Linked Url:https://leetcode.com/problems/single-number/ Given a non-empty 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 withou

LeetCode: 136 Single Number(easy)

题目: 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? 代码: 1 class Solution { 2 public: 3 int s

leetcode 136. Single Number &amp;&amp; 137. Single Number II

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 136. Single Number C++

Given a non-empty 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? Example 1: Input: [2,2,1] Output: