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. 思路: 最经 ...
随机推荐
- Eclipse一步一步搭建SSM+Maven
Eclipse 搭建SSM(Spring.Spring MVC .Mybatis) 利用Maven管理Jar包 一般而言,新的eclipse都已经集成了maven,如果没有那么 ...
- 用JS写一个简单的程序,算出100中7的倍数的最大值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Anaconda 安装教程(Win10环境) Tensorflow安装
序 Python易用,但用好却不易,其中比较头疼的就是包管理和Python不同版本的问题,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.An ...
- import json
- oracle DML-(insert、select、update、delete)
一.插入记录INSERT INTO table_name (column1,column2,...) values ( value1,value2, ...); 示例:insert into emp ...
- Blender 工具使用—–准星
Blender 工具使用-–准星 移动准星 直接按鼠标左键 将准星放置在坐标原点 快捷键Shift + C 将准星放置到指定位置 比如下面这个位置: 按Shift + S快捷键组合,弹出一个工具栏,选 ...
- 1027C Minimum Value Rectangle
传送门 题目大意 有n个木棍,让你选4根使得组成的矩形的周长的平方除以面积最小. 分析 这个题看起来就是一个需要证明的贪心,下面我们来证明一下: 所以我们只需要枚举一边所有的a的可能值,然后b就是比a ...
- 安装Maven及Eclipse中配置Maven
下载maven版本: 1.进入官网:http://maven.apache.org/download.cgi ,下载编译后的maven版本:如图下: 2.创建一个目录,把下载的maven压缩包,进 ...
- Entity Framework Tutorial Basics(10):Entity Lifecycle
Entity Lifecycle: Before we work on CRUD operation (Create, Read, Update, Delete), it's important to ...
- PHP中 Include 与 Require之间的区别
*引入机制 如果没有给出目录(只有文件名)时则按照 include_path 指定的目录寻找.如果在 include_path 下没找到该文件则 include 最后才在调用脚本文件所在的目录和当前工 ...