LeetCode Single Number (找不不重复元素)
题意:给一个序列,序列中只有1个是单个的,其他都是成对出现的。也就是序列中有奇数个元素。要求找出这个元素。
思路:成对出现用异或最好了。两个同样的数一异或就变零,剩下的,就是那个落单的。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=;
for(int i=; i<nums.size(); i++)
res^=nums[i];
return res;
}
};
AC代码
LeetCode Single Number (找不不重复元素)的更多相关文章
- [Leetcode] single number 找单个数
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- text-align:justify实现文本两端对齐布局,兼容IE
要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清楚的概念.大多 IE下的显示错误,就是源于 haslayout. 什么是 haslayout ...
- 2014 Multi-University Training Contest 2
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uyet.html ZCC Loves Intersection ZCC Loves COT 首先考 ...
- eclipse 开发技巧
1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如applic*.xm ...
- POJ 1716
#include <iostream> #include <algorithm> #define MAXN 20005 using namespace std; int _m[ ...
- Java传入参数个数不确定可用(Type ... values)
/** * 可变长的参数. * 有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法: * 1. 重载,多重载几个方法,尽可能的满足参数的个数.显然这不是什么好办法. ...
- JS操作Radio与Select
//radio的chang事件,以及获取选中的radio的值 $("input[name=radioName]").on("change", function( ...
- lintcode :reverse integer 颠倒整数
题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接 ...
- C语言中volatile关键字的作用
http://blog.csdn.net/tigerjibo/article/details/7427366#comments 一.前言 1.编译器优化介绍: 由 于内存访问速度远不及CPU处理速度, ...
- MIT算法导论——第一讲.Analysis of algorithm
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- XE2编译出来的DLL的DLLMain的退出地方用到了halt0
DelphiXE2内存加模块升级版.支持32位和64位模块. 已转至新的博客 http://www.raysoftware.cn/?p=51 很多年以前写过内存加载DLL的一片技术. http://b ...