Single Number

https://leetcode.com/problems/single-number/

这是来自于leetcode上的一道题目。这里主要强调的是解决题目时候必须数学先行。题目虽然很简单,不假思考就可以给出几个很直观的答案,但是由于时间复杂度是O(nlogn)空间复杂度是O(n)

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(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size();i++)
        {
            if(i+1<nums.size())
              if(nums[i]==nums[i+1])
                 i++;
              else
                 return nums[i];
                 if(i+1==nums.size())
                 return nums[i];
        }
    }
};

第二种方法是采用unordered_map的方式进行存储

class Solution {
public:
    int singleNumber(vector<int>& nums) {
       unordered_map<int ,int>umap;
       for(int i=0;i<nums.size();i++)
         umap[nums[i]]++;
        for(auto it:umap)
          if(it.second==1)
            return it.first;
    }
};

第三种方法是由于因为只有一个数不重复,其他每个数都出现两次。那么进行异或操作之后就为0;

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

所以做题目时候,可以先把问题当成数学问题会怎么算,就是忽略了数据结构

如果能直接给出

result=f(已知条件)

这样的函数,那么问题就会简单很多!

时间: 2024-11-05 11:30:00

Single Number的相关文章

Configuring Single Number Reach on Cisco CUCM 7.1.3

Cisco CUCM allows you to configure single number reach (also known as mobility) so that a call destined for your desk phone's extension can ring on a number of other devices such as your cellphone, blackberry, home phone, etc.  This feature also allo

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only o

Single Number II

题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,1,2,3,3,3,2,2,4,1] return 4 Challenge One-pass, constant extra space. 链接地址 http://www.lintcode.com/en/problem/single-number-ii/

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循环

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only

LeetCode 136、137:Single Number I &amp; II

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? Single Number II Given an arr

LeetCode 笔记26 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si

137.Single Number II(法1排序法2STL容器map哈希法3位运算法4改进的位运算)

Given an array of integers, every element appears three timesexcept for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement itwithout using extra memory? HideTags Bit Manipulation #pragma once

Single Number II (17)

Given an array of integers, every element appears threetimes except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 这一次数字重复出现的次数是3了,不像是 single number中的2,不能

【LeetCode】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? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可