268. 丢失的数字
为保证权益,题目请参考 268. 丢失的数字(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
static int missingNumber(vector<int> &nums) {
int t = 0;
for (int i = 0; i <= nums.size(); i++) {
t = t ^ i;
}
for (int i = 0; i < nums.size(); i++) {
t = t ^ nums[i];
}
return t;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Python
python
# 268. 丢失的数字
# https://leetcode-cn.com/problems/missing-number/
################################################################################
# from typing import List
# from functools import reduce
# class Solution:
# def missingNumber(self, nums: List[int]) -> int:
# t = reduce(lambda x, y: x ^ y, list(range(len(nums) + 1)), 0)
# ans = reduce(lambda x, y: x ^ y, nums, t)
# return ans
################################################################################
from typing import List
from functools import reduce
class Solution:
def missingNumber(self, nums: List[int]) -> int:
t = reduce(lambda x, y: x + y, list(range(len(nums) + 1)), 0)
ans = reduce(lambda x, y: x - y, nums, t)
return ans
################################################################################
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31