当前位置:网站首页>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
边栏推荐
- Self attention and multi head attention
- 72. editing distance ●●
- synergy: server refused client with our name
- 实用 | 如何利用 Burp Suite 进行密码爆破!
- PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
- 蛮力法/1~n的幂集 v4 递归
- torch. nn. Simple understanding of parameter / / to be continued. Let me understand this paragraph
- 【无标题】破目
- Redis set password command (temporary password)
- [generation confrontation network learning part I] classic Gan and its existing problems and related improvements
猜你喜欢

^30H5 Web Worker多线程

2台电脑共享一套键盘鼠标

「运维有小邓」自助帐户解锁工具

View play and earn will lead crypto games astray

Redis缓存穿透

LeetCode:1037. Effective boomerang - simple

自注意力(self-attention)和多头注意力(multi-head attention)

Niuke.com: numbers that appear more than half of the times in the array

LeetCode:497. 非重叠矩形中的随机点————中等
![[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
随机推荐
Elastic-Job的快速入门,三分钟带你体验分布式定时任务
Brute force method / task assignment
Calculus review 1
Redis set password command (temporary password)
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
Nanny tutorial: how to become a contributor to Apache linkis documents
01js基础 null与undefined区别 类型转换 == 代码块 逻辑运算符
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation
Redis cluster configuration
^29事件循环模型
1、 Vulkan develops theoretical fundamentals
「运维有小邓」自助帐户解锁工具
蛮力法/1~n的幂集 v4 递归
聊聊服务器性能优化~(建议收藏)
A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
Can you still have a wonderful life if you are laid off at the age of 35?
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
Signal and system review 1
Magic tower game implementation source code and level generation
Fast Planner - detailed explanation of kinetic astar