当前位置:网站首页>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只出现一次的数字
边栏推荐
- canvas 高级功能(中)
- 堆叠条形图鼠标移入tooltip中提示过滤为0元素,实现自定义气泡
- 35岁被裁员,还能拥有美妙人生吗?
- 中国工科研究生200多篇英文论文中最常见的习惯(The Most Common Habits from more than 200 English Papers written by Gradua)
- 【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
- 揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- 2台电脑共享一套键盘鼠标
- Deploying static websites using OSS and CDN on Alibaba cloud international
- 冷酸灵,一个国产品牌IPO的30年曲折史
猜你喜欢

canvas 高级功能(中)

Self attention and multi head attention

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.

^29事件循环模型

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

AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘

面试必备——synchronized底层原理的基础知识

Elastic-Job的快速入门,三分钟带你体验分布式定时任务

Game compatibility test (general scheme)

A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
随机推荐
Software definition boundary (SDP)
LeetCode:497. Random points in non overlapping rectangles -- medium
Mixin -- mixed
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
LeetCode 进阶之路 - 167.两数之和 II - 输入有序数组
Magic tower game implementation source code and level generation
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
揭秘:春晚微信红包,是如何抗住 100 亿次请求的?
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
Knife4j configuration can use direct copy
Solve the problem that the idea automatically becomes * when there are more than 5 identical packages
Diablo immortal wiki address Diablo immortal database address sharing
Leetcode divides the array so that the maximum difference is k
Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
72. 编辑距离 ●●●
NetWorkX使用方法及 nx.draw()相关参数
Power consumption development experience sharing: design power consumption board
知识图谱/关系可视化
Canvas advanced functions (medium)
Nanny tutorial: how to become a contributor to Apache linkis documents