Osheep

时光不回头,当下最重要。

iOS 算法 简单的二分法

//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

二分查找法

NSArray *numberArray =@[@1,@3,@27,@36,@42,@70,@82];

NSIntegersearchNum =70;

NSIntegermid;

NSIntegermin =0;

NSIntegermax = [numberArray count] -1;

BOOLfound =NO;

while(min <= max) {

mid = (min + max)/2;

if(searchNum == [numberArray[mid] intValue]) {

NSLog(@”我们发现数量!它是===> %@”, numberArray[mid]);

found =YES;

break;

}elseif(searchNum < [numberArray[mid] integerValue]) {

max = mid -1;

}elseif(searchNum > [numberArray[mid] integerValue]) {

min = mid +1;

}

}

if(!found) {

NSLog(@”这个数字没有找到.”);

}

//链表反转(其实就是将数组里面的值翻转过来)

//百度过链表反转是这意思就用oc数组实现了下,不正确的地方可以指出一下

NSArray*array =@[@1,@2,@3,@4,@5];

NSMutableArray*mArr = [NSMutableArrayarray];

for(NSInteger i = array.count -1; i >=0; i –) {

[mArr addObject:array[i]];

}

NSLog(@”array ===> %@”,array);

NSLog(@”mArr  ===> %@”,mArr);

//结果:

array =1,2,3,4,5;

mArr =5,4,3,2,1;

谢谢!!!

点赞