【Leetcode】【Medium】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?
解题:
此题可直接运用异或运算的性质:N^A^A = N
代码:
class Solution {
public:
int singleNumber(int A[], int n) {
for (int i = ; i < n; ++i)
A[] ^= A[i];
return A[];
}
};
但是在leetcode网站性能分析中,异或运算(19ms)不是最快的,不知道还有没有其他算法可以达到10ms以下。
【Leetcode】【Medium】Single Number的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Single Number II
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. No ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode每天一题】Fibonacci Number(斐波那契数列)
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- 【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刷题笔记】Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
随机推荐
- Loj 6433. 「PKUSC2018」最大前缀和 (状压dp)
题面 Loj 题解 感觉挺难的啊- 状压\(dp\) 首先,有一个性质 对于一个序列的最大前缀和\(\sum_{i=1}^{p} A[i]\) 显然对于每个\(\sum_{i=p+1}^{x}A[i] ...
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...
- Mock单元测试
/// <summary> /// 普通插入 /// </summary> [Fact] public void InsertOrder_Tests() { _sqlMappe ...
- Scrapy错误-no active project Unknown command: crawl
在运行别人的scrapy项目时,使用命令行 scrapy crawl douban(douban是该项目里爬虫的名字,烂大街的小项目---抓取豆瓣电影). 执行之后,出现报错如下: 上网搜寻无果. 大 ...
- 安装配置flutter环境
flutter 的中文文档 https://flutterchina.club/get-started/install/ github 地址 https://github.com/flutter/fl ...
- git 代码统计
查看git上的个人代码提交量: git log --author="Marek Romanowski" --since="2019-01-01" --no-me ...
- Linq 与 Lambda 简单使用
//Lambda表达式详解 //int //List<int> numbers = new List<int> {1,2,3,4,5,6,7,8,9 }; //var n = ...
- webAPI过滤器返回数据加密
项目需求: 接口返回的数据,存在一些敏感信息,不希望其他用户看到,将Data进行加密传输 代码如下: public class EncryptDataFilterAttribute : ActionF ...
- ubuntu下安装vue-cli框架
首先安装好node.js,安装方式见 http://www.cnblogs.com/teersky/p/7255334.html 之后正式开始vue-cli之旅吧,输入以下代码安装vue-cli模块 ...
- jqGrid随窗口大小变化自适应宽度
$(function(){ $(window).resize(function(){ $("#jqgridID").setGridWidth($(window).width()); ...