码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode-136. 只出现一次的数字

时间:2020-07-08 15:07:58      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:problem   style   联系   时间复杂度   输出   出现   链接   tps   tor   

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

示例 1:

输入: [2,2,1]
输出: 1
示例 2:

输入: [4,1,2,1,2]
输出: 4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/single-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

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

 

Leetcode-136. 只出现一次的数字

标签:problem   style   联系   时间复杂度   输出   出现   链接   tps   tor   

原文地址:https://www.cnblogs.com/Amaris-diana/p/13266676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!