diff --git "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" index 844b5cb6ff0f178fe304042ad519e56cf261fdc0..b45bfd2705dbb3aa058d2a90b74fb8d108def7fd 100644 --- "a/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" +++ "b/\351\253\230\351\242\221\351\235\242\350\257\225\347\263\273\345\210\227/\346\266\210\345\244\261\347\232\204\345\205\203\347\264\240.md" @@ -131,5 +131,33 @@ public int missingNumber(int[] nums) {

+======其他语言代码====== -======其他语言代码====== \ No newline at end of file + + +```python +def missingNumber(self, nums: List[int]) -> int: + #思路1,位运算 + res = len(nums) + for i,num in enumerate(nums): + res ^= i^num + return res +``` + +```python +def missingNumber(self, nums: List[int]) -> int: + #思路2,求和 + n = len(nums) + return n*(n+1)//2-sum(nums) +``` + +```python +def missingNumber(self, nums: List[int]) -> int: + #思路3,防止整形溢出的优化 + res = len(nums) + for i,num in enumerate(nums): + res+=i-num + return res +``` + +事实上,在python3中不存在整数溢出的问题(只要内存放得下),思路3的优化提升并不大,不过看上去有内味了哈... \ No newline at end of file