[LeetCode]Missing Number
发布日期:2021-11-22 02:48:54 浏览次数:4 分类:技术文章

本文共 569 字,大约阅读时间需要 1 分钟。

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?

Credits:

Special thanks to  for adding this problem and creating all test cases.

参考了别人的代码

code:

public class Solution268 {	 public int missingNumber(int[] nums) {	 		 int sum=0;		 for(int i=0; i<=nums.length; i++){			 sum+=i;		 }		 for(int i:nums)			 sum -=i;		 return sum;	 }}

转载地址:https://blog.csdn.net/zxdfc/article/details/48711667 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:[LeetCode]Generate Parentheses
下一篇:[LeetCode]String to Integer (atoi)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月02日 13时07分18秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章