[算法][LeetCode]Single Number——异或运算的巧妙运用
题目要求
Given an 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?
一个int数组中,除了有一个int只出现了一次,其他int都出现了两次,请找出这个int。
要求:设计的算法是线性的复杂度,并且不要用额外的内存空间。
解题思路
异或运算的几个相关公式:
Java代码
public static int singleNumber(int[] A) {
for (int i = 1; i < A.length; i++) {
A[i] ^= A[i-1];
}
return A[A.length-1];
}
[算法][LeetCode]Single Number——异或运算的巧妙运用的更多相关文章
- [LeetCode] Single Number II 位运算
Given an array of integers, every element appears three times except for one. Find that single one. ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
- [LeetCode]Single Number 异或的妙用
1.数组中仅仅有一个元素仅仅出现一次,其余出现偶数次. 利用异或中同样元素相互抵消的方式求解. 2.数组中仅仅有2个元素仅仅出现一次.其余出现偶数次. class Solution { public: ...
- LeetCode算法题-Single Number(Java实现)
这是悦乐书的第175次更新,第177篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第34题(顺位题号是136).给定一个非空的整数数组,除了一个元素外,每个元素都会出现两 ...
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- memcached+Mysql(主从)
昨天和守住看了下http://hi.baidu.com/156544632/blog/item/3b26527b68623ff00bd18746.html这篇文章,思路很好,但感觉就是太乱了,而且还出 ...
- maple minimax函数
numapprox[minimax] - minimax rational approximation Calling Sequence minimax(f, x=a..b, [m, n], w, ...
- python登陆Tom邮箱的代码一例
本文出处参考:http://www.cnblogs.com/LinuxHunter/archive/2010/11/30/1891635.html 在很多的python 教程中都会讲到登录邮箱或发送邮 ...
- 使用JSON Web Tokens和Spring实现微服务
http://www.jdon.com/dl/best/json-web-tokens-spring-cloud-microservices.html
- dp之多重背包poj1276
题意:有现今cash,和n种钱币,每种钱币有ni个,价值为di,求各种钱币组成的不超过cash的最大钱数....... 思路:二进制拆分转化为01背包,或者转化为完全背包都是可以的. 反思:这个题目我 ...
- VBA 获得绝对地址控制焦点的设置
先上代码,有时间再补上说明. Dim Mefoucs As String MsgBox "你选定的当前单元格是:" & Selection.Address Mefoucs ...
- exchange邮箱系统增加验证码机制
首先背景是exchange的邮箱系统没有后台源代码.因为这个原因,生成验证码的机制放在aspx的runat="sever"后台代码里面. 首先需要找到iis中logon.aspx文 ...
- 29个酷炫的Firefox配置参数
你可能安装了许多的firefox插件以增加浏览器的功能,但是如果你想更好地使用firefox,学习如何配置about:config参数是很有必要的. about:config配置页包含了所有的fire ...
- 多个 label checkbox 组合 显示在同一个水平线上[前提Bootstrap框架]
<th align="left" valign="middle"> <label class="checkbox inline fo ...
- 【MySql】脚本备份数据库
#!/bin/bash #this is a script of mysql backup #Mysql="mysql" #MysqlDump="mysqldump&qu ...