当前位置:网站首页>Leetcode advanced road - 136 A number that appears only once
Leetcode advanced road - 136 A number that appears only once
2022-06-10 21:22:00 【Li_ XiaoJin】
Given an array of non-empty integers , Except that an element only appears once , Each of the other elements occurs twice . Find the element that only appears once .
explain :
Your algorithm should have linear time complexity . Can you do this without using extra space ?
Example 1:
Input : [2,2,1]
Output : 1
Example 2:
Input : [4,1,2,1,2]
Output : 4
My mind only thinks of violent solutions , After reading the solution, I found that bit operation can be used , That's clever .
/**
* 136. A number that appears only once
* @Author: lixj
* @Date: 2020/9/21 10:19
*/
public class SingleNumber {
/**
* Violence law
* @param nums
* @return
*/
public int singleNumber(int[] nums) {
if (nums.length ==1) return nums[0];
Arrays.sort(nums);
if (nums[0] != nums[1]) return nums[0];
if (nums[nums.length-1] != nums[nums.length-2]) return nums[nums.length-1];
for (int i = 1; i < nums.length-1; i++) {
if (nums[i-1] != nums[i] && nums[i] != nums[i+1]) {
return nums[i];
}
}
return 0;
}
/**
* An operation
* @param nums
* @return
*/
public int singleNumber1(int[] nums) {
int single = 0;
for (int num : nums){
single ^= num;
}
return single;
}
public static void main(String[] args) {
int[] nums = new int[]{2,4,3,2,3};
// int[] nums = new int[]{2,2,1};
// int[] nums = new int[]{1};
SingleNumber singleNumber = new SingleNumber();
System.out.println(singleNumber.singleNumber1(nums));
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/leetcode Advanced road -136 A number that appears only once
边栏推荐
- Meetup Preview: introduction to the new version of linkis and the application practice of DSS
- 「运维有小邓」自助帐户解锁工具
- Full Permutation V3 recursion of brute force method /1~n
- Read the source code of micropyton - add the C extension class module (3)
- Redis缓存雪崩
- Attack and defense drill | network security "whistleblower": security monitoring
- pytorch深度学习——卷积操作以及代码示例
- Understanding deep learning attention
- What should be paid attention to when designing Multilayer PCB?
- synergy: server refused client with our name
猜你喜欢

35岁被裁员,还能拥有美妙人生吗?

设计多层PCB板需要注意哪些事项?

Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?

Pytorch deep learning -- convolution operation and code examples
![[generation confrontation network learning part I] classic Gan and its existing problems and related improvements](/img/5a/0a4015cd4dcc21afd16ca7f895d909.png)
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements

Redis缓存击穿

2 pcs share a set of keyboard and mouse

【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进

You have to learn math to play art?

聊聊服务器性能优化~(建议收藏)
随机推荐
在YUV图像上根据背景色实现OSD反色
Codeforces Round #798 (Div. 2)
Cas de test app
Niuke.com: numbers that appear more than half of the times in the array
你的公司会选择开发数据中台吗?
Redis缓存击穿
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
Zero trust architecture
蛮力法/1~n个整数中取k个整数
Portable FDW framework for Pb
Whether there is a simple path from brute force method /u to V
Meetup Preview: introduction to the new version of linkis and the application practice of DSS
自注意力(self-attention)和多头注意力(multi-head attention)
【电脑使用】如何设置没有自启项的软件开机启动
揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
Use DAP link to download the executable file separately to the mm32f5 microcontroller
简解深度学习Attention
LeetCode:497. Random points in non overlapping rectangles -- medium
You have to learn math to play art?
电子招标采购商城系统:优化传统采购业务,提速企业数字化升级