3

#yyds干货盘点# LeetCode 热题 HOT 100:只出现一次的数字

 1 year ago
source link: https://blog.51cto.com/u_13321676/5780402
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#yyds干货盘点# LeetCode 热题 HOT 100:只出现一次的数字

精选 原创

灰太狼_cxh 2022-10-20 17:47:42 博主文章分类:leetcode ©著作权

文章标签 时间复杂度 代码实现 数组 文章分类 Java 编程语言 阅读数242

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

输入: [2,2,1]

输入: [4,1,2,1,2]

代码实现:

class Solution {
public int singleNumber(int[] nums) {
int single = 0;
for (int num : nums) {
single ^= num;
}
return single;
}
}
  • 收藏
  • 8评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK