【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?
看了别人的解答才会的,利用公式a XOR b XOR a = b,从A[n]开始,依次往前执行A[n] = A[n] XOR A[n-1],最后A[0]就是答案。
例如如果序列是{a,b,a,c,b}
那么数组A各项分别是:
| 0 | 1 | 2 | 3 | 4 |
| c XOR a XOR a = c | c XOR b XOR a XOR b = c XOR a | c XOR b XOR a | c XOR b | b |
代码:
class Solution {
public:
int singleNumber(int A[], int n) {
while(--n){
A[n-] ^= A[n];
}
return A[];
}
};
这里自己还犯了一个白痴想法,认为a XOR b要么是0要么是1,这个只是对于a,b为0,1的时候成立的,如果a,b不是0,1,此时是把a,b表示成二进制数后逐位异或的。
Java版本代码:
public class Solution {
public int singleNumber(int[] A) {
int answer = 0;
for(int i = 0;i < A.length;i++){
answer = answer ^ A[i];
}
return answer;
}
}
【leetcode刷题笔记】Single Number的更多相关文章
- 【leetcode刷题笔记】Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- 【leetcode刷题笔记】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【leetcode刷题笔记】Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
随机推荐
- Ubuntu14.04下MySQL的安装与卸载
转载自:https://www.2cto.com/os/201408/329502.html 安装MysQL 执行以下命令:sudo apt-get install mysql-server 2. 继 ...
- (十)Thymeleaf用法——Themeleaf内联
5. 内联 [[...]]是内联文本的表示格式,但需要使用th:inline属性(分为text,javascript,none)激活. 5.1 文本内联 <p th:inline=&quo ...
- webservice中DateTime类型參数的传入问题
This step-by-step article describes how to format DateTime and Date values in the XML that is extrac ...
- 安装 redis [standlone模式]
下载redis版本:https://redis.io/download 我下载的是:redis-3.0.6 下载后,在linux上 tar -zxvf redis-3.0 ...
- JDK自带的定时任务
import java.util.TimerTask; /** * 实现定时任务 * */ public class MyTimerTask extends TimerTask { @Override ...
- MYSQL 随机选取几条数据
SELECT * FROM tablename AS r1 JOIN (SELECT ROUND(RAND() *(SELECT MAX(id)FROM tablename)) AS id) AS r ...
- C++11并发学习之三:线程同步(转载)
C++11并发学习之三:线程同步 1.<mutex> 头文件介绍 Mutex又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文 ...
- k8s调度-指定node
1.给node加标签 kubectl label nodes k8s-slave2 slave= 2.查看标签 [root@k8s_master centos7]# kubectl describe ...
- python 线程安全
http://www.cnblogs.com/monsteryang/p/6592385.html
- SQL-SQL基础
SQL(Structured Query Language)是通用的数据库查询语言,各个数据库厂商均对SQL-92标准做了支持,同一时候各家又再次基础上做了相应扩展,比如oracle的PL/SLQ. ...