Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LeetCode URL: https://leetcode.com/problems/missing-number/
- /**
- * Sum all the numbers and subtract the sum from n * (n+1) / 2
- *
- * Time Complexity: O(N)
- *
- * Space Complexity: O(1)
- *
- * N = Length of the input array.
- */
- class Solution {
- public int missingNumber(int[] nums) {
- if (nums == null || nums.length == 0) {
- return -1;
- }
- int sum = 0;
- int len = nums.length;
- for (int num : nums) {
- sum += num;
- }
- return len * (len + 1) / 2 - sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement