82. Single Number【easy】
Given 2*n + 1
numbers, every numbers occurs twice except one, find it.
Given [1,2,2,1,3,4,3]
, return 4
One-pass, constant extra space.
题意
给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。
解法一:
class Solution {
public:
/*
* @param A: An integer array
* @return: An integer
*/
int singleNumber(vector<int> &A) {
int num = A[];
for (int i = ; i < A.size(); ++i) {
num ^= A[i];
} return num;
}
};
利用异或的性质,相同为0
82. Single Number【easy】的更多相关文章
- 136. Single Number【LeetCode】异或运算符,算法,java
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- 491. Palindrome Number【easy】
Check a positive number is a palindrome or not. A palindrome number is that if you reverse the whole ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
随机推荐
- HDU 1232 (13.10.31)
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Pycharm 2018 激活 亲测有效
下载 https://share.weiyun.com/5NVc5U3 并将 JetbrainsCrack-3.1-release-enc.jar 放置到 pycharm安装目录的\bin目录下( ...
- uva539 卡坦岛 简单回溯!
继续回溯搞起! 开始想复杂了,用了好多数组判断节点的度.边是否已经走过,结果导致超时了,后来简化成如下版本,走过的标志不需要另辟vis数组,只要将map[i][j]和map[j][i]赋值0即可. # ...
- 架构设计:系统间通信(20)——MQ:消息协议(下)
(接上文<架构设计:系统间通信(19)--MQ:消息协议(上)>) 上篇文章中我们重点讨论了"协议"的重要性.并为各位读者介绍了Stomp协议和XMPP协议. 这两种协 ...
- JAVA中split对空串的影响。
public class SplitEmptyString { /** * @param args */ public static void main(String[] args) { // 空串的 ...
- 【树莓派】树莓派raspi-config配置
发现有些树莓派盒子,输入的结果和键盘的实际字符有差异,比如输入 | ,结果显示为 ~. 这是因为树莓派的键盘设置问题. 可以通过设置raspi-config进行配置: 第一次使用树莓派的时候需要进行一 ...
- STL之hashtable源代码剖析
// Filename: stl_hashtable.h /////////////////////////////////////////////////////////////////////// ...
- JUnit 3.8 演示递归删除文件目录的 测试类程序 .
用递归方式来实现删除硬盘的文件或目录(空文件夹) 首先要找到递归的入口及出口,这点很重要,成败在此,呵呵! 代码实现: import java.io.File ; class RecursionDel ...
- Linux 监测常用的图形工具
cacti zabbix nagios nagiosgraph
- OFBiz:解析doRequest()
这里的doRequest()是指RequestHandler中的同名函数: public void doRequest(HttpServletRequest request, HttpServletR ...