弟中弟的Leetcode总结——数组类(十一)

题目描述

Single Number

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?

Example 1:
Input: [2,2,1]
Output: 1

Example 2:
Input: [4,1,2,1,2]
Output: 4

思路

这道题本身我的思路是想用访问表来记录每个数字出现的次数来做,可以达到线性时间的要求。但是题目提示说可以不需要额外的内存,因此需要换别的思路。
在讨论区看到了大神的思路——位运算XOR。当两个相同的数做XOR运算时,结果为0。当0与其它数做XOR运算的时候,结果就是那个数。因此只需要将数组中的所有数全部进行位运算,最后的结果一定就是单独的那个数。

代码(C)

int singleNumber(int* nums, int numsSize) {int ans=0;for(int i=0;i<numsSize;i++){ans^=nums[i];}return ans;
}

弟中弟的Leetcode总结——数组类(十一)相关推荐

  1. 弟中弟的Leetcode总结——数组类(十)

    弟中弟的Leetcode总结--数组类(十) 题目描述 Climbing Stairs You are climbing a stair case. It takes n steps to reach ...

  2. 弟中弟的Leetcode总结——数组类(五)

    弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...

  3. 弟中弟的Leetcode总结——数组类(二)

    弟中弟的Leetcode总结--数组类(二) 题目描述 Container With Most Water Given n non-negative integers a1, a2, -, an , ...

  4. 弟中弟的Leetcode总结——数组类(四)

    弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...

  5. 弟中弟的Leetcode总结——数组类(八)

    弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...

  6. 弟中弟的Leetcode总结——数组类(九)

    弟中弟的Leetcode总结--数组类(九) 题目描述 Subsets Given a set of distinct integers, nums, return all possible subs ...

  7. 弟中弟的Leetcode总结——数组类(六)

    弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...

  8. 弟中弟的Leetcode总结——数组类(七)

    弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...

  9. 弟中弟的Leetcode总结——字符串类(一)

    弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...

最新文章

  1. 关于学习Python的一点学习总结(26->自定义函数及创建初始化数据结构函数)
  2. weblogic不用密码启动服务
  3. 如何成为一名优秀的web前端工程师[转]
  4. android textview import,android – textview中的镜像文本?
  5. Nginx跨域问题的原因分析
  6. mysql+磁盘i+o+优化_浅析MySQL数据库磁盘I/O调整优化 | 学步园
  7. 2021年中国电动卡车马达市场趋势报告、技术动态创新及2027年市场预测
  8. matlab fft2怎么移动频率对称,fft2 二维快速傅里叶变换(Matlab)
  9. HUffman树学习笔记
  10. hevc参考代码matlab版,HEVC部分源码剖析
  11. 汽车零部件开发工具OSEK NM协议栈源代码及配置功能
  12. CVE-2018-18311 阿里云漏洞修复 RHSA-2019:0109-Important: perl secur
  13. 一线工程师告诉你嵌入式真实现状与发展前景
  14. 如何提高深度睡眠质量,科学解决睡眠问题
  15. 图像融合之Poisson融合及其改进
  16. 安卓 魔窗SDK 快速接入
  17. .net Stream篇(五)
  18. python机器人库(robotics-toolbox-python)的运用
  19. 高代第2章第1节n元排列:奇排列、偶排列的判断
  20. scrapy实战项目(简单的爬取知乎项目)

热门文章

  1. 【教学类-29-03】20230409《门牌号-黏贴版(5层*5间)灰底下划线》-(中班《我爱我家》偏数学)
  2. max模型怎么导入ue4_3DMax模型导入ue4的步骤是什么求详解?
  3. C/C++家族族谱管理系统
  4. Python的ORM框架Peewee使用入门(二)
  5. 邓应海:4.20今日黄金原油走势分析,附精准黄金原油策略布局
  6. 【git安装配置 / 拉取上传】仓库流程
  7. html pc端一般宽度多少钱,pc网页设计尺寸_pc端网页设计尺寸规范
  8. C语言基础之四舍五入
  9. Elasticsearch索引容量管理实践
  10. 诺基亚6升级+Android8,诺基亚6升级安卓7.1.1系统:不止是更快