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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LEETCODE--Missing Number

2019-11-11 05:55:48
字體:
供稿:網(wǎng)友

Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? 方法一: 與single number思想相似 按位操作(使用XOR ^)

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

方法二: 先排序后尋找;

class Solution {public: int missingNumber(vector<int>& nums) { sort(nums.begin(), nums.end()); if(nums[0] == 1) return 0; int i = 0; for(; i < nums.size()-1; i++){ if(nums[i+1] - nums[i] != 1) return nums[i] + 1; } return nums[nums.size()-1] + 1; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 珠海市| 武义县| 灵宝市| 西乌珠穆沁旗| 灵丘县| 遵义市| 泽普县| 大埔县| 绍兴县| 韶山市| 新密市| 芦山县| 信宜市| 长沙县| 荣昌县| 克拉玛依市| 赣榆县| 台东市| 宁蒗| 安顺市| 内江市| 开江县| 聂拉木县| 望都县| 兴城市| 博湖县| 奈曼旗| 华蓥市| 如东县| 房山区| 博爱县| 顺昌县| 大关县| 象州县| 武义县| 庆阳市| 绥化市| 会东县| 阳曲县| 七台河市| 恩平市|