Skip to content

Latest commit

 

History

History
59 lines (43 loc) · 1.24 KB

0136-single-number.adoc

File metadata and controls

59 lines (43 loc) · 1.24 KB

136. Single Number

{leetcode}/problems/single-number/[LeetCode - Single Number^]

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4

思路分析

只有一个数出现一次,其余数字出现两次,则可以用异或来找出该数。异或两位相异得 1,相同得 0,则出现两次的数字都全部得 0,留下了数字即为只出现一次的数字。

{image_attr}
一刷
link:{sourcedir}/_0136_SingleNumber.java[role=include]
二刷
link:{sourcedir}/_0136_SingleNumber_2.java[role=include]