Leetcode-day1

學習筆記
Dec 22, 2020

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

陣列中 每個數字都會出現兩次除了某一數,找出那個數字

Follow up: Could you implement a solution with a linear runtime complexity and without using extra memory?

Example 1:

Input: nums = [2,2,1]
Output: 1

Example 2:

Input: nums = [4,1,2,1,2]
Output: 4

Example 3:

Input: nums = [1]
Output: 1

Code:

int singleNumber(int* nums, int numsSize){

for(int i=0;i<numsSize;i++){
int count=0;
//printf("%d\n",nums[i]);
for(int j=0;j<numsSize;j++){

if(nums[j]==nums[i]){
//num[0]=num[0]:num[1]:num[2]>>2
//num[1]=num[0]:num[1]:num[2]>>2
//num[2]=num[0]:num[1]:num[2]>>1
count++;
printf("%d\n",count);
}

}
if(count==1){
return nums[i];
}
}
return -1;
}

INPUT:

[2,2,1]

OUTPUT:

1

--

--