博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:260. Single Number III
阅读量:4039 次
发布时间:2019-05-24

本文共 1165 字,大约阅读时间需要 3 分钟。

LeetCode刷题: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.

Example:

Input:  [1,2,1,3,2,5]

Output: [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?


算法设计

package com.bean.algorithm.basic;import java.util.HashSet;import java.util.Iterator;public class SingleNumberIII {	public static int[] singleNumber(int[] nums) {		int[] ans = { 0, 0 };		HashSet
h = new HashSet
(); for (int i = 0; i < nums.length; i++) { if (h.contains(nums[i])) { h.remove(nums[i]); } else { h.add(nums[i]); } } Iterator
i = h.iterator(); if (i.hasNext()) ans[0] = i.next(); if (i.hasNext()) ans[1] = i.next(); return ans; } public static void main(String[] args) { // TODO Auto-generated method stub int[] arrays = new int[] { 1,2,1,3,2,5 }; int[] ANSWER = singleNumber(arrays); for(int i=0;i

程序运行结果:

3 5 

转载地址:http://bitdi.baihongyu.com/

你可能感兴趣的文章
final 的作用
查看>>
在Idea中使用Eclipse编译器
查看>>
idea讲web项目部署到tomcat,热部署
查看>>
IDEA Properties中文unicode转码问题
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>