leetcode:136. Single Number解题报告
2017-03-04 21:08
344 查看
Single Number
1、原题
Given an 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?
2、题目解析
这道题是说让我们在一个整型数组中寻找唯一一个只出现一次的数字,其它的都出现了两次。这道题最简单的思路就是遍历数组去一个一个标记比较该数字出现了几次了。但是看其提示说应该要采用线性的算法。这样这条路就走不通了。
我们看完题目的关键点要放在其它数字出现两次这里,我们可以想到使用异或运算两个相同的数字会变为0,让后0和任何数进行异或还是原来的数,这样这题就很简答了。
3、实现
public class Solution { public int singleNumber(int[] nums) { int item = nums[0]; for (int i = 1; i < nums.length; i++) { item = item ^ nums[i]; } return item; } }
相关文章推荐
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
- LeetCode 解题报告 Single Number
- LeetCode-Single Number-解题报告
- LeetCode解题笔记136 Single Number
- [leetcode]136. Single Number@Java解题报告
- 【LeetCode】Single Number 解题报告(Java & Python)
- [LeetCode] 416. Partition Equal Subset Sum 解题报告
- 【LeetCode】Basic Calculator 解题报告
- [Leetcode] 201. Bitwise AND of Numbers Range 解题报告
- [Leetcode] 206. Reverse Linked List 解题报告
- 【LeetCode】141.Linked List Cycle(easy)解题报告
- 【LeetCode】438. Find All Anagrams in a String 解题报告
- Leetcode #56. Merge Intervals 间隔合并 解题报告
- [leetcode] 72. Edit Distance 解题报告
- [Leetcode] 502. IPO 解题报告
- [leetcode] 60. Permutation Sequence 解题报告
- [Leetcode] 670. Maximum Swap 解题报告
- 【LeetCode】Min Stack 解题报告
- [Leetcode] 331. Verify Preorder Serialization of a Binary Tree 解题报告
- LeetCode: Letter Combinations of a Phone Number 解题报告