国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

leecode 解題總結:136. Single Number

2019-11-08 03:02:31
字體:
來源:轉載
供稿:網(wǎng)友
#include <iostream>#include <stdio.h>#include <vector>#include <string>using namespace std;/*問題: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?分析:一個數(shù)組中,每個元素都出現(xiàn)了兩次,只有一個元素不是。這明顯是用異或來做的,根據(jù)b=a^a^b輸入:31 1 2輸出:2*/class Solution {public:    int singleNumber(vector<int>& nums) {        if(nums.empty())		{			return 0;		}		int size = nums.size();		int result = 0;		for(int i = 0 ; i < size ; i++)		{			result ^= nums.at(i);		}		return result;    }};void PRint(vector<int>& result){	if(result.empty())	{		cout << "no result" << endl;		return;	}	int size = result.size();	for(int i = 0 ; i < size ; i++)	{		cout << result.at(i) << " " ;	}	cout << endl;}void process(){	 vector<int> nums;	 int value;	 int num;	 Solution solution;	 vector<int> result;	 while(cin >> num )	 {		 nums.clear();		 for(int i = 0 ; i < num ; i++)		 {			 cin >> value;			 nums.push_back(value);		 }		 num = solution.singleNumber(nums);		 cout << num << endl;	 }}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇平县| 乌拉特后旗| 彭州市| 梅河口市| 手游| 哈巴河县| 杭锦旗| 上犹县| 稷山县| 邹城市| 河间市| 金沙县| 武隆县| 岳阳县| 古田县| 衢州市| 阿鲁科尔沁旗| 慈利县| 赣州市| 洞头县| 宜章县| 屏南县| 天柱县| 肥乡县| 乐山市| 南投市| 唐海县| 吉安县| 富锦市| 五大连池市| 彭州市| 岳阳市| 诸城市| 尚志市| 岑巩县| 乐都县| 饶阳县| 肇州县| 三明市| 上高县| 深泽县|