Skip to content

Commit 1bf601f

Browse files
committed
myfirst commit
1 parent c0bff5d commit 1bf601f

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include<math.h>
3+
int main()
4+
{
5+
int nums[4]={1,3,3,4};
6+
int numSize=4;
7+
int removeDuplicates(int* nums, int numsSize);
8+
9+
int result=removeDuplicates(nums,numSize);
10+
printf("result= %d",result);
11+
}
12+
13+
/**Remove Duplicates from Sorted Array II**/
14+
int removeDuplicates(int* nums, int numsSize) {
15+
int swap(int *a,int *b);
16+
int pre=nums[0];
17+
int count=1;
18+
int taint=1;
19+
if(numsSize==0) return 0;
20+
if(numsSize==1) return 1;
21+
if(numsSize==2) return 2;
22+
for(int i=1;i<numsSize;i++){
23+
if(nums[i]==pre){
24+
pre=nums[i];
25+
count++;
26+
if(count<3){
27+
if(taint>1){
28+
swap(&nums[taint],&nums[i]);
29+
taint++;
30+
}
31+
}else{
32+
if(taint<2){
33+
taint=i;
34+
}
35+
}
36+
continue;
37+
}else if(nums[i]!=pre){
38+
pre=nums[i];
39+
count=1;
40+
if(taint>1){
41+
swap(&nums[taint],&nums[i]);
42+
taint++;
43+
}
44+
45+
}
46+
47+
}
48+
if(taint>1)
49+
return taint;
50+
else
51+
return numsSize;
52+
}
53+
54+
int swap(int *a,int *b)
55+
{
56+
int temp;
57+
temp=*a;
58+
*a=*b;
59+
*b=temp;
60+
return 0;
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include<math.h>
3+
int main()
4+
{
5+
int nums[4]={-1,3,4,7};
6+
int numSize=4;
7+
bool search(int* nums, int numsSize, int target);
8+
//int result=search(nums,numSize,2);
9+
printf("result= %d",result);
10+
}
11+
12+
13+
/**Search in Rotated Sorted Array II**/
14+
bool search(int* nums, int numsSize, int target) {
15+
//int head=0, tail=numsSize-1,mid=0;
16+
bool binarySearch(int* nums,int h,int t,int target);
17+
return binarySearch(nums,0,numsSize-1,target);
18+
}
19+
20+
bool binarySearch(int* nums,int h,int t,int target){
21+
int head=h, tail=t,mid=0;
22+
while(head<=tail){
23+
mid=(head+tail+1)/2;
24+
//check
25+
if(mid==head||mid==tail){
26+
if(target==nums[head]){
27+
return true;
28+
}else if(target==nums[tail]){
29+
return true;
30+
}else return false;
31+
}
32+
33+
if(target==nums[head]) return true;
34+
else if(target==nums[tail]) return true;
35+
else if(target>nums[head]){
36+
if(nums[mid]>target){
37+
tail=mid;
38+
// return binarySearch(nums,head,mid,target);
39+
}else if(nums[mid]<target){
40+
if(nums[mid]<nums[head]){
41+
tail=mid;
42+
}else if(nums[mid]>nums[head]){
43+
head=mid;
44+
}else
45+
return binarySearch(nums,head,mid,target)+binarySearch(nums,mid+1,tail,target);
46+
}else return true;
47+
}else if(target<nums[tail]){
48+
if(nums[mid]<target){
49+
head=mid;
50+
// return binarySearch(nums,mid,tail,target);
51+
}else if(nums[mid]>target){
52+
if(nums[mid]>nums[tail]){
53+
head=mid;
54+
}else if(nums[mid]<nums[tail]){
55+
tail=mid;
56+
}else
57+
return binarySearch(nums,head,mid,target)+binarySearch(nums,mid+1,tail,target);
58+
}else
59+
return true;
60+
}
61+
else
62+
return false;
63+
64+
65+
}
66+
}
67+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include<math.h>
4+
int main()
5+
{
6+
int nums[4]={-4,2,0,1};
7+
int numSize=4;
8+
int threeSumClosest(int* nums, int numsSize, int target);
9+
int result=threeSumClosest(nums,numSize,2);
10+
printf("result= %d",result);
11+
}
12+
13+
/**3Sum Closest**/
14+
int threeSumClosest(int* nums, int numsSize, int target) {
15+
void quicksort(int* s, int l, int r);//快排
16+
int twoSumClosest(int* nums,int numsSize, int target);//子问题 2sum
17+
int* temp=(int*)malloc(sizeof(int) * (numsSize-1)); //new int[numsSize-1];
18+
19+
int start=0;
20+
int* interResult=(int*)malloc(sizeof(int) * (numsSize)); //new int[numsSize];
21+
for(int i=0;i<numsSize;i++){
22+
start=0;
23+
for(int j=0;j<numsSize;j++){
24+
if(i==j) continue;
25+
temp[start++]=nums[j];
26+
}
27+
quicksort(temp,0,numsSize-2);
28+
interResult[i]=nums[i]+twoSumClosest(temp,numsSize-1,target);
29+
}
30+
int result=interResult[0];
31+
for(int k=1;k<numsSize;k++){
32+
if(abs(result-target)>abs(interResult[k]-target))
33+
result=interResult[k];
34+
}
35+
return result;
36+
}
37+
38+
39+
40+
int twoSumClosest(int* nums,int numsSize, int target){
41+
int head=0,tail=numsSize-1;
42+
int distance; //当前head 和 tail的值与target的差值
43+
int tempSum;
44+
int curNearest=nums[head]+nums[tail];//pow(2.0,31.0)-1; //当前与target值最接近的两个数和
45+
while(head<tail){
46+
tempSum=nums[head]+nums[tail];
47+
distance=tempSum-target;
48+
if(distance<0){
49+
head++;
50+
}else if(distance>0){
51+
tail--;
52+
}else
53+
return target;
54+
if(abs(distance)<abs(curNearest-target))
55+
curNearest=tempSum;
56+
}
57+
return curNearest;
58+
}
59+
60+
//网上找的快速排序算法
61+
void quicksort (int* a, int lo, int hi)
62+
{
63+
// lo is the lower index, hi is the upper index
64+
// of the region of array a that is to be sorted
65+
int i=lo, j=hi, h;
66+
67+
// comparison element x
68+
int x=a[(lo+hi)/2];
69+
70+
// partition
71+
do
72+
{
73+
while (a[i]<x) i++;
74+
while (a[j]>x) j--;
75+
if (i<=j)
76+
{
77+
h=a[i]; a[i]=a[j]; a[j]=h;
78+
i++; j--;
79+
}
80+
} while (i<=j);
81+
82+
// recursion
83+
if (lo<j) quicksort(a, lo, j);
84+
if (i<hi) quicksort(a, i, hi);
85+
}
86+
/** Trapping Rain Water **/
87+
还未完成
88+
89+
/** Word Search **/
90+
还未完成

0 commit comments

Comments
 (0)