题目:
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].

Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

翻译:
给定一个数组,里面有2个元素,只出现一次,其他元素都是出现2次。求这2个出现一次的元素。
说明:返回结果的数组顺序不重要。你的程序需要运行在线性复杂度下。你可以实现它只用常数级的空间复杂度吗?

代码:

public class Solution {public int[] singleNumber(int[] nums) {int[] result=new int[2];Map<Integer,Integer> map=new HashMap<>();for(int i=0;i<nums.length;i++){Integer value=map.get(nums[i]);if(value==null){map.put(nums[i],1);}else{map.put(nums[i],1+value);}}int resultIndex=0;for(Integer key:map.keySet()){if(map.get(key)==1){result[resultIndex]=key;resultIndex++;}}return result;}
}

Leet Code OJ 260. Single Number III [Difficulty: Medium]相关推荐

  1. Leet Code OJ 136. Single Number [Difficulty: Medium]

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

  2. Leet Code OJ 2. Add Two Numbers [Difficulty: Medium]

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  3. Leet Code OJ 482. License Key Formatting [Difficulty: Medium]

    题目 Now you are given a string S, which represents a software license key which we would like to form ...

  4. 【?异或】LeetCode 260. Single Number III

    LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...

  5. leetcode 算法解析(一):260. Single Number III(C++版本和自己的注解)

    这个题来自<剑指offer>但是书上上感觉讲解不太详细,还是看博客吧(我把下面博客改写成了C++版本运行通过) 注意这个题的相关代码中,输入的数组只能有两个数出现一次,如果有第三个数出现一 ...

  6. Leet Code OJ 202. Happy Number [Difficulty: Easy]

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

  7. Leet Code OJ 263. Ugly Number [Difficulty: Easy]

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

  8. Leet Code OJ 268. Missing Number [Difficulty: Medium]

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is miss ...

  9. leetcode 260. Single Number III | 260. 只出现一次的数字 III(位运算:分组异或)

    题目 https://leetcode.com/problems/single-number-iii/ 题解:分组异或 参考1:讨论区题解 you know you can eliminate dou ...

最新文章

  1. 通过WebRTC实现实时视频通信(三)
  2. unity片元着色器中获取屏幕坐标_Unity踩坑笔记(持续更新)
  3. XMLHttpResponse 在项目里面的运用
  4. 【渝粤题库】国家开放大学2021春3962金融营销基础题目
  5. 利用open***建立桥接***[zt]
  6. Interlocked..::.Add 方法
  7. MD5加密的两种方式
  8. 计算机毕业论文java毕业设计论文题目基于SpringBoot项目源码旅游信息管理系统[包运行成功]
  9. y480 linux无线网卡驱动,联想y480无线网卡驱动下载
  10. android 跳转京东app,打开并跳转到京东app指定页面
  11. cocoscreator摘星星补充-添加结束游戏
  12. 【运维心得】如何一步切换企业邮箱主域名
  13. Longest Word
  14. 站在物联网风口 中兴着力构建GIA 的IoT新生态
  15. echart地图飞线图
  16. Oracle创建表空间和用户
  17. 码、候选码、主码、全码和外码的区分
  18. APS系统的现状以及与MES系统的关联
  19. unity 摄像头跟着鼠标移动_unity第三视角移动,摄像机跟随
  20. 动规(11)-踩方格

热门文章

  1. java大话_大话JAVA(一)
  2. 0x5f3759df的数学原理
  3. 查看UNIX/Linux资源占用的top命令
  4. windbg调试堆破坏
  5. 短视频时代不可忽视的幕后功臣竟然是它!
  6. Python中的WSGI
  7. 和一场音视频技术大会只有一条网线的距离
  8. 当 AI 足够聪明时,我们的验证码还有用吗?
  9. 收藏 | 2020年腾讯技术工程十大热门文章
  10. 腾讯觅影:AI+医疗不光能诊断癌症,还可以很公益