【LeetCode】马三来刷题之 Single Number
最近每天刷题又找回了大一大二时天天晚上在机房刷ACM的那种感脚~,题目链接:https://leetcode.com/problems/single-number/
136. Single Number
- Total Accepted: 155864
- Total Submissions: 301077
- Difficulty: Easy
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?
Subscribe to see which companies asked this question
同样是两种方法过的,第一种方法是使用STL的map进行统计,如果一个数字出现出现一次,则将其对应的键的值进行++操作,最后遍历整个map,
一旦发现值为1的就return 该键值对的键。第二种方法是先对数组排序,然后前一个和后一个依次相互比较,一旦发现有不同的,就返回前者,
如果遍历到num.size()-2为止还没有发现有不同的,那唯一的一个不同的值肯定就是最后一个了,所以直接return nums[nums.size()-1]。
第一种方法:
int singleNumber(vector<int>& nums) {if(nums.empty())return 0;map<int,int> m;for(int i=0;i<nums.size();i++){m[nums[i]]++;}for(map<int,int>::iterator it=m.begin();it!=m.end();it++){if((*it).second==1){return (*it).first;}}return 0;
}
第二种方法;
int singleNumber(vector<int>& nums) {sort(nums.begin(),nums.end());for(int i=0;i<nums.size()-2;){if(nums[i]!=nums[i+1])return nums[i];i+=2;}return nums[nums.size()-1];
}
每天一道题,保持新鲜感,就这样~
【LeetCode】马三来刷题之 Single Number相关推荐
- 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /** * Definition for a binary tree no ...
- 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...
- 【LeetCode 剑指offer刷题】查找与排序题14:Wiggle Sort(系列)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Wiggle Sort II Given an unsorted array nums, reorder it su ...
- 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...
- 【LeetCode 剑指offer刷题】字符串题6:67 把字符串转成整数
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 67 把字符串转成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符 ...
- 【LeetCode 剑指offer刷题】回溯法与暴力枚举法题6:Number of Islands
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Number of Islands Given a 2d grid map of '1's (land) and ' ...
- 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...
- 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...
- 【LeetCode 剑指offer刷题】树题19:8 二叉树中序遍历的下一个结点
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 8 二叉树中序遍历的下一个结点 题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注 ...
- 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...
最新文章
- VC++6.0如何删除文件
- 霍布森选择效应(Hobson choice Effect)
- IOS网络篇1之截取本地URL请求(NSURLProtocol)
- Windows Phone DataBound ListBox中针对UIElement的事件绑定(Button)
- 基于TCP协议实现通信
- 嵌入式GUI FTK支持输入法
- Bootstrap 表格的可选样式
- awk 匹配_20分钟降服awk
- 计算机网络之网络安全基础-网络安全概述
- 恒强制版系统980_恒强制版软件操作答疑
- java面向对象程序设计 第3版_Java面向对象程序设计(第3版)-聂哲
- Mac 朗文5++(Longman LDOCE5)双解·例句发音 词库制作
- Linux Shell脚本面试25问
- 无线ap的ntp服务器,怎么配置cisco路由器的NTP
- 【PAT】A1107 Social Clusters (30分)
- vc2010 std::tr1 bind库捉虫记
- 有趣的设计模式——从一杯奶茶品味装饰模式
- 深度学习笔记:神经网络权重确定初始值方法
- iOS安全攻防(二十):越狱检测的攻与防
- EMV技术学习和研究(十一)QPBOC