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 ...
随机推荐
- CentOS7 时间设置与网络同步
1.查看时区 [root@localhost /]# date -R Thu, Jul :: + +0800表示东八区,这边就不用再设置 时区中的CST表示中国标准时间. 时区相关共享文件在/usr/ ...
- FJNU2018低程F jq解救fuls (贪心乱搞)题解
题目描述 一天fuls被邪恶的"咕咕咕"抓走了,jq为了救fuls可谓是赴汤蹈火,费了九牛二虎之力才找到了"咕咕咕"关押fuls的地方. fuls被关在一个机关 ...
- Attribute2Image --- Conditional Image Generation from Visual Attributes 论文笔记
Attribute2Image --- Conditional Image Generation from Visual Attributes Target: 本文提出一种根据属性生成图像的产生式模 ...
- windows 如何不显示带点的文件名、文件夹?
这些以点开头的文件夹和文件是为了在 linux 中隐藏的.但是在 windows 中如何不显示他们? 资源管理器: CMD:
- python测试
[链接]JointhePythonDeveloperscommunityonSlack! http://pythondevelopers.herokuapp.com/
- Spring依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- C# 防止窗体闪烁
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle ...
- Hash——字符串匹配(求s1在s2中出现的次数)
题目描述: 这是一道模板题. 给定一个字符串 A 和一个字符串 B ,求 B 在 A 中的出现次数.A 和 B中的字符均为英语大写字母. 求A 在 B 中出现了几次.(可重叠) 样例输入: 3 BA ...
- Panda3D
Panda3D 是个开源的游戏及物理引擎(也支持ODE及Bullet). 相关链接:网站: https://www.panda3d.org/下载: https://www.panda3d.org/do ...
- Python:ModuleNotFoundError: No module named 'windows'
pymouse安装后,又出现了ModuleNotFoundError: No module named 'windows'的错误 解决: 下载安装pyhook:http://www.lfd.uci.e ...