投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来。
题目为:
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?
解题思路为:其他数字都出现两次,只有一个数字出现一次,思考要用什么方法才能让那些出现两次的数字经过某个操作能相互消除呢?那就是异或操作
代码为:复制代码
1
2
3
4
5
6
7
8public class Solution { public int singleNumber(int[] A) { int result = 0; for(int number: A) result = result ^ number; return result; } }
最后
以上就是现实大白最近收集整理的关于LeetCode刷题之一:寻找只出现一次的数字的全部内容,更多相关LeetCode刷题之一:寻找只出现一次内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复