当前位置:网站首页>LeetCode 进阶之路 - 136.只出现一次的数字
LeetCode 进阶之路 - 136.只出现一次的数字
2022-06-10 20:01:00 【Li_XiaoJin】
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
示例 1:
输入: [2,2,1]
输出: 1
示例 2:
输入: [4,1,2,1,2]
输出: 4
我的脑子只想出暴力方法来解,看了题解后发现可以用位运算,真是巧妙。
/**
* 136. 只出现一次的数字
* @Author: lixj
* @Date: 2020/9/21 10:19
*/
public class SingleNumber {
/**
* 暴力法
* @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;
}
/**
* 位运算
* @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: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/leetcode进阶之路-136只出现一次的数字
边栏推荐
- Redis缓存击穿
- Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
- mysql基础篇
- Fast Planner - detailed explanation of kinetic astar
- Theoretical basis of distributed services
- 详解三级缓存解决循环依赖
- Construction of RT thread smart win10 64 bit compilation environment
- Four methods to obtain the position index of the first n values of the maximum and minimum values in the list
- Knife4j configuration can use direct copy
- RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
猜你喜欢

Attack and defense drill | network security "whistleblower": security monitoring

Construction of RT thread smart win10 64 bit compilation environment

冷酸灵,一个国产品牌IPO的30年曲折史

^29事件循环模型

连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors

canvas 高级功能(上)

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日

CET-6 - Business English - the last recitation before the test

Connexion MySQL errorcode 1129, State hy000, Host 'xxx' is Blocked because of many Connection Errors

A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
随机推荐
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
In depth learning experience and tools
How to use Diablo immortal database
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
KCon 2022 议题大众评选火热进行中!不要错过“心仪”的议题哦~
Microsoft Word 教程,如何在 Word 中更改页面方向、为页面添加边框?
NetWorkX使用方法及 nx.draw()相关参数
六级考试-商务英语-考前最后一背
解决idea超过5个相同包的时候自动变成*的问题
2台电脑共享一套键盘鼠标
pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)
Nanny tutorial: how to become a contributor to Apache linkis documents
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
请问九洲期货是正规的吗?开户安不安全
[generation confrontation network learning part I] classic Gan and its existing problems and related improvements
2 pcs share a set of keyboard and mouse
Portable FDW framework for Pb
蛮力法/1~n的幂集 v4 递归
Pytorch deep learning -- convolution operation and code examples
Steps to build a personal website using servers purchased by Alibaba cloud international