leetcode 136、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
两个相同的数异或为0,一个数和0异或不变,因此将所有的数异或即只出现一次的数。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = ;
for (int i : nums) {
ans ^= i;
}
return ans;
}
};
leetcode 136、Single Number的更多相关文章
- [LeetCode#136, 137]Single Number, Single Number 2
The question: Single Number Given an array of integers, every element appears twice except for one. ...
- leetcode@ [136/137] Single Number & Single Number II
https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...
- 【Leetcode 136】Single Number
问题描述:给出一个整数数组,除了一个元素外,其他每个元素都出现了2次,找出只出现1次的元素. int singleNumber(vector<int>& nums); 分析:比较自 ...
- leetcode 136 137 Single Number
题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...
- 【一天一道LeetCode】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- LeetCode 笔记26 Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
随机推荐
- Paw —— 比Postman更舒服的API利器
特点: 颜值高本地应用,流畅有收藏夹,管理请求可使用环境变量.比如用来一键切换开发环境请求和线上环境请求.即不同环境的同个接口只有host不一样,其它都是一样的,所以就把host抽离出来弄成一个环境变 ...
- 阿里云 Ubuntu16.04 部署 LAMP
1.更新软件源 sudo apt-get update 2.安装Apache sudo apt-get install apache2 3.查看Apache是否安装成功 apache2 –v 如下所示 ...
- mapreduce求平均数
1. 现有某电商关于商品点击情况的数据文件,表名为goods_click,包含两个字段(商品分类,商品点击次数),分隔符“ ”,由于数据很大,所以为了方便统计我们只截取它的一部分数据,内容如下 ...
- java——注解Annotation
http://www.cnblogs.com/xdp-gacl/p/3622275.html 元注解: @Retention:当 @Retention 应用到一个注解上的时候,它解释说明了这个注解的的 ...
- oracle merge into函数中插入clob字段
当使用Merge into 函数向ORACLE数据库中插入或更新数据时,报错“ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”,使用如下方法可以把String转换为clob. 需 ...
- ES6多行字符串+模板字符串
多行字符串 最新的ES6标准新增了一种多行字符串的表示方法,用反引号 ` ... ` 表示: 'use strict'; // 如果浏览器支持模板字符串,将会替换字符串内部的变量: var name ...
- Python 15 I/O编程
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- java编程--01介绍日期的比较
引子:平时开发常常需要对时间进行格式化,进行比较,进行加减计算.最常用的类不外乎:SimpleDateFormat,Calendar,Date,DateTimeStamp等.下面想对java中的日期编 ...
- 【C】一个读取文件记录器
分享一下!!! #include<stdio.h> #include<windows.h> #define N 70000 //定义行数!!用空间换取时间 struct stu ...
- [转]Javascript的匿名函数
本文转自:http://dancewithnet.com/2008/05/07/javascript-anonymous-function/ 一.什么是匿名函数? 在Javascript定义一个函数一 ...