Java [Leetcode 136]Single Number
题目描述:
Given an array of integers, every element appears twice except for one. Find that single one.
解题思路:
参考别人的想法,用异或的方法实在是太绝了。A ^ A = 0 and A ^ B ^ A = B,因为只有一个是单着的,那么其他成对的两数相异或的结果是0,最后与单着的数异或还是为这个数。
代码如下:
public class Solution {
public int singleNumber(int[] nums) {
int result = 0;
for (int i = 0; i < nums.length; i++){
result ^= nums[i];
}
return result;
}
}
Java [Leetcode 136]Single Number的更多相关文章
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- LeetCode 136. Single Number C++ 结题报告
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
- [LeetCode] 136. Single Number 单独数
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- LeetCode 136 Single Number(仅仅出现一次的数字)
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...
- Java for LeetCode 136 Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- leetcode 136. Single Number ----- java
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode 136. Single Number (落单的数)
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
随机推荐
- 20160727noip模拟赛zld
首先最优策略肯定是这样的:我们取出这个序列中的最大值,然后将整个序列分为左右两部分, 那么我们一定先把左右两部分合起来然后再与这个值合并 那么我们可以得出一个基于最值查询(rmq)的的算法,但是zld ...
- C# 正则表达式 匹配IP地址
\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b
- 【技术贴】解决MySql连接不上 ip远程连接Host is not allowed to conn
落雨 如果你想连接远程IP的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL ...
- struts.properties配置详解(转)
Struts 2框架有两个核心配置文件,其中struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等.除此之 外,Struts 2框架还包含 s ...
- PHP ServerPush
原文:http://yorsal.com/archives/302 随着人们对Web即时应用需求的不断上升,Server Push(推送)技术在聊天.消息提醒尤其是社交网络等方面开始兴起,成为实时应用 ...
- hdu 4447 Yuanfang, What Do You Think?
思路: 这题有个结论也可以自己归纳: 对于给定的n,其约数用pi表示 T(n)=T(p1)T(p2)……T(pn)T(n') 其中T(n')是这个式子所独有的也就是 T(n')=(x^n-1)/T(p ...
- netty是什么?
Netty是什么? 相对于Tomcat这种Web Server(顾名思义主要是提供Web协议相关的服务的),Netty是一个Network Server,是处于Web Server更下层的网络框架,也 ...
- Linux开机启动流程
开机过程指的是从打开计算机电源直到LINUX显示用户登录画面的全过程: 1)加载BIOS 2)读取MBR 3)Boot Loader 4)加载内核 ...
- Quartz的任务的临时启动和暂停和恢复
Quartz的任务的临时启动和暂停和恢复 在项目中需要手动启停某些服务,那么需要有一个控制这些任务的类.由于任务是有Quartz控制的,我们只需要通过Quartz的相关的API实现相关的功能即可. p ...
- Intellij IDEA创建javaWeb以及Servlet简单实现
1.创建web工程 File --> New --> Project...