题目链接:https://leetcode.com/problems/missing-number/
题目:
Given an array containing n distinct numbers taken from 0,
, find the one that is missing from the array.
1, 2, ..., n
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?
题意:在给定包含n个数的数组中找出丢失的那个
分析:很简单,排下序,遍历一下即可
代码:
public class Solution { public int missingNumber(int[] nums) { int k = 0; int len = nums.length; Arrays.sort(nums); for(int i=0; i<nums.length; i++) { if(nums[i] != k) return k; k++; } if(k == len) { return k; } return 0; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-11 04:31:58