【leetcode】Single Number (Medium) ☆
题目:
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?
要求O(n)算法,还不能有辅助空间。开始用了各种O(n^2)的方法,都超时,后来突然顿悟了。异或可以消掉两个相同数字。
直接把所有数字异或在一起,就是单独的那个数字了。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public://异或可以把两个相同的数字消除 O(n) AC
int singleNumber3(int A[], int n) {
int ans = ;
for(int i = ; i < n; i++)
{
ans ^= A[i];
}
return ans;
}
}; int main()
{
Solution s;
int a[] = {,,,,,,,,};
int n = s.singleNumber3(a, ); return ;
}
【leetcode】Single Number (Medium) ☆的更多相关文章
- 【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 && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 【题解】【位操作】【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
Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...
- 【leetcode】Single Number II (medium) ★ 自己没做出来....
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【Leetcode】Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- 【leetcode】Single Number II
int singleNumber(int A[], int n) { int once = 0; int twice = 0; int three = 0; for (int i = 0; i < ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- [设计模式] javascript 之 模板方法模式
模板方法模式说明 定义:定义方法操作的骨架,把一些具体实现延伸到子类中去,使用得具体实现不会影响到骨架的行为步骤! 说明:模式方法模式是一个继承跟复用的典型模式,该模式定义了一个抽象类,Abstrac ...
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- SQL按指定文字顺序进行排序(中文或数字等)
在有些情况下我们需要按指定顺序输出数据,比如选择了ID in(3,1,2,5,4)我们希望按这个3,1,2,5,4的顺序输出,这样只使用order by ID是无法实现的, 但是我们可以使用order ...
- ASP.NET后台输出js大全,页面顶部、form表单中前面与后面、和UpdatePanel(ScriptManager、AJAX)输出JS
Response.Write 与 Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...
- jQuery中添加自定义或函数方法
<script type="text/javascript"> (function () { $.fn.parHy = function (val) { alert(v ...
- 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式
1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...
- ZOJ 3811 Untrusted Patrol
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3811 解题报告:一个无向图上有n个点和m条边,其中有k个点上安装 ...
- 小米手机无法打开程序报错Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication的解决办法
打开studio的setting 然后 Preferences -> Build, Execution, Deployment -> Instant Run -> Enable In ...
- leetcode 86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Android解析服务器Json数据实例
Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...