C#LeetCode刷题之#136-只出现一次的数字(Single Number)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4046 访问。
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
输入: [2,2,1]
输出: 1
输入: [4,1,2,1,2]
输出: 4
Given a non-empty 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?
Input: [2,2,1]
Output: 1
Input: [4,1,2,1,2]
Output: 4
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4046 访问。
public class Program {public static void Main(string[] args) {int[] nums = null;nums = new int[] { 4, 1, 2, 1, 2 };var res = SingleNumber(nums);Console.WriteLine(res);nums = new int[] { 2, 2, 1 };res = SingleNumber2(nums);Console.WriteLine(res);nums = new int[] { 5, 9, 3, 9, 3, 2, 5 };res = SingleNumber3(nums);Console.WriteLine(res);Console.ReadKey();}private static int SingleNumber(int[] nums) {var dic = new Dictionary<int, int>();for(var i = 0; i < nums.Length; i++) {if(dic.ContainsKey(nums[i])) {dic[nums[i]]++;} else {dic[nums[i]] = 0;}}foreach(var item in dic) {if(item.Value == 0) {return item.Key;}}return 0;}private static int SingleNumber2(int[] nums) {var table = new Hashtable();for(var i = 0; i < nums.Length; i++) {if(table.ContainsKey(nums[i])) {table.Remove(nums[i]);} else {table.Add(nums[i], nums[i]);}}var enumerator = table.Values.GetEnumerator();enumerator.MoveNext();return (int)enumerator.Current;}private static int SingleNumber3(int[] nums) {var result = 0;foreach(var num in nums) {result ^= num;}return result;}}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4046 访问。
4
1
2
分析:
显而易见,以上3种算法的时间复杂度均为: 。SingleNumber3的执行效率是最高的,并且没有使用额外的空间,意思是指算法的空间复杂度为 。
SingleNumber3的解法分析参考我的另一篇博文 C#LeetCode刷题之#268-缺失数字(Missing Number)。
C#LeetCode刷题之#136-只出现一次的数字(Single Number)相关推荐
- Leetcode题库 136.只出现一次的数字(异或 C实现)
文章目录 思路 代码 思路 两相同数字异或时为0, 将数组中数字挨个儿异或, 最后仅会剩下那个只出现一次的数字 代码 int singleNumber(int* nums, int numsSize) ...
- Leetcode刷题100天—2042. 检查句子中的数字是否递增—day70
前言: 作者:神的孩子在歌唱 大家好,我叫智 2042. 检查句子中的数字是否递增 难度简单2收藏分享切换为英文接收动态反馈 句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔 ...
- C#LeetCode刷题之#434-字符串中的单词数(Number of Segments in a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...
- C#LeetCode刷题之#171-Excel表列序号(Excel Sheet Column Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3852 访问. 给定一个Excel表格中的列名称,返回其相应的列序 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) 15.3% 中 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...
- LeetCode刷题实战417:太平洋大西洋水流问题
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- Leetcode刷题指南和top100题目
原文链接:https://blog.csdn.net/qq_39521554/article/details/79160815 参考博文:https://blog.csdn.net/mmc2015/a ...
最新文章
- 【译】Linux系统和性能监控(2)
- 【划分树】 POJ 2104 HDU 2665 K-th Number 裸题
- 6000字思考!一篇看懂促销系统的底层逻辑
- 去中心化钱包CoinU基本介绍,你想知道的都有。
- 分区助手扩大c盘后自动修复_C盘空间不够,如何扩大?
- TensorFlow 1.0正式发布
- ScrollView中使用ListView
- block在美团iOS的实践
- postfix邮件服务器
- protocol学习笔记001---RPC和HTTP协议之间的区别_与各自优势
- WebService处理Map
- 像素越高,模型越好?2.1亿 VS 3亿实战测试
- Ksplice:不再重启你的Linux
- linux 后台进程管理,Linux 后台进程管理利器 Supervisor
- linux 内核参数优化 mysql_Linux 系统内核参数优化
- js vue 创建一个div_Vue.js 创建一个 CNODE 社区(1)
- [企业邮局]域名选择与注册
- 在微信支付宝提现时候对输入框输入值的判断
- 1000道最新高频Java面试题,覆盖25个技术栈(多线程、JVM、高并发、spring、微服务、kafka,redis、分布式)从底层原理到架构!
- 老司机开车|消费升级如何具体化?