Leecode_Single Number
2015-08-25 07:01
211 查看
The Problem:
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?
The Solution:
*The ‘XOR’ of a number and itself is 0. So the XOR of all the elements in the array is the Single Number.
public class Solution {
public int singleNumber(int[] nums) {
int n = 0;
for( int i = 0; i < nums.length; i++)
{
n ^= nums[i];
}
return n;
}
}
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?
The Solution:
*The ‘XOR’ of a number and itself is 0. So the XOR of all the elements in the array is the Single Number.
public class Solution {
public int singleNumber(int[] nums) {
int n = 0;
for( int i = 0; i < nums.length; i++)
{
n ^= nums[i];
}
return n;
}
}
相关文章推荐
- 正则表达式测试工具 regexbuddy
- 轻松python文本专题-单独处理字符串每个字符的方法汇总
- 轻松python文本专题-单独处理字符串每个字符的方法汇总
- CentOS6.5 升级 Python 2.7 版本
- Exactly-once Spark Streaming from Apache Kafka
- Exactly-once Spark Streaming from Apache Kafka
- C语言的标准库概述
- 想访问外网?越狱已不是问题!So Easy!时空隧道邀请码!
- Spring MVC PropertiesMethodNameResolver example
- 【LeetCode-面试算法经典-Java实现】【200-Number of Islands(岛的数目)】
- OSChina 周二乱弹 —— 乱弹是机器生成的么
- Spring MVC MultiActionController annotation example
- 【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】
- Spring MVC MultiActionController example
- POJ 3645 解题报告
- 【LeetCode-面试算法经典-Java实现】【198-House Robber(抢劫犯)】
- Win 10免费升级正在进行中(+ing)
- Simplify Path
- Leetcode: Flatten Binary Tree to Linked List
- Leecode_Missing number