LeetCode编程练习 - Set Mismatch学习心得

52 篇文章 0 订阅
10 篇文章 0 订阅

题目:

    The set S originally contains numbers from 1 ton. But unfortunately, due to the data error, one of the numbers in the set got duplicated toanother number in the set, which results in repetition of one number and loss of another number.

    Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

    Example 1:

       Input: nums = [1,2,2,4]

       Output: [2,3]

    Note:

       1、The given array size will in the range [2,10000].

       2、The given array's numbers won't have any order.

    集合S最初包含从1到n的数字,但由于数据错误,集合中的一个数字被赋值到集合中的另一个数字,导致重复一个数字和另一个数字的丢失。给定一个数组nums,表示该错误之后的数据状态。先发现数字两次,然后找到丢失的数字,以数组的形式返回他们。


思路:

    先发现数字两次,然后找到丢失的数字,也就是说有最终输入两个变量并以数组的形式输出,初始化两个变量为0。由于数据丢失时,一个数字被赋值到了另一个数字,判断两个数字是否相同,若相同则是发生了数据丢失错误,然后判断是否发现数字两次以及是否丢失数字。

  

    运行结果不符,查看解决方案,与方案的思路相同,但对于书写上还是有些差异。

  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值