国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 曲阜市| 明光市| 阳泉市| 大英县| 合阳县| 云梦县| 闽清县| 武定县| 霞浦县| 措勤县| 稷山县| 青阳县| 昭觉县| 霍邱县| 东海县| 泗水县| 彰化市| 通道| 阳高县| 沙洋县| 静乐县| 南昌县| 黄骅市| 内江市| 炎陵县| 宜川县| 泰州市| 康乐县| 花莲县| 马边| 民丰县| 革吉县| 临邑县| 新建县| 山丹县| 涪陵区| 阳东县| 宾川县| 遂宁市| 竹溪县| 中西区|