Level:

  Easy

题目描述:

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,那么我们可以将整个数组中的元素进行异或最后的结果肯定就是只出现一次的数。

代码:

class Solution {
public int singleNumber(int[] nums) {
int xor=0;
for(int i=0;i<nums.length;i++){
xor=xor^nums[i];
}
return xor;
}
}

4.Single Number(出现一次的数)的更多相关文章

  1. LeetCode 136. Single Number (落单的数)

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  2. lintcode 中等题:Single number III 落单的数III

    题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  3. LeetCode 136. Single Number C++ 结题报告

    136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...

  4. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  5. [LeetCode] 136. Single Number 单独数

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  6. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  7. [LeetCode] 260. Single Number III 单独数 III

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  8. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  9. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  10. LeetCode 【Single Number I II III】

    Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...

随机推荐

  1. 发RTX通知

    安装sdk 在RTXServer目录下找到WebRoot目录,找到里面的SendNotify.cgi(就是一个php页面,默认是pc - ascii编码).打开页面,在头部加上编码信息 header( ...

  2. python-xlrd 实现excel 导入数据

    首先安装 xlrd 两种方式: 1.wheel 方式 安装: 首先要下载 wheel :

  3. webapi中使用token验证(JWT验证)

    本文介绍如何在webapi中使用JWT验证 准备 安装JWT安装包 System.IdentityModel.Tokens.Jwt 你的前端api登录请求的方法,参考 axios.get(" ...

  4. List的使用1(两张表或者一张表的自身关系)

    第一,在Model中 首先,在视图Model(GZUModel)中定义一个SelectListItem集合 public List<SelectListItem> AList { get; ...

  5. Datapump tips

    同时导出多个schema下的表 $ expdp system/manager dumpfile=test.dmp logfile=test.log directory=TESTDIR schemas= ...

  6. javascript——正则表达式(RegExp、String)(未完工)

    在 javascript 中,正则表达式由两部分组成:正则表达式的匹配模式文本:匹配模式文本的修饰符: 修饰符: 修饰符 说明 i 忽略大小写 g 执行全局匹配 m 执行多行匹配 匹配模式文本包括以下 ...

  7. Java 对密码MD5

    MD5,即"Message-Digest Algorithm 5(信息-摘要算法)": MD5将任意长度的“字节串”变换成一个128bit的大整数,并且它是一个不可逆的字符串变换算 ...

  8. centos7虚拟机桥接上网(DHCP)

    centos设置成自动获取ip地址方式(DHCP) 1.打开终端查看网卡信息 #ifconfigifcfg-enp0s3lovirbro 2.编辑文件#vim /etc/sysconfig/netwo ...

  9. 提示crontab command not found的解决方法

    操作步骤   1. 确认crontab是否安装:   执行 crontab 命令如果报 command not found,就表明没有安装   2. 安装 crontab   执行 yum insta ...

  10. sql如何选取两个数据表中的值

    一.直接在要选择的数据前面加上数据表的名字就行了 SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Order ...