当前位置:网站首页>136. Single Number只出现一次的数字
136. Single Number只出现一次的数字
2022-08-08 02:07:00 【DXB2021】
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:
You must implement a solution with a linear runtime complexity and use only constant extra space.
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
Example 1:示例 1:
Input: nums = [2,2,1]
Output: 1
输入: [2,2,1] 输出: 1
Example 2:示例 2:
Input: nums = [4,1,2,1,2]
Output: 4
输入: [4,1,2,1,2] 输出: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
1 <= nums.length <= 3 * 
-3 *
<= nums[i] <= 3 * 
Each element in the array appears twice except for one element which appears only once.
C语言:(异或运算)
int singleNumber(int* nums, int numsSize){
for(int i=1;i<numsSize;i++)
nums[0]^=nums[i];
return nums[0];
}执行结果: 通过
执行用时:16 ms, 在所有 C 提交中击败了88.61%的用户
内存消耗:7 MB, 在所有 C 提交中击败了61.65%的用户
通过测试用例:61 / 61
C语言:(暴力法)
int singleNumber(int* nums, int numsSize){
int result;
for(int i=0;i<numsSize;i++)
{
for(int j=0;j<numsSize;j++)
{
if((nums[i]==nums[j])&&(i!=j))
{break;}
if(j==numsSize-1)
{result=nums[i];}
}
}
return result;
}执行结果:通过
执行用时:1020 ms, 在所有 C 提交中击败了6.61%的用户
内存消耗:7.1 MB, 在所有 C 提交中击败了41.73%的用户
通过测试用例:61 / 61
边栏推荐
猜你喜欢

torch.utils.data of PyTorch source code interpretation: the whole process of parsing data processing (very good, one article is enough)

puzzle(018.4)珍珑棋局

LeetCode刷题——组合总和 Ⅳ#377#Medium

Embedded sharing collection 31-serial port

机器学习笔记 - 基于CNN+OpenCV的图像着色

Several daily LeetCode exercises

音视频基础

leetcode 30. 串联所有单词的子串

如何验证期货公司开户的正规性

微信小程序疑难杂症---修改数组里的某个属性的值
随机推荐
CS8630 invalid nullable value: c # 7.3 "Enable"
1.9 and orderly array insert PTA problem sets
【PHP库】phpseclib - sftp远程文件操作
Rdkit common commands/parameter settings
陈强教授《机器学习及R应用》课程 第九章作业
陈强教授《机器学习及R应用》课程 第十二章作业
尚德理财靠谱吗?证券开户安全吗?
SwiftUI使用MatchedGeometryEffect快速同步不同继承层级中视图的位置和尺寸
Several daily LeetCode exercises
哪里期货开户低手续费高交返
类和对象的深度剖析
R 语言设某电子元件的寿命X(年)服从λ=3的指数分布,求该电子元件寿命为两年的概率。
prometheus学习2数据类型了解&PromQL
Cpu 监控脚本
The futures company how to validate the normality of opening an account
Overseas Metaverse media must pay attention to the integrity of the propaganda
陈强教授《机器学习及R应用》课程 第十章作业
In-depth Synchronized various usage methods
SQL提问!求帮助,急!
Embedded sharing collection 31-serial port