• [LeetCode]55. 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 companies asked this question

     
    这题一个问题是题目没有限定输入数组的情况,同时也没有规定在输入数组不满足要求的情况下的输出。因此在考虑输入数组不满足“every element appears twice except for one”的情况下输出定义为什么值不清楚。
    解法1:数组排序,再比较相邻数是否相等即可。时间复杂度O(nlogn),空间复杂度O(n)。(快排)
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            for(int i = 1; i < nums.size() - 1; ++i)
                if(nums[i - 1] != nums[i] && nums[i] != nums[i + 1]) return nums[i];
            return nums.size() == 0 ? 0 : nums[0]; //任意输出0或者第一个值
        }
    };
    解法2:扫描一遍数组,将其映射到Map中;再扫描一遍Map,找到其中出现了两次的数。时间复杂度和空间复杂度都是O(n)。
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            map<int, int> m;
            for(int i = 0; i < nums.size(); ++i)
                ++m[nums[i]];
            for(map<int, int>::iterator iter = m.begin(); iter != m.end(); ++iter)
            {
                if(iter->second == 1) return iter->first;
            }
            return nums.size() == 0 ? 0 : nums[0]; //任意输出0或者第一个值
        }
    };

    解法3:考虑题目要求的O(n)时间复杂度和O(1)空间复杂度,则不能使用常规排序法或者Map。题目的Hide Tags提示使用位操作。注意到两个相同的数异或后结果是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;
        }
    };
  • 相关阅读:
    8VC Venture Cup 2016
    8VC Venture Cup 2016
    8VC Venture Cup 2016
    HDU 5627 Clarke and MST &意义下最大生成树 贪心
    HDU 5626 Clarke and points 平面两点曼哈顿最远距离
    《花开物语》观后感
    Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp
    Codeforces Round #146 (Div. 1) A. LCM Challenge 水题
    Educational Codeforces Round 7 E. Ants in Leaves 贪心
    Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
  • 原文地址:https://www.cnblogs.com/aprilcheny/p/4932505.html
Copyright © 2020-2023  润新知