/*
* @lc app=leetcode.cn id=136 lang=c
*
* [136] 只出现一次的数字
*
* https://leetcode-cn.com/problems/single-number/description/
*
* algorithms
* Easy (59.12%)
* Total Accepted: 48.6K
* Total Submissions: 82.2K
* Testcase Example: '[2,2,1]'
*
* 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
*
* 说明:
*
* 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
*
* 示例 1:
*
* 输入: [2,2,1]
* 输出: 1
*
*
* 示例 2:
*
* 输入: [4,1,2,1,2]
* 输出: 4
*
*/
int singleNumber(int* nums, int numsSize) {
int i,j;
int res;
for(i=;i<numsSize;i++){
for(j=i+;j<numsSize;j++){
if(nums[j]==nums[i]){
nums[i]=;
nums[j]=;
break;
}
}
}
for(i=;i<numsSize;i++){
if(nums[i]!=){
res = nums[i];
}
}
return res;
}

这是自己的思路,耗时比较大。。。而且也比较侥幸。

把相同的数都变为0,最后那个不为0的数输出就是结果。(看来测试用例没有0这一项。。。)

参考别人的算法。(看到后惊了)

/*
* @lc app=leetcode.cn id=136 lang=c
*
* [136] 只出现一次的数字
*
* https://leetcode-cn.com/problems/single-number/description/
*
* algorithms
* Easy (59.12%)
* Total Accepted: 48.6K
* Total Submissions: 82.2K
* Testcase Example: '[2,2,1]'
*
* 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
*
* 说明:
*
* 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
*
* 示例 1:
*
* 输入: [2,2,1]
* 输出: 1
*
*
* 示例 2:
*
* 输入: [4,1,2,1,2]
* 输出: 4
*
*/
int singleNumber(int* nums, int numsSize) {
int result = nums[];
for (int i = ; i < numsSize; i++)
{
result ^= nums[i]; }
return result;
}

运算符(^)它的作用是两个数的二进制中的每一个比特位,形同为0,不同则为1.即(1 ^ 1 = 0,50 ^ 50 = 0,0 ^ 100 = 100),那如果将这组数据每一位都^则最后的出的那个结果就是只出现一次的那个数。

真的

Leecode刷题之旅-C语言/python-136只出现一次的数字的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  10. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

随机推荐

  1. java面试题之----super和this

    super和this的异同: super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句) supe ...

  2. 好记性不如烂笔头-nginx安装环境与Linux安装ftp组件

    Nginx安装环境 1. Nginx安装环境 Nginx是C语言开发,建议在linux上运行,我参加工作这些年来一直使用Linux发行版之一的 Centos作为安装环境. 1.1 gcc 安装Ngin ...

  3. python 后台服务

    centos 6x #!/bin/sh # chkconfig: 123456 90 10 # TTS Server for Speech Synthesis # workdir=/etc/speec ...

  4. HTTP Strict Transport Security

    HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式. 作用 一个网站接受一个HTTP的 ...

  5. Extjs 自动列宽

    //auto column width Ext.grid.Panel.prototype.viewConfig = { listeners: { refresh: function (dataview ...

  6. 【Spring实战】—— 4 Spring中bean的init和destroy方法讲解

    本篇文章主要介绍了在spring中通过配置init-method和destroy-method方法来实现Bean的初始化和销毁时附加的操作. 在java中,我们并不需要去管理内存或者变量,而在C或C+ ...

  7. 一对多sql

    <!-- 分页查询派货成本 --> <select id="queryCostRegionPriceBycondtion" parameterMap=" ...

  8. x64 分页机制——虚拟地址到物理地址寻址

    原博客:http://www.cnblogs.com/lanrenxinxin/p/4735027.html 详细的理论讲解都在上面 下面说的是通过windbg手动进行寻址,深入理解 x64: 实践: ...

  9. 如何在ubuntu上安装virtualbox的driver module vboxdrv

    干净的ubuntu安装完毕之后是没有vboxdrv这个driver module的. 新建一个folder jerry_virtualbox: 使用wget下载virtualbox安装包:https: ...

  10. OC property(声明)

    @interface Student : NSObject { int _age; int _no; float _height; } // 当编译器遇到@property时,会自动展开成getter ...