Leetcode:Singel Number
问题描写叙述:
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?
解题思路:本题能够用位运算来求解。由于a^0=a,a^a=0,a^b=b^a(即异或运算满足交换律),且已知数组中除了要找的元素,每一个元素都出现两次。则能够将数组中的各元素依次异或。依据异或运算的交换律,能够改变运算顺序使出现两次的元素相邻,则全部出现两次元素异或的结果为0。最后得到异或的结果为要找的元素。
代码:
class Solution {
public:
int singleNumber(int A[], int n) {
int i;
int result = A[0];
for(i = 1;i < n;i++)
result = result ^ A[i];
return result;
}
};
Leetcode:Singel Number的更多相关文章
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] 694. Number of Distinct Islands
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- 力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...
- [LeetCode] 200. Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] 305. Number of Islands II 岛屿的数量 II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- 【BZOJ 1305】[CQOI2009]dance跳舞
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 男生和女生每个人都分身成两个节点 即x[1],x[2]和y[1],y[2] 然后如果i和j不相互喜欢 那么add(x[i][2],y ...
- 3.is null和is not null
3.WHERE中使用is null和is not null //查询工资是null空值的人 select * from person where salary is null; //查询工 ...
- ftp for linux 配置
曾经配的熟悉的不能再熟悉了的东西,多年不用就忘了. 好真是好记性不如烂笔头.本文假如你已经安装好了, 1,ftp默认是不同意root用户登录的,假设要root用户登录,请例如以下改动:打开/etc/v ...
- HDU 3949 XOR(高斯消元搞基)
HDU 3949 XOR pid=3949" target="_blank" style="">题目链接 题意:给定一些数字,问任取几个异或值第 ...
- 【笨木头Lua专栏】基础补充02:函数的几个特别之处
没想到距离上一篇基础补充已经过了1年多了,近期准备捡回Lua,把基础都补补,今天来聊聊Lua的函数吧~ 0.环境 我突然对Lua又大感兴趣的最主要原因是,Cocos Code IDE開始浮出水面了,它 ...
- kettle工具的设计原则
不多说,直接上干货! Kettle工具在设计初,就考虑到了一些设计原则.这些原则里借鉴了以前使用过的其他一些ETL工具积累下的经验和教训. 易于开发:作为数据仓库和ETL开发者,你只想把时间用在创建B ...
- transient修饰符的作用
transient修饰符的作用: entity实体类: package com.baidu.entity; import com.fasterxml.jackson.annotation.JsonIg ...
- APP信息获取接口
https://itunes.apple.com/lookup?id=APPID&callback=2 http://myapp.com/cgi-bin/mapp/mapp_info?type ...
- JS异步操作之promise发送短信验证码.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta cont ...
- react-native之文件上传下载
目录 文件上传 1.文件选择 2.文件上传 1.FormData对象包装 2.上传示例 文件下载 最近react-native项目上需要做文件上传下载的功能,由于才接触react-native不久,好 ...