首页 > 学院 > 开发设计 > 正文

268. Missing Number

2019-11-06 06:01:26
字体:
来源:转载
供稿:网友

题目摘要 给定一个数组,包含从0, 1, 2, ..., nn个唯一数字,从数组中找到缺少的那个。 例如, nums = [0, 1, 3],返回2

解法 1. 计算0到n的总和account = (0 + n) * (n + 1) / 2,计算数组总和sum,返回account - sum 2. 异或所有的索引和数据,返回

注意 1. 注意第一种解法中计算account是n与length的关系

可问问题

原题 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.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表