Skip to content

Commit b0676d9

Browse files
committed
solve 268.missing-number
1 parent faf27f1 commit b0676d9

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

vscode/268.missing-number.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode id=268 lang=java
3+
*
4+
* [268] Missing Number
5+
*
6+
* https://leetcode.com/problems/missing-number/description/
7+
*
8+
* algorithms
9+
* Easy (47.56%)
10+
* Total Accepted: 263.6K
11+
* Total Submissions: 549.2K
12+
* Testcase Example: '[3,0,1]'
13+
*
14+
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
15+
* find the one that is missing from the array.
16+
*
17+
* Example 1:
18+
*
19+
*
20+
* Input: [3,0,1]
21+
* Output: 2
22+
*
23+
*
24+
* Example 2:
25+
*
26+
*
27+
* Input: [9,6,4,2,3,5,7,0,1]
28+
* Output: 8
29+
*
30+
*
31+
* Note:
32+
* Your algorithm should run in linear runtime complexity. Could you implement
33+
* it using only constant extra space complexity?
34+
*/
35+
class Solution {
36+
public int missingNumber(int[] nums) {
37+
int n = nums.length;
38+
int result = n;
39+
for (int i = 0; i < n; i++) {
40+
result = result ^ i ^ nums[i];
41+
}
42+
return result;
43+
}
44+
}
45+

0 commit comments

Comments
 (0)