Single Number leetcode java
问题描述:
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?
要求:采用线性时间复杂度,并且最好不使用多余的空间。
一、位操作
异或运算,只有当两个bit不同时,返回1。如果两个数是相同的,异或运算将会返回0。
nums中只含有一个single number,因此,采用异或运算,最后得到的就是所求的single number。
public int singleNumber(int[] nums){ int single = 0; for(int i = 0; i < nums.length; i ++) single = single ^ nums[i]; return single; }
二、hashset
相比于hashtable,hashmap存储的是key-value键值对,hashset直接操作对象,且不允许存储重复元素。
public int singleNumber(int[] nums){ HashSet<Integer> set = new HashSet<Integer>(); for(int i = 0; i < nums.length; i ++) if(!set.add(nums[i])) //若添加存在重复 set.remove(nums[i]); //删除重复元素 Iterator<Integer> iterator = set.iterator(); //迭代器,遍历set return iterator.next();
}
Single Number leetcode java的更多相关文章
- 136. Single Number - LeetCode
Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...
- LeetCode算法题-Single Number(Java实现)
这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...
- leetcode 137. Single Number II ----- java
Given an array of integers, every element appears three times except for one. Find that single one. ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Single Number leetcode
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
随机推荐
- Golang模拟客户端POST表单功能文件上传
客户端通过multipart.Write把文件的文本流写入一个缓存中,然后调用http的Post方法把缓存传到服务器. package main import ( "bytes" ...
- Cannot retrieve metalink for repository: epel 错误解决办法
centos下安装完EPEL源, 然后更新一下yum缓存, 如果发现这样的错误:Error: Cannot retrieve metalink for repository: epel. Please ...
- Docker5之Deploy your app
Make sure you have published the friendlyhello image you created by pushing it to a registry. We’ll ...
- IDEA入门级使用教程----你怎么还在用eclipse?
http://blog.csdn.net/qq_31655965/article/details/52788374
- Xilinx_ISE 14.7 Win10 闪退
打开D:\Xilinx\14.7\ISE_DS\ISE\lib\nt64 将libPortabilityNOSH.dll 重命名为libPortability.dll,替换原libPortabilit ...
- jquery.form.js实现将form提交转为ajax方式提交的方法
本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...
- HTML-CSS-JS-JQ常用知识点总结
html:展示文件 标签:<html><head><title></title><meta><link><style> ...
- linux 基础命令(12月25日笔记)
1. cp指令指令:cp (copy,复制)作用:复制文件/文件夹到指定的位置语法:#cp [-r] 被复制的文档路径 文档被复制到的路径选项: -r:recurs ...
- linux golang
wget -c http://www.golangtc.com/static/go/go1.3.linux-386.tar.gz #下载32位Linux的够源码包 tar -zxvf go1.1.li ...
- mime模块响应或设置Node.js的Content-Type头
转载自:https://itbilu.com/nodejs/core/VJYaAfKrl.html MIME,即:Multipurpose Internet Mail Extensions,多用途 ...