大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~
Given a non-empty array of integers nums
, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Input: nums = [2,2,1]
Output: 1
Input: nums = [4,1,2,1,2]
Output: 4
Input: nums = [1]
Output: 1
1 <= nums.length <= 3 * 10^4
3 * 10^4 <= nums[i] <= 3 * 10^4
首先先简单的翻译一下题目
给一组阵列,要找出其中只出现过一次的数字,其余的每个数字只会出现两次。
作法大致上是这样
key
的值是1
。class Solution:
def singleNumber(self, nums: List[int]) -> int:
dict = {}
for num in nums:
if num not in dict:
dict[num] = 1
else:
dict[num] += 1
for key in dict:
if dict[key] == 1:
return key
int singleNumber(int* nums, int numsSize){
int ans = 0;
for (int index=0 ; index<numsSize ; index++) {
// printf("\nBefore: %p\n", ans);
ans ^= nums[index];
// printf("After: %p\n", ans);
}
return ans;
}
Python
C
大家明天见
<<: 【从实作学习ASP.NET Core】Day27 | 前台 | PayPal 订单付款 (2)
来聊聊rebase rebase本身就是两个字组成,「re+base」 实际在做的时候,我们大概可以...
从官网的攻略介绍来看,因为安全考量,所以平常都应该使用send_from_directory(),而...
参考 LeetCode Binary Search Summary 二分搜索法小结 的 python...
今天主要要来简单的使用NestedScrollView这个应用,与ScrollView相似,也与昨天...
如果你 tailwind 已经写一段时间了,相信你有时候也会想把因为 tailwind 语法而变的很...