Last active
December 9, 2015 20:58
-
-
Save lbj96347/4327054 to your computer and use it in GitHub Desktop.
Objective-C zip 函数 用于二维数组重排
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** * 把一个二维数组做坐标转换。例; | |
| 原数组: | |
| arry = [[a, b, c], | |
| [e, f, g] | |
| ] | |
| data = zip(array) | |
| 变型后: | |
| data = [[a, e], | |
| [b, f], | |
| [c, g] | |
| ] | |
| 注意:请确保原数组的每个子数组的元素数量是一致的。 | |
| */ | |
| NSArray * zip(NSArray *arrays){ | |
| NSInteger maxSize = 0; | |
| for (NSArray *array in arrays) { | |
| if (array.count > maxSize) { | |
| maxSize = array.count; | |
| } | |
| } | |
| NSMutableArray *result = [NSMutableArray arrayWithCapacity:maxSize]; | |
| for (NSInteger index = 0; index < maxSize; index ++) { | |
| NSMutableArray *row = nil; | |
| if (result.count > index) { | |
| row = [result objectAtIndex:index]; | |
| }else{ | |
| row = [NSMutableArray arrayWithCapacity:arrays.count]; | |
| } | |
| for (NSArray *originRow in arrays) { | |
| [row addObject:[originRow objectAtIndex:index]]; | |
| } | |
| [result addObject:row]; | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment