4.Single Number(出现一次的数)
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(出现一次的数)的更多相关文章
- LeetCode 136. Single Number (落单的数)
		
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
 - lintcode 中等题:Single number III 落单的数III
		
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
 - LeetCode 136. Single Number C++ 结题报告
		
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
 - LeetCode——Single Number II(找出数组中只出现一次的数2)
		
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
 - [LeetCode] 136. Single Number 单独数
		
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
 - [LeetCode] 137. Single Number II 单独数 II
		
Given a non-empty array of integers, every element appears three times except for one, which appears ...
 - [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 ...
 - LeetCode Single Number I / II / III
		
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
 - 【LeetCode】Single Number I & II & III
		
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
 - LeetCode 【Single Number I II III】
		
Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...
 
随机推荐
- c#抓取网页数据
			
写了一个简单的抓取网页数据的小例子,代码如下: //根据Url地址得到网页的html源码 private string GetWebContent(string Url) { string strRe ...
 - springmvc中针对一个controller方法配置两个url请求
			
转自:https://blog.csdn.net/sun5769675/article/details/50252019
 - elasticsearch 概念与架构(3)
			
转自:https://devops.taobao.com/ Node(节点):单个的装有Elasticsearch服务并且提供故障转移和扩展的服务器. Cluster(集群):一个集群就是由一个或多个 ...
 - 用JS,求斐波那契数列第n项的值
			
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
 - 自定义Android Studio方法注释模板
			
前言 你们从Eclipse转到Android Studio的时候,是不是会怀念Eclipse的方法注释模版? 敲/**加回车,模板就出来了,而Android Studio却不能自定义(或者我没有找到) ...
 - java如何从cmd运行并使用text文件作为输入源的总结
			
大家好,好几天没写东西了,又和大家见面了 首先,编译我们的.java文件,生成.class文件 使用命令 javac yourname.java 编译 然后使用java youname < yo ...
 - elasticsearch配置文件里的一些坑 [Failed to load settings from [elasticsearch.yml]]
			
这里整理几个空格引起的问题. 版本是elasticsearch-2.3.0 或者elasticsearch-rtf-master Exception in thread "main" ...
 - Swing框架的继承关系
			
---------------siwuxie095 Java SE 8 (截止 2017/4/1 最新)在线 API 文档: http://docs.oracle.com/javase/8/docs/ ...
 - SpringBoot10 整合JSP
			
1 整合JSP 1.1 导入相关依赖 JSP依赖.JSTL依赖 <?xml version="1.0" encoding="UTF-8"?> < ...
 - g2o20160424 CMakeLists.txt
			
LIB_PREFIX: 设置生成库的前缀 SET(LIB_PREFIX g2o_) # The library prefix SET(LIB_PREFIX g2o_) 变量的默认配置 # defaul ...