540. Single Element in a Sorted Array

  • 原题
  • C语言题解

原题

You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Find this single element that appears only once.

Example 1:

Input: [1,1,2,3,3,4,4,8,8]
Output: 2

2是单个元素,所以输出为2。

Example 2:

Input: [3,3,7,7,10,11,11]
Output: 10

10是单个元素,所以输出为10。

Note: Your solution should run in O(log n) time and O(1) space.

----------------------------------------代码如下----------------------------------------

C语言题解

int singleNonDuplicate(int* nums, int numsSize){if(nums==NULL || numsSize==0){//若数组数量为1,则直接返回0;return 0;};if(numsSize==1){//若数组数量为1,则直接返回数组第一位;return nums[0];};int i = 0;int num = 0;for(loop = 0; i < numsSize;i++)//遍历所有元素;num^=nums[i];//异或运算。等同于num=num^nums[i]; 1100^1001==0101;//两个相同数字异或,相应的位都会被异或为0;//所以不管多少数字,只要是成对存在,那结果都为0;//所以循环结束后,num=num1^0;num1为单个的元素;例如return num;
}

LeetCode 540 Single Element in a Sorted Array 在分类好的数组里寻找单个元素 C语言 异或的巧妙使用相关推荐

  1. LeetCode 540. Single Element in a Sorted Array

    题目: Given a sorted array consisting of only integers where every element appears twice except for on ...

  2. LeetCode 540. 有序数组中的单一元素(Single Element in a Sorted Array) 42

    540. 有序数组中的单一元素 540. Single Element in a Sorted Array 题目描述 每日一算法2019/6/14Day 42LeetCode540. Single E ...

  3. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  4. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  5. Merge Sorted Array 合并两个有序数组

    1.原题目 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may ass ...

  6. Leetcode——153. Find Minimum in Rotated Sorted Array

    题目原址 https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ 题目描述 Suppose an ...

  7. LeetCode 961 N-Repeated Element in Size 2N Array --python,java解法

    题目地址:N-Repeated Element in Size 2N Array - LeetCode Acceptance:73.3% Difficulty:Easy In a array A of ...

  8. leetcode python3 简单题88. Merge Sorted Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第八十八题 (1)题目 英文: Given two sorted integer a ...

  9. leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

最新文章

  1. Linux 并发服务器雏形总结
  2. 美国防部官员讨论量子科学、5G和定向能的发展
  3. Java字符串处理技巧
  4. Map的4种遍历方法
  5. IOS - UTF-8转码问题
  6. .NET Core开源行动:一键创建Excel Add-in
  7. springboot mybatis-plus 配置 yml 、druid 配置 yml 、mybatis-plus 代码生成
  8. 化工企业数据分析中心项目之采购模块分析
  9. mysql 创建用户命令-grant
  10. SPOJ SUBST1 New Distinct Substrings(后缀数组 本质不同子串个数)题解
  11. 天池比赛实践——阿里云安全恶意程序检测(基于机器学习算法XGBoost)
  12. 数格子算面积的方法_数格子估算面积与数格点估算面积
  13. Android通过MediaStore获取音乐文件信息的方法
  14. 【华为云服务之】在华为云ECS上安装Docker
  15. 云原生|Qunar 云原生容器化落地实践
  16. 【五、反向代理及其相关配置】
  17. 如何用python创建最简单的服务器
  18. android中gravity什么意思,浅谈android 中layout_gravity和gravity
  19. JAVA语言的优点及特性
  20. “人文素养老师”——马小平老师

热门文章

  1. Windows10 ls命令
  2. 华为手机如何连接无线打印服务器,惊呆了!华为Mate 20居然支持无线打印
  3. (ntdll.dll)处(位于 .exe 中)引发的异常: 0xC0000139: Entry Point Not Found
  4. 桥式整流电路工作原理
  5. FRM干货丨投行内部职位及证书要求全解读!
  6. 巨磁阻抗GMI磁传感器模组选型推荐
  7. Packager is not running at localhost:19001
  8. php取出字符串数字部分,php提取字符串中数字文字片段的方法大全 - 乖兔博客
  9. 抱抱脸(hugging face)教程-中文翻译-分享一个模型
  10. 付费的知识星球要过期了,python 教你怎么办