-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumber.c
More file actions
75 lines (62 loc) · 1.32 KB
/
SingleNumber.c
File metadata and controls
75 lines (62 loc) · 1.32 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
问题描述:
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
解题思路:
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
void quickSort(int* nums,int low,int high){
int x=low+1;
int y=high;
while(x<=y){
if(nums[x]>nums[low] && nums[y]<nums[low]){
int temp=nums[x];
nums[x]=nums[y];
nums[y]=temp;
}
if(nums[x]<=nums[low]){
x++;
}
if(nums[y]>=nums[low]){
y--;
}
}
int temp=nums[low];
nums[low]=nums[y];
nums[y]=temp;
if(y-1>low){
quickSort(nums,low,y-1);
}
if(x<high){
quickSort(nums,x,high);
}
}
int singleNumber(int* nums, int numsSize){
quickSort(nums,0,numsSize-1);
for(int i=0;i<numsSize;){
if(i==numsSize-1 || nums[i]!=nums[i+1]){
return nums[i];
}else{
i+=2;
}
}
return 0;
}
int main(){
int m[]={4,1,2,1,2};
quickSort(m,0,4);
for(int i=0;i<5;i++){
printf("%d\t",m[i]);
}
return 0;
}