【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 ...
随机推荐
- 119th LeetCode Weekly Contest Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- vue 同一个组件的跳转, 返回时保留原来的下拉位置
1,需求分析 公司的项目有这样一个需求: 同一个list组件,根据传过来的listId渲染成多个页面,每个页面都可以下拉.在返回到不同的list页面时,要保留当时下拉的位置. 说的我自己都有点懵逼了, ...
- webApi的控制台服务
1.新建console项目,引用 下面包 2.新建Controller public class UserController : ApiController { public IEnumerable ...
- 获取url路径的正则
package unit; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Util { // ...
- Java 安全套接字编程以及 keytool 使用最佳实践
Java 安全套接字编程以及 keytool 使用最佳实践 http://www.ibm.com/developerworks/cn/java/j-lo-socketkeytool/
- $bzoj1052-HAOI2007$ 覆盖问题 抽屉原理 二分答案
题面描述 某人在山上种了\(N\leq 2*10^4\)棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用\(3\) ...
- kafaka安装
wget https://mirrors.cnnic.cn/apache/kafka/2.0.0/kafka_2.11-2.0.0.tgz 解压 Tar -xvf kafka_2.11-2.0.0.t ...
- baidumapapi点线面的绘制已离线化
百度API离线化 baidumapapi2.0商用是要收费的,开发者使用也要申请个Key. 有个项目要用到点线面的绘制功能,在百度的API示例中发现有这样js封装(DrawingManager_min ...
- Unity 修改windows窗口的标题
修改windows窗口的标题名称,就是修改下图的东西: 第一种: using UnityEngine; using System; using System.Runtime.InteropServic ...
- Single Vendor Project in OpenStack
1.astara: ptl: name: Ryan Petrello irc: ryanpetrello email: ryan.petrello@dreamhost.com irc-channel: ...