二分法
利用二分法查找某个数 并返回当下位置的下标
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int double_search ( int * nums, int taget , int numsSize)
{
int middle= 0 ;
int left= 0 , right= numsSize- 1 ;
while ( left< right)
{
middle = ( left+ right) / 2 ;
if ( nums[ middle] > taget)
right= middle - 1 ;
else if ( nums[ middle] < taget)
left= middle+ 1 ;
else if ( nums[ middle] == taget)
{
printf ( "%d" , middle) ;
return middle;
}
else return - 1 ;
}
}
int main ( )
{
int array[ 10 ] = { 2 , 3 , 5 , 7 , 10 , 11 , 13 , 17 , 18 , 19 } ;
double_search ( array, 20 , 10 ) ;
}
双指针法
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int doublepoint ( int * nums , int target, int numsSize)
{
int fastp= 0 , slowp= 0 ;
for ( fastp= 0 ; fastp< numsSize; fastp++ )
{
if ( target!= nums[ fastp] ) {
nums[ slowp] = nums[ fastp] ;
slowp++ ; }
printf ( "%d" , slowp) ;
}
return slowp;
}
int main ( )
{
int array[ 10 ] = { 2 , 3 , 4 , 2 , 5 , 9 , 7 , 2 , 3 , 4 } ;
doublepoint ( array, 3 , 10 ) ;
}
暴力求解 除去数组中值为某个数
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int removeit ( int * nums , int target, int numsSize)
{
int i= 0 , j= 0 ;
for ( i= 0 ; i< numsSize; i++ ) {
if ( nums[ i] == target)
for ( j= i+ 1 ; j< numsSize; j++ )
nums[ j- 1 ] = nums[ j] ;
}
numsSize-- ;
}
int main ( )
{
int array[ 10 ] = { 2 , 3 , 7 , 8 , 6 , 2 , 3 , 4 , 7 , 2 } ;
removeit ( array, 2 , 10 ) ;
for ( int k= 0 ; k< 7 ; k++ )
printf ( "%d " , array[ k] ) ;
}
快慢指针 对数组中的数 求平方 并排序
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int * doublepointsort ( int * nums, int numsSize )
{
int left = 0 , right= numsSize- 1 ;
int * ans= ( int * ) malloc ( sizeof ( int ) * numsSize) ;
int k= numsSize;
while ( left< right)
{
if ( nums[ left] * nums[ left] < nums[ right] * nums[ right] )
{
ans[ k] = nums[ right] * nums[ right] ;
k-- ;
right-- ;
}
else if ( nums[ left] * nums[ left] > nums[ right] * nums[ right] )
{ ans[ k] = nums[ left] * nums[ left] ;
k-- ;
left++ ;
}
if ( nums[ left] * nums[ left] == nums[ right] * nums[ right] )
{
ans[ k] = nums[ left] * nums[ left] ;
k-- ;
ans[ k] = nums[ right] * nums[ right] ;
k-- ;
left++ ;
right-- ;
}
}
for ( int m= numsSize; m> 0 ; m-- )
printf ( "%d" , ans[ m] ) ;
return ans;
}
int main ( )
{
int array[ 10 ] = { - 4 , - 5 , - 2 , 1 , 3 , 5 , 10 , 16 , 30 , 20 } ;
doublepointsort ( array, 10 ) ;
}
求最小子序列长度 通过滑动窗口 也类似双指针的方法
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int minlistnum ( int s, int * nums, int numsSize)
{
int sum= 0 , minlength= 0 ;
int i= 0 ;
int result= 100000 ;
for ( int j= 0 ; j< numsSize; j++ )
{
sum+= nums[ j] ;
while ( sum>= s)
{
minlength= j- i+ 1 ;
result= result < minlength ? result : minlength;
sum-= nums[ i] ;
i++ ;
}
}
printf ( "%d" , result) ;
}
int main ( )
{
int array[ 10 ] = { 1 , 2 , 3 , , 7 , 8 , 9 , 10 } ;
minlistnum ( 3 , array, 10 ) ;
}
螺旋数组
貌似还有点问题没解决
# include <stdio.h>
# include <stdlib.h>
int main ( )
{
int array[ 5 ] [ 5 ] = { 0 } ;
ljm ( array, 5 ) ;
}
int ljm ( int ( * nums) [ 5 ] , int n)
{
int loop= n/ 2 ;
int starx= 0 ;
int stary = 0 ;
int offset= 1 ;
int i, j;
int count= 1 ;
while ( loop-- )
{ i= starx;
j= stary;
for ( j= stary; j< stary+ n- offset; j++ )
{
nums[ i] [ j] = count++ ;
for ( i= starx; i< starx+ n- offset; i++ )
{ nums[ i] [ j] = count++ ;
printf ( "%d " , nums[ i] [ j] ) ;
}
for ( ; j> stary; j-- )
{
nums[ i] [ j] = count++ ; }
for ( ; i> starx; i-- ) ;
{
nums[ i] [ j] = count++ ;
}
starx++ ;
stary++ ;
offset+= 2 ;
}
} }