首页 > 学院 > 开发设计 > 正文

Leetcode 136. Single Number

2019-11-14 09:54:31
字体:
来源:转载
供稿:网友

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?

s思路: 1. 这道题遇到过! 不用内存,那就是一直做xor运算,相同的数xor等于0,最后只剩下the single number. 2. 这道题就是利用两个数异或的性质,基本也就是一道数学题!

class Solution {public: int singleNumber(vector<int>& nums) { int res=0; for(int i=0;i<nums.size();i++){ res^=nums[i]; } return res; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表