大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题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)
在资料输入输出原本Java提供了java.io套件来给开发者使用,不过都是面对Byte[]的操作,在...
角色情境 小明同时学会输入指令操作着终端机、 以及透过滑鼠操作着图像化介面的 Sourcetree ...
昨天已经把 struct 写好了,今天来呈现资料在手机画面上,在此之前可以先上网搜寻 API 说明文...
https://leetcode.com/problems/remove-nth-node-fro...
本篇与STM32相关性不大 会有这个章节其实是有原因的...有一天我请学长帮我看一下程序的时候,请我...