/*
* @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. jQuery Event.stopImmediatePropagation() 函数详解

    stopImmediatePropagation()函数用于阻止剩余的事件处理函数的执行,并防止当前事件在DOM树上冒泡. 根据DOM事件流机制,在元素上触发的大多数事件都会冒泡传递到该元素的所有祖辈 ...

  2. Hadoop fs命令(转)

    最近使用hive做一些etl工作,除了日常sql的编写,了解hadoop及hive的一些底层原理性质的东西包括调优非常有必要,一次hive调优就把原来的零散文件做了合并.首先记下hadoop常用的命令 ...

  3. 框架页面jquery装载

  4. git相关操作(githug)

    Level 15  restructure 关卡描述 你添加了一些文件到你的仓库,但现在知道你的项目需要进行调整.创建一个新的文件夹命名为“src”,使用git将所有的".html" ...

  5. IOS MapKit框架的使用(专门用于地图显示)

    ● MapKit框架使用前提 ● 导入框架 ● 导入主头文件#import <MapKit/MapKit.h>   ●  MapKit框架使用须知 ●  MapKit框架中所有数据类型的前 ...

  6. webstorn中的vue文件识别es6代码

    webstorn中的vue文件识别es6代码 1.webstorm中es6语法报错,解决方法: 打开 Settings => Languages & Frameworks => J ...

  7. 百练oj 2815:城堡问题(dfs)

    传送门: http://bailian.openjudge.cn/practice/2815 2815:城堡问题 查看 提交 统计 提示 提问 总时间限制: 1000ms 内存限制: 65536kB ...

  8. PAT1087. All Roads Lead to Rome

    PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...

  9. socket 代码实例

    ​ 1. TCP SOCKET 客户端: #!/usr/bin/env python # -*-coding:utf-8 -*- import socket HOST = 'localhost' PO ...

  10. C++/C 内存大小

    #include <stdio.h> struct test1{    char a1;    int a2;    double a3;}; struct test2{    char ...