LeetCode 268. Missing Number

<div class="question-title" style="box-sizing: border-box; padding-bottom: 10px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(238, 238, 238); color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; margin-top: 1em;"><h3 style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 0px; margin-bottom: 10px; font-size: 24px; display: inline-block;">268. Missing Number</h3>   <span class="fa fa-lg fa-star add-favor not-favor" data-toggle="popover" data-trigger="hover" data-placement="top" data-content="save to favorite" ng-click="detailCtrl.favor()" ng-class="{'favor': detailCtrl.is_favor, 'not-favor': !detailCtrl.is_favor}" data-original-title="" title="" style="box-sizing: border-box; display: inline-block; font-stretch: normal; line-height: 0.75em; font-family: FontAwesome; font-size: 1.33333em; text-rendering: auto; -webkit-font-smoothing: antialiased; vertical-align: -15%; color: grey; cursor: pointer; position: relative; top: -3px;"></span><a target=_blank class="pull-right btn btn-default" href="https://leetcode.com/problems/missing-number/submissions/" style="box-sizing: border-box; color: rgb(51, 51, 51); text-decoration: none; display: inline-block; padding: 6px 12px; margin-bottom: 0px; line-height: 1.42857; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-radius: 4px; float: right; background-image: none; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0px 0px; background-repeat: initial;">My Submissions</a><div class="pull-right btn-group right-pad" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle; float: right; margin-right: 15px;"><button type="button" class="btn btn-default active" style="margin: 0px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 14px; line-height: 1.42857; font-family: inherit; color: rgb(51, 51, 51); overflow: visible; cursor: pointer; padding: 6px 12px; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; border: 1px solid rgb(173, 173, 173); border-radius: 4px 0px 0px 4px; outline: 0px; box-shadow: rgba(0, 0, 0, 0.121569) 0px 3px 5px inset; position: relative; float: left; z-index: 2; background-image: none; background-color: rgb(230, 230, 230);">Question</button><button type="button" class="btn btn-default " data-toggle="tooltip" data-placement="top" title="" data-original-title="Not available yet, please check back later." style="margin: 0px 0px 0px -1px; font-style: inherit; font-variant: inherit; font-stretch: inherit; font-size: 14px; line-height: 1.42857; font-family: inherit; color: rgb(51, 51, 51); overflow: visible; cursor: pointer; padding: 6px 12px; white-space: nowrap; vertical-align: middle; -webkit-user-select: none; border: 1px solid rgb(204, 204, 204); border-radius: 0px 4px 4px 0px; position: relative; float: left; background-image: none; background-color: rgb(255, 255, 255);">Editorial Solution</button></div></div><div class="row col-md-12" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1140px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span class="total-ac text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); font-size: 13px; vertical-align: text-bottom;">Total Accepted: <span style="box-sizing: border-box; font-weight: 700;">50272</span></span> <span class="total-submit text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); margin-left: 15px; font-size: 13px; vertical-align: text-bottom;">Total Submissions: <span style="box-sizing: border-box; font-weight: 700;">124950</span></span> <span class="total-submit text-info" style="box-sizing: border-box; color: rgb(49, 112, 143); margin-left: 15px; font-size: 13px; vertical-align: text-bottom;">Difficulty: <span style="box-sizing: border-box; font-weight: 700;">Medium</span></span></div><div class="row" style="box-sizing: border-box; margin-right: -15px; margin-left: -15px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><div class="col-md-12" style="box-sizing: border-box; position: relative; min-height: 1px; padding-right: 15px; padding-left: 15px; float: left; width: 1170px;"><div class="question-content" style="box-sizing: border-box; margin-left: 20px; margin-top: 20px; margin-bottom: 5px; line-height: 30px; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding-left: 20px; padding-bottom: 2px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Given an array containing <span style="box-sizing: border-box;">n</span> distinct numbers taken from <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">0, 1, 2, ..., n</code>, find the one that is missing from the array.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">For example,<br style="box-sizing: border-box;" />Given <span style="box-sizing: border-box;">nums</span> = <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[0, 1, 3]</code> return <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">2</code>.</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><span style="box-sizing: border-box; font-weight: 700;">Note</span>:<br style="box-sizing: border-box;" />Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">看了好几遍题目才看懂</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">注意:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">1.初始化vector<int>a的时候,a的size为0。在每次用push_back添加完元素后,才能用下标访问</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">2.LeetCode在run code的时候越界有时候不报错。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">3.因为不小心在size()后多加了1导致nums[i]越界查了很久,所以小心a[nums[i]]嵌套时候的内外vector的边界</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">Runtime:40ms</p></div></div></div>
class Solution {
public:
  class Solution {
public:
    int missingNumber(vector<int>& nums) {
        
        int mising_num=0;
        vector<int> a;
        for(int i=0;i<nums.size()+1;i++)//初始化用push_back添加元素,而不是用a[i]=0,否则会报错
        {
            a.push_back(0);
        }
        for(int i=0;i<nums.size();i++)//记下已经在序列中的数,标记为1。因为不小心长度多加了1查了很久,Run code居然不报错==
        {
            a[nums[i]]=1;
            
        }
        for(int i=0;i<nums.size()+1;i++)
        {
            if(a[i]==0)
           { mising_num=i;
            break;}
        }
       // cout<<mising_num;
        return mising_num;
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值