Single Number and Single Number II
[] Given an array of integers, every element appears twice except for one. Find that single one. [] Given an array of integers, every element appears three times except for one. Find that single one. (better solution is needed) Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
题目中文意思就是说给定一个整数数组,数组中所有元素都出现了两次,只有一个元素只出现了一次,找出这个只出现了一次的元素。
用异或来解决这类问题会非常简单。
代码:
int SingleNumber(int arr[] , int length)
{
int i , xor;
for(xor = , i = ; i < length ; ++i)
xor = xor ^ arr[i]; return xor;
}
完整代码:
#include<iostream>
using namespace std; int SingleNumber(int arr[] , int length)
{
int i , xor;
for(xor = , i = ; i < length ; ++i)
xor = xor ^ arr[i]; return xor;
} int main()
{
int arr[] = { , , , , , , };
int length = sizeof(arr)/ sizeof(int); cout<<SingleNumber(arr , length)<<endl; return ; }
Single Number and Single Number II的更多相关文章
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- LC 375. Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- Find n‘th number in a number system with only 3 and 4
这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...
- odd number、 even number
odd number 奇数 even number 偶数
- JavaScript Number() Vs new Number()
最近在优化一个页面时候.IDEA 提示我错误的使用了包装类.当时感觉很诧异. 随后.我上Stack Overflow上面查了一下,终于发现了问题所在. new Number('123') 与 Numb ...
- es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法
ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值. Number.isFinite()用来检查 ...
- JS由Number与new Number的区别引发的思考
在回答园子问题的时候发现了不少新东西,写下来分享一下 == 下面的图就是此篇的概览,另外文章的解释不包括ES6新增的Symbol,话说这货有包装类型,但是不能new... 基于JS是面向对象的,所以我 ...
- LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number Given an array of integers, every element appears twice except for one. Find that ...
- how to convert a number to a number array in javascript without convert number to a string
how to convert a number to a number array in javascript without convert number to a string 如何在不将数字转换 ...
随机推荐
- 用strtok函数分割字符串
用strtok函数分割字符串 需要在loadrunner里面获得“15”(下面红色高亮的部分),并做成关联参数. //Body response 内容: <BODY><; PRE&g ...
- VS code配置docker的shell环境
今天尝试了下使用docker来做虚拟机,几番折腾后终于搞定可以用了,但是想着每次都要在命令行敲半天也太恶心了,所以就找了一下可视化的管理工具 首先说下,我的docker主机环境是windows10,用 ...
- Python并发编程-进程池回调函数
回调函数不能传参数 回调函数是在主进程中执行的 from multiprocessing import Pool import os def func1(n): print('in func1', o ...
- [BOI2004]Sequence
题面描述 给定整数数组$a_1,a_2,a_3...a_n$,求递增数组$b_1,b_2,b_3...b_n$ 使得$|a_1 - b_1| + |a_2 - b_2| + ... + |a_n - ...
- 51nod1675 序列变换
link 题意: 给定长为n的序列a,b,下标从1开始,问有多少对x,y满足gcd(x,y)=1且$a_{b_x}=b_{a_y}$? $n\leq 10^5.$ 题解: $a_{b_x}$和$b_{ ...
- 【BZOJ】2724: [Violet 6]蒲公英
2724: [Violet 6]蒲公英 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2900 Solved: 1031[Submit][Statu ...
- bzoj 2724 在线区间众数
如果不是在线,就是裸的莫队. 但这道题要求在线,然后就不会了.. 标程: http://hi.baidu.com/__vani/item/ecc63f3527395283c2cf2945 算法主要是分 ...
- Velocity模板学习(一)
一.Velocity是什么 Velocity是一个基于Java的模板引擎,允许任何人仅仅简单地使用模板语言就可以引用由Java代码编写的对象. 二.Velocity的基本语法 1.变量 变量的定义 在 ...
- git -- git clone
git clone 命令参数: usage: git clone [options] [--] <repo> [<dir>] -v, --verbose be more ver ...
- uoj 48 核聚变反应强度 次小公因数
[UR #3]核聚变反应强度 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/48 Description 著名核 ...