帝赐尔,3158con,皇朝足球吧
linked url:
given a non-empty 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?
example 1:
input: [2,2,1] output: 1
example 2:
input: [4,1,2,1,2] output: 4
solution:
the method is xor option, principles is : 0 xor 0 = 0; 0 xor 1 = 1;any num xor itself = 0,so we pass the array and xor its elements all,so the result which is we want.
ac code follow:
1 class solution { 2 public: 3 int singlenumber(vector<int>& nums) { 4 int res = nums[0]; 5 for(int i = 1;i < nums.size();++i) 6 res = nums[i]^res; 7 return res; 8 } 9 };
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论