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. 思路: 最经 ...
随机推荐
- Windows Bypass UAC
Windows 10 sdclt UAC bypass @echo off reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\A ...
- Linux 对mysql远程授权连接操作 和 查看mysql数据库和表 基本命令
Linux 对mysql远程连接的授权操作 首先linux连接mysql数据库 授权: grant all on *.* to ' with grant option; //允许账户root从任何主机 ...
- DDD学习笔录——领域驱动设计的常见误区(即错误的理解)
可以将DDD看成一种开发思想体系:它促成了一种新的以领域为中心的思维方式. 它是一种学习过程,而非最终目标,这就是DDD的最大优势. 任何团队都可以编写一个软件来满足一组用例的需求,但那些将时间和精力 ...
- 如何利用MATLAB并行计算缩短程序运行时间
本来CPU就是双核,不过以前一直注重算法,没注意并行计算的问题.今天为了在8核的dell服务器上跑程序才专门看了一下.本身写的程序就很容易实现并行化,因为beamline之间并没有考虑相互作用.等于可 ...
- leetcode429
这道题目是属于树的层次遍历,使用两层的队列非空判断. class Solution { public: vector<vector<int>> levelOrder(Node* ...
- js中substring和substr用法与区别
String.substring( ):用于返回一个字符串的子串 用法如下:string.substring(from, to) 其中from指代要抽去的子串第一个字符在原字符串中的位置 to指代所要 ...
- 基于:Hadoop 2.6.0-cdh5.4.0 hive1.1.0 HBase 1.0.0-cdh5.4.0 关键配置文件
core-site.xml <configuration> <property> <name>fs.defaultFS</name> <value ...
- C++——virtual
一.放在父类的函数名前面:多态 1.作用:实现多态:子类可以自定义父类中的virtual函数 #include <iostream> using namespace std; class ...
- getRectSubPix函数
转自http://blog.csdn.net/qq_18343569/article/details/47953441 getRectSubPix函数 2015-08-24 16:47 1104人阅读 ...
- 去掉utf-8的Bom头:使用java以及jdbc不使用第三方库执行sql文件脚本
package com.xxx.xxx.dao; import java.io.BufferedReader; import java.io.File; import java.io.FileInpu ...