Skip to content

Commit bdcab53

Browse files
committed
Add solution #268
1 parent 6c9d9fd commit bdcab53

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
2929
263|[Ugly Number](./0263-ugly-number.js)|Easy|
3030
264|[Ugly Number II](./0264-ugly-number-ii.js)|Medium|
31+
268|[Missing Number](./0268-missing-number.js)|Easy|
3132
283|[Move Zeroes](./0283-move-zeroes.js)|Easy|
3233
345|[Reverse Vowels of a String](./0345-reverse-vowels-of-a-string.js)|Easy|
3334
347|[Top K Frequent Elements](./0347-top-k-frequent-elements.js)|Medium|

solutions/0268-missing-number.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 268. Missing Number
3+
* https://leetcode.com/problems/missing-number/
4+
* Difficulty: Easy
5+
*
6+
* Given an array `nums` containing `n` distinct numbers in the range `[0, n]`,
7+
* return the only number in the range that is missing from the array.
8+
*
9+
* Follow up: Could you implement a solution using only `O(1)` extra space
10+
* complexity and `O(n)` runtime complexity?
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @return {number}
16+
*/
17+
var missingNumber = function(nums) {
18+
let result = 0;
19+
20+
for (let i = 0; i < nums.length; i++) {
21+
result += i + 1 - nums[i];
22+
}
23+
24+
return result;
25+
};

0 commit comments

Comments
 (0)