136.Single Number

Given an 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?

思路:

一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环,对数组中每个数在数组中遍历寻找与自己一样的数,而且不是本身。若找到,则将satatus设置为1,但在下一个数开始遍历前将status设置回0,若在遍历之后status仍为0,则表示在数组中没有与其相同的数,则输出该数即可。由于用到了for循环的嵌套,当时自己也感觉无法满足题目中要求的a linear runtime complexity,但并没有更好的办法,结果也恰恰是超时。

我的代码:

int singleNumber(int* nums, int numsSize) {
    for(int i=0,status=0;i<numsSize;i++)
    {
        for(int j=0;j<numsSize;j++)
        {
            if((*(nums+i)==*(nums+j)) && (i !=j))
            status=1;
        }
        if (status==0)
        return *(nums+i);
    }
}

参考代码1:

#include <stdio.h>
// This is classical interview question
// As we know, the same number XOR together will be 0,
// So, XOR all of numbers, the result is the number which only appears once.
int singleNumber(int A[], int n) {
    int s = 0;
    for(int i=0; i<n; i++){
        s = s^A[i];
    }
    return s;
}

int main()
{
    int a[]={1,1,2,2,3};
    printf("%d\n", singleNumber(a,5));
    return 0;
}

总结1:

参考代码的核心思路就是运用了<两个相同的数进行按位异或运算结果一定为0,一个数与0按位异或结果即为该数本身>,所以讲数组中所有数按位异或,留下的那个数即是那个single number。

实际提交代码:

class Solution {
public:
    int singleNumber(vector<int>& nums)
    {
        int s = 0;
        for (int i=0;i < nums.size();i++)
        {
            s = s ^ nums[i];
        }
    return s;
    }
};

总结1.1:

与参考代码略有不同,因为建了一个类,注意vector<int>&与int*的共同点,nums.size()可表示c++中数组元素的个数。

参考代码2、3:涉及更多c++的语法,明天吧c++语法过一遍后再来补充。

时间: 2024-12-24 05:50:52

136.Single Number的相关文章

【59】136. Single Number

136. Single Number Description Submission Solutions Add to List Total Accepted: 191020 Total Submissions: 360448 Difficulty: Easy Contributors: Admin Given an array of integers, every element appears twice except for one. Find that single one. Note:Y

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an 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? Subscribe to see which co

LeetCode 136. Single Number &amp; 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

136. Single Number - LeetCode

Question 136.?Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int singleNumber(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int i=0; i<nums.length; i++) { Integer count = countMap.get(nums

136. Single Number C++ 答案

136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int singleNumber(vector<int>& nums) { int s = 0; for(int i = 0; i < nums.size(); i++) { s = s ^ nums[i]; } return s; } }; 参考 LeetCode Problems' So

136. Single Number leetcode做题报告

Given an 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? class Solution { public: int singleNumber(vec

LeetCode 136 Single Number(只出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次.找出这个只出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度.你可以不用额外空间来实现它吗? 原文 Given an 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 i

LeetCode Problem 136:Single Number

描述:Given an 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? 题目要求O(n)时间复杂度,O(1)空间复杂度. 思路1:初步使用暴力搜索,遍历

LeetCode 136 Single Number(仅仅出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could yo