本文共 6108 字,大约阅读时间需要 20 分钟。
用C语言实现字符串的翻转
和计算一个View上的UIImageView的个数
,这两个算法题目,只给出代码,工程文件未做上传,请谅解。/*实现字符串翻转*/char *reverse_str(char *str) { if(NULL == str) { //字符串为空直接返回 return str; } char *begin; char *end; begin = end = str; while(*end != '\0') { //end指向字符串的末尾 end++; } --end; char temp; while(begin < end) { //取地址,交换两个字符 temp = *begin; *begin = *end; *end = temp; begin++; end--; } return str; //返回结果}void main() { char str[] = "kengdiedemianshiti"; printf(reverse_str(str));}
//Objective-C 冒泡排序+ (void)inputNeedArray:(NSMutableArray *)array{ //依次拿相邻的两个元素做比较 for (int i = 0; i < array.count; ++i) { for (int j = 0; j < array.count - 1; ++j) { if (array[j] < array[j + 1]) { [array exchangeObjectAtIndex:j withObjectAtIndex:j + 1]; } NSString * str = @""; for (NSNumber * value in array) { str = [str stringByAppendingString:[NSString stringWithFormat:@"%zd ",[value integerValue]]]; } NSLog(@"%@",str); } NSLog(@"------"); }}
//Objective-C 选择排序+ (void)inputNeedArray:(NSMutableArray *)array{ //拿第一个跟后面的挨个比较 for (int i = 0; i < array.count; i++) { for (int j = i + 1; j < array.count; j++) { if (array[i] < array[j]) { [array exchangeObjectAtIndex:i withObjectAtIndex:j]; } NSString * str = @""; for (NSNumber * value in array) { str = [str stringByAppendingString:[NSString stringWithFormat:@"%zd ",[value integerValue]]]; } NSLog(@"%@",str); } NSLog(@"------"); }}
//输入一个数组,返回去除包含"Foo"字符串的元素的一个数组//[@"A", @"FooB", @"C", @"FooH", @"FooI"+ (void)returnNeedArray:(NSMutableArray *)array { NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:1]; for (int i = 0; i < array.count; i++) { if (![array[i] hasPrefix:@"Foo"]) { [muArr addObject:array[i]]; } } NSLog(@"%@", muArr); }
+ (NSMutableArray *)combineArray:(NSArray *)arrayNumberOne otherArray:(NSArray *)arrayNumberTwo{ // NSArray *array1 = @[@1, @3, @5, @6,]; // NSArray *array2 = @[@2, @3, @4, @5, @7]; NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:1]; int i = 0; int j = 0; while (i < arrayNumberOne.count && j < arrayNumberTwo.count) { if (arrayNumberOne[i] < arrayNumberTwo[j]) { resultArray[i + j] = arrayNumberOne[i]; i++; } else { resultArray[i + j] = arrayNumberTwo[j]; j++; } } while (i < arrayNumberOne.count) { resultArray[i + j] = arrayNumberOne[i]; i++; } while (j < arrayNumberTwo.count) { resultArray[i + j] = arrayNumberTwo[j]; j++; } //去重 有序 NSOrderedSet *set = [NSMutableOrderedSet orderedSetWithArray:resultArray]; NSLog(@"%@", set.array); return resultArray;}
+ (void)prinfLogForGCD{ dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL); NSLog(@"1 --- %@", [NSThread currentThread]); dispatch_async(queue, ^{ NSLog(@"2 --- %@", [NSThread currentThread]); dispatch_sync(queue, ^{ NSLog(@"3 --- %@", [NSThread currentThread]); }); NSLog(@"4 --- %@", [NSThread currentThread]); }); NSLog(@"5 --- %@", [NSThread currentThread]);}
#import+ (void)printLogForObjcgetClassAndClass{ XXPrintObjectClass *obj = [XXPrintObjectClass new]; NSLog(@"instance :%p", obj); NSLog(@"class :%p", object_getClass(obj)); NSLog(@"meta class :%p", object_getClass(object_getClass(obj))); NSLog(@"root meta :%p", object_getClass(object_getClass(object_getClass(obj)))); NSLog(@"root meta's meta :%p", object_getClass(object_getClass(object_getClass(object_getClass(obj))))); NSLog(@"---------------------------------------------"); NSLog(@"class :%p", [obj class]); NSLog(@"meta class :%p", [[obj class] class]); NSLog(@"root meta :%p", [[[obj class] class] class]); NSLog(@"root meta's meta :%p", [[[[obj class] class] class] class]);}
- (NSInteger)returnUIImageViewNumber:(UIView *)view{ static NSInteger a = 0; for (int i = 0; i < view.subviews.count; i++) { if ([[NSString stringWithFormat:@"%@", [view.subviews[i] class]] isEqualToString:@"UIImageView"]) { a++; } else if (view.subviews[i].subviews.count != 0) { [self returnUIImageViewNumber:view.subviews[i]]; } }// NSLog(@"%d", a); return a;}
+ (void)checkElementFromArray:(NSArray *)array findNumber:(int)number{ BOOL isOrNot = NO; //判断二维数组是否为空 if (array.count != 0) { //总行数 int rows = (int)array.count; NSArray *arr = array[0]; if (arr.count != 0) { //总列数 int columns = (int)arr.count; //行 int row = 0; //列 int column = columns - 1; //最后一个元素 while (row < rows && column >= 0) { NSString *arrStr = [NSString stringWithFormat:@"%@", array[row][column]]; NSString *numStr = [NSString stringWithFormat:@"%d", number]; int arrNumber = [arrStr intValue]; int targetNumber = [numStr intValue]; if (arrNumber == targetNumber) { isOrNot = YES; break; } else if (arrNumber > targetNumber) { --column; } else { ++row; } } } } NSLog(@"%hhd", isOrNot);}
转载地址:http://trbfa.baihongyu.com/