算法
一、字符串反转,前后指针向中间移动交换
/// @param cha <#cha description#>
void char_resverse(char* cha){
char* start = cha;
char* end = cha + strlen(cha) -1;
while (end > start) {
char temp = *start;
*(start++) = *end;
*(end--) = temp;
}
}
二、单向链表 头插法反序
/// @param head <#head description#>
struct Node* reverseList(struct Node* head){
struct Node *curr = head;
struct Node prev = NULL;
while (curr) {
struct Node next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
};
三、合并有序数组算法,思想为遍历比较下标值,然后把没比较完的放入结果的后面
+(NSMutableArray*)mergeArray{
NSArray *arrayA = @[@"1",@"3",@"5",@"7",@"9"];
NSArray *arrayB = @[@"2",@"4",@"6",@"8",@"10",@"11",@"12"];
int idxA = 0;
int idxB = 0;
NSMutableArray *results = [NSMutableArray array];
while (idxA < [arrayA count] && idxB < [arrayB count]) {
if([arrayA[idxA] intValue] > [arrayB[idxB] intValue]){
[results addObject:arrayB[idxB]];
idxB++;
}else{
[results addObject:arrayA[idxA]];
idxA++;
}
}
while (idxA < [arrayA count]) {
[results addObject:arrayB[idxA]];
idxA++;
}
while (idxB < [arrayB count]) {
[results addObject:arrayB[idxB]];
idxB++;
}
return results;
}
四、查找两个子视图的所有父视图(分别查找两个视图的所有父视图,然后保存到两个数组中,然后倒序遍历比较父视图,如果一样则放到一个新数组里,如果不一样则退出循环)
-(NSMutableArray)findCommonSuperView:(UIView)aView bVIew:(UIView)bVIew{
NSMutableArray *commonSuperViews = [NSMutableArray array];
NSMutableArray *aviewSuperViews = [NSMutableArray array];
NSMutableArray *bviewSuperViews = [NSMutableArray array];
//找出a、b视图的所有共同视图
UIView *tempView = aView.superView;
while(tempView){
[aviewSuperViews addObjct:tempView];
tempView = superView;
}
tempView = bView.superView;
while(tempView){
[bviewSuperViews addObjct:tempView];
tempView = superView;
}
//倒序遍历
int i = 0;
while(i<MIN(aviewSuperViews.count,bviewSuperViews.count)){//小于最小的
View *asuperView = aviewSuperViews[aviewSuperViews.count -i -1];
View *bsuperView = viewSuperViews[aviewSuperViews.count -i -1];
if (asuperView == bsuperView){
[commonSuperViews addObject:asuperView];
i++;
}else{
break;
}
return commonSuperViews;
}
}
五、求无序数组的中位数
利用快速排序进行左右分割,