LeetCode136: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?
解题思路:
很简单的一题,直接用异或运算解决:连个相同数进行异或结果为0,0与任何数异或结果为原数
也可先进行排序再遍历排序后的数组,当某个数没有重复时,结果就是它了
也可用bitmap进行,不多说了。
实现代码:
#include <iostream> using namespace std;
/*
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?
*/
class Solution {
public:
int singleNumber(int A[], int n) {
int ret = ;
for(int i = ; i < n; i++)
ret ^= A[i];
return ret; }
}; int main(void)
{
int arr[] = {,,,,,,};
int len = sizeof(arr) / sizeof(arr[]);
Solution solution;
int once = solution.singleNumber(arr, len);
cout<<once<<endl;
return ;
}
LeetCode136:Single Number的更多相关文章
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
- LeetCode137:Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- lintcode 中等题:Single number III 落单的数III
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- leetcode:Single Number
public int SingleNumber(int[] nums) { if(nums==null||nums.Length%2==0) return 0; int ret=nums[0]; fo ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [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. Note:Y ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
随机推荐
- Web标准:五、超链接伪类
Web标准:五.超链接伪类 知识点: 1.链接的四种样式 2.将链接转换为块状 3.用css制作按钮 4.首字下沉 1)链接的四种样式 超链接有四个伪类,分别是: a:link 未访问的链接 a: ...
- 2.Add Two Numbers (List)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 1.Two Sum (Array; HashTable)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [leetcode]403. Frog Jump青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- Ubuntu部分快捷键的使用简介
1.切换到字符界面 Ctrl+Alt+F1 切换到tty1字符界面 Ctrl+Alt+F2 切换到tty2字符界面 Ctrl+Alt+F3 切换到tty3字符界面 Ctrl+Alt+F4 切换到tty ...
- Mockplus 3.2前瞻,五大特色功能让你惊喜!
在这个火热的夏季,我们有理由热切期待Mockplus 3.2的发布! 作为国产的一流原型设计工具,Mockplus 3.2版本会给我们带来什么呢? 格子(Repeater) 我们平常的设计,有大量需要 ...
- redmine2.6.5 邮件配置
打开configuration.xml (路径:apps/redmine/htdocs/config/) production: email_delivery: delivery_method: :s ...
- Oracle连接字符串大全
// 在 C# 代码中用以下数据库提供程序访问 Oracle 数据库 // Oracle Data Provider for .NET / ODP.NET 使用 TNS 写法 Data Source= ...
- Unicode、UTF-8 和 ISO8859-1
Unicode.UTF-8 和 ISO8859-1到底有什么区别 1.本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等. 在下面的描述中,将以"中文" ...
- 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)
Java原生的API可用于发送HTTP请求 即java.net.URL.java.net.URLConnection,JDK自带的类: 1.通过统一资源定位器(java.net.URL)获取连接器(j ...