├── ArrayFullArrange ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m ├── InsertSortDemo ├── InsertSortDemo.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcuserdata │ │ │ └── cs.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── cs.xcuserdatad │ │ └── xcschemes │ │ └── xcschememanagement.plist └── InsertSortDemo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── ViewController.h │ ├── ViewController.m │ └── main.m ├── OC_算法_1 ├── OC_算法.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcuserdata │ │ │ └── cs.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── cs.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ ├── OC_算法.xcscheme │ │ └── xcschememanagement.plist └── OC_算法 │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Base │ ├── BinaryTreeNode.h │ ├── BinaryTreeNode.m │ ├── HashMap.h │ ├── HashMap.m │ ├── LinkedArray.h │ ├── LinkedArray.m │ ├── ListNode.h │ ├── ListNode.m │ ├── Stack.h │ ├── Stack.m │ ├── TreeNode.h │ └── TreeNode.m │ ├── Category │ ├── NSArray+Extension.h │ ├── NSArray+Extension.m │ ├── NSString+Extension.h │ └── NSString+Extension.m │ ├── Info.plist │ ├── PrefixHeader.pch │ ├── ViewController.h │ ├── ViewController.m │ ├── main.m │ └── 算法 │ ├── 1-10 │ ├── 03-repeatNumber.h │ ├── 03-repeatNumber.m │ ├── Fibonacci.h │ ├── Fibonacci.m │ ├── PrintListFromTailToHead_06.h │ ├── PrintListFromTailToHead_06.m │ ├── ReConstructBinaryTree_07.h │ ├── ReConstructBinaryTree_07.m │ ├── ReplaceBlank_05.h │ ├── ReplaceBlank_05.m │ ├── TwoDimensionalArrayFind_04.h │ ├── TwoDimensionalArrayFind_04.m │ ├── TwoStackQueue.h │ └── TwoStackQueue.m │ ├── 11-20 │ ├── DeleteNode_18.h │ ├── DeleteNode_18.m │ ├── IsNumeric_20.h │ ├── IsNumeric_20.m │ ├── MatrixHasPath.h │ ├── MatrixHasPath.m │ ├── MinNumberInRotateArray.h │ ├── MinNumberInRotateArray.m │ ├── MovingCount_13.h │ ├── MovingCount_13.m │ ├── NumberOfOne_15.h │ ├── NumberOfOne_15.m │ ├── RegularExpressionMath_19.h │ ├── RegularExpressionMath_19.m │ ├── power_16.h │ ├── power_16.m │ ├── printOneToMaxOfNDigits_17.h │ └── printOneToMaxOfNDigits_17.m │ ├── 21-30 │ ├── EntryNodeOfLoop_23.h │ ├── EntryNodeOfLoop_23.m │ ├── FindKthToTail_22.h │ ├── FindKthToTail_22.m │ ├── HasSubTree_26.h │ ├── HasSubTree_26.m │ ├── IsSymmetrical_28.h │ ├── IsSymmetrical_28.m │ ├── MergeTwoListNode_25.h │ ├── MergeTwoListNode_25.m │ ├── MirrorTree_27.h │ ├── MirrorTree_27.m │ ├── PrintMatrixClockwise_29.h │ ├── PrintMatrixClockwise_29.m │ ├── ReverseList_24.h │ ├── ReverseList_24.m │ ├── reOrderArray_21.h │ └── reOrderArray_21.m │ └── 31-40 │ ├── Backtracking_34.h │ ├── Backtracking_34.m │ ├── FindKthSmallest_40.h │ ├── FindKthSmallest_40.m │ ├── InOrder_36.h │ ├── InOrder_36.m │ ├── IsPopOrder_31.h │ ├── IsPopOrder_31.m │ ├── MoreThanHalfNumSolution_39.h │ ├── MoreThanHalfNumSolution_39.m │ ├── Permutation_38.h │ ├── Permutation_38.m │ ├── PrintFromTopToBottom_32.h │ ├── PrintFromTopToBottom_32.m │ ├── RandomListNode_35.h │ ├── RandomListNode_35.m │ ├── Serializer_37.h │ ├── Serializer_37.m │ ├── VerifySquenceOfBST_33.h │ └── VerifySquenceOfBST_33.m └── README.md /ArrayFullArrange/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // ArrayFullArrange 4 | // 5 | // Created by cs on 2019/3/30. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /ArrayFullArrange/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // ArrayFullArrange 4 | // 5 | // Created by cs on 2019/3/30. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /ArrayFullArrange/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /ArrayFullArrange/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /ArrayFullArrange/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /ArrayFullArrange/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /ArrayFullArrange/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /ArrayFullArrange/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // ArrayFullArrange 4 | // 5 | // Created by cs on 2019/3/30. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /ArrayFullArrange/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // ArrayFullArrange 4 | // 5 | // Created by cs on 2019/3/30. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | static int sum = 0; // 全排列个数 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view, typically from a nib. 22 | 23 | NSMutableArray *arrM = [NSMutableArray arrayWithObjects:@1,@2,@2, nil]; 24 | [self permutation:arrM prefixIndex:0 number:(int)arrM.count]; 25 | } 26 | 27 | // 递归实现数组全排列 28 | // prefixIndex - 表示前缀的位置 29 | // number - 要排列的数目 30 | - (void)permutation:(NSMutableArray *)arrayM prefixIndex:(int)prefixIndex number:(int)number { 31 | if (prefixIndex == number - 1) { // 前缀是最后一个位置,此时打印排列数 32 | // 打印数组结果 33 | sum += 1; 34 | [self print:arrayM len:number index:sum]; 35 | } else { 36 | for (int i = prefixIndex; i < number; i++) { 37 | if ([self isSwap:arrayM len:number index:i]) { 38 | // 交换前缀,使之产生下一个前缀. - i 递增,所以可以保证数组中的每一个元素都位于第0个,作为前缀的起始字符 39 | [self swap:arrayM i:prefixIndex j:i]; 40 | 41 | // 以递归的方式对剩下的元素进行全排列 42 | [self permutation:arrayM prefixIndex:prefixIndex + 1 number:number]; 43 | 44 | // 将前缀换回来,继续做上一个的前缀排列. 45 | [self swap:arrayM i:prefixIndex j:i]; 46 | } 47 | } 48 | } 49 | } 50 | 51 | // 实现两数交换 52 | - (void)swap:(NSMutableArray *)arrM i:(NSUInteger)i j:(NSUInteger)j { 53 | NSNumber *temp = arrM[i]; 54 | arrM[i] = arrM[j]; 55 | arrM[j] = temp; 56 | } 57 | 58 | // 打印数组内容 59 | - (void)print:(NSMutableArray *)arrM len:(NSUInteger)len index:(int)index { 60 | NSMutableString *strM = [NSMutableString string]; 61 | for (NSUInteger i = 0; i < len; i++) { 62 | [strM appendString:[NSString stringWithFormat:@"%@ ",arrM[i]]]; 63 | } 64 | NSLog(@"index:[%d] %@",index,strM); 65 | } 66 | 67 | // 是否交换 68 | - (bool)isSwap:(NSMutableArray *)arrM len:(int)len index:(int)index { 69 | for (int i = index + 1; i < len; i++) { 70 | if ([arrM[index] intValue] == [arrM[i] intValue]) { 71 | return NO; 72 | } 73 | } 74 | return YES; 75 | } 76 | 77 | - (void)printArrM:(NSMutableArray *)arrM { 78 | NSMutableString *strM = [NSMutableString string]; 79 | for (int i = 0; i < arrM.count; i++) { 80 | [strM appendString:[NSString stringWithFormat:@"%@ ",arrM[i]]]; 81 | } 82 | NSLog(@"%@",strM); 83 | } 84 | 85 | @end 86 | -------------------------------------------------------------------------------- /ArrayFullArrange/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ArrayFullArrange 4 | // 5 | // Created by cs on 2019/3/30. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4028F825224865FF00080169 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4028F824224865FF00080169 /* AppDelegate.m */; }; 11 | 4028F828224865FF00080169 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4028F827224865FF00080169 /* ViewController.m */; }; 12 | 4028F82B224865FF00080169 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4028F829224865FF00080169 /* Main.storyboard */; }; 13 | 4028F82D2248660000080169 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4028F82C2248660000080169 /* Assets.xcassets */; }; 14 | 4028F8302248660000080169 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4028F82E2248660000080169 /* LaunchScreen.storyboard */; }; 15 | 4028F8332248660000080169 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4028F8322248660000080169 /* main.m */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 4028F820224865FF00080169 /* InsertSortDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = InsertSortDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | 4028F823224865FF00080169 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 21 | 4028F824224865FF00080169 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 22 | 4028F826224865FF00080169 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 23 | 4028F827224865FF00080169 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 24 | 4028F82A224865FF00080169 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 25 | 4028F82C2248660000080169 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | 4028F82F2248660000080169 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 27 | 4028F8312248660000080169 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 28 | 4028F8322248660000080169 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 4028F81D224865FF00080169 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 4028F817224865FF00080169 = { 43 | isa = PBXGroup; 44 | children = ( 45 | 4028F822224865FF00080169 /* InsertSortDemo */, 46 | 4028F821224865FF00080169 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | 4028F821224865FF00080169 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | 4028F820224865FF00080169 /* InsertSortDemo.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | 4028F822224865FF00080169 /* InsertSortDemo */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 4028F823224865FF00080169 /* AppDelegate.h */, 62 | 4028F824224865FF00080169 /* AppDelegate.m */, 63 | 4028F826224865FF00080169 /* ViewController.h */, 64 | 4028F827224865FF00080169 /* ViewController.m */, 65 | 4028F829224865FF00080169 /* Main.storyboard */, 66 | 4028F82C2248660000080169 /* Assets.xcassets */, 67 | 4028F82E2248660000080169 /* LaunchScreen.storyboard */, 68 | 4028F8312248660000080169 /* Info.plist */, 69 | 4028F8322248660000080169 /* main.m */, 70 | ); 71 | path = InsertSortDemo; 72 | sourceTree = ""; 73 | }; 74 | /* End PBXGroup section */ 75 | 76 | /* Begin PBXNativeTarget section */ 77 | 4028F81F224865FF00080169 /* InsertSortDemo */ = { 78 | isa = PBXNativeTarget; 79 | buildConfigurationList = 4028F8362248660000080169 /* Build configuration list for PBXNativeTarget "InsertSortDemo" */; 80 | buildPhases = ( 81 | 4028F81C224865FF00080169 /* Sources */, 82 | 4028F81D224865FF00080169 /* Frameworks */, 83 | 4028F81E224865FF00080169 /* Resources */, 84 | ); 85 | buildRules = ( 86 | ); 87 | dependencies = ( 88 | ); 89 | name = InsertSortDemo; 90 | productName = InsertSortDemo; 91 | productReference = 4028F820224865FF00080169 /* InsertSortDemo.app */; 92 | productType = "com.apple.product-type.application"; 93 | }; 94 | /* End PBXNativeTarget section */ 95 | 96 | /* Begin PBXProject section */ 97 | 4028F818224865FF00080169 /* Project object */ = { 98 | isa = PBXProject; 99 | attributes = { 100 | LastUpgradeCheck = 1010; 101 | ORGANIZATIONNAME = cs; 102 | TargetAttributes = { 103 | 4028F81F224865FF00080169 = { 104 | CreatedOnToolsVersion = 10.1; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = 4028F81B224865FF00080169 /* Build configuration list for PBXProject "InsertSortDemo" */; 109 | compatibilityVersion = "Xcode 9.3"; 110 | developmentRegion = en; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | Base, 115 | ); 116 | mainGroup = 4028F817224865FF00080169; 117 | productRefGroup = 4028F821224865FF00080169 /* Products */; 118 | projectDirPath = ""; 119 | projectRoot = ""; 120 | targets = ( 121 | 4028F81F224865FF00080169 /* InsertSortDemo */, 122 | ); 123 | }; 124 | /* End PBXProject section */ 125 | 126 | /* Begin PBXResourcesBuildPhase section */ 127 | 4028F81E224865FF00080169 /* Resources */ = { 128 | isa = PBXResourcesBuildPhase; 129 | buildActionMask = 2147483647; 130 | files = ( 131 | 4028F8302248660000080169 /* LaunchScreen.storyboard in Resources */, 132 | 4028F82D2248660000080169 /* Assets.xcassets in Resources */, 133 | 4028F82B224865FF00080169 /* Main.storyboard in Resources */, 134 | ); 135 | runOnlyForDeploymentPostprocessing = 0; 136 | }; 137 | /* End PBXResourcesBuildPhase section */ 138 | 139 | /* Begin PBXSourcesBuildPhase section */ 140 | 4028F81C224865FF00080169 /* Sources */ = { 141 | isa = PBXSourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 4028F828224865FF00080169 /* ViewController.m in Sources */, 145 | 4028F8332248660000080169 /* main.m in Sources */, 146 | 4028F825224865FF00080169 /* AppDelegate.m in Sources */, 147 | ); 148 | runOnlyForDeploymentPostprocessing = 0; 149 | }; 150 | /* End PBXSourcesBuildPhase section */ 151 | 152 | /* Begin PBXVariantGroup section */ 153 | 4028F829224865FF00080169 /* Main.storyboard */ = { 154 | isa = PBXVariantGroup; 155 | children = ( 156 | 4028F82A224865FF00080169 /* Base */, 157 | ); 158 | name = Main.storyboard; 159 | sourceTree = ""; 160 | }; 161 | 4028F82E2248660000080169 /* LaunchScreen.storyboard */ = { 162 | isa = PBXVariantGroup; 163 | children = ( 164 | 4028F82F2248660000080169 /* Base */, 165 | ); 166 | name = LaunchScreen.storyboard; 167 | sourceTree = ""; 168 | }; 169 | /* End PBXVariantGroup section */ 170 | 171 | /* Begin XCBuildConfiguration section */ 172 | 4028F8342248660000080169 /* Debug */ = { 173 | isa = XCBuildConfiguration; 174 | buildSettings = { 175 | ALWAYS_SEARCH_USER_PATHS = NO; 176 | CLANG_ANALYZER_NONNULL = YES; 177 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 178 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 179 | CLANG_CXX_LIBRARY = "libc++"; 180 | CLANG_ENABLE_MODULES = YES; 181 | CLANG_ENABLE_OBJC_ARC = YES; 182 | CLANG_ENABLE_OBJC_WEAK = YES; 183 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 184 | CLANG_WARN_BOOL_CONVERSION = YES; 185 | CLANG_WARN_COMMA = YES; 186 | CLANG_WARN_CONSTANT_CONVERSION = YES; 187 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 188 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 189 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 190 | CLANG_WARN_EMPTY_BODY = YES; 191 | CLANG_WARN_ENUM_CONVERSION = YES; 192 | CLANG_WARN_INFINITE_RECURSION = YES; 193 | CLANG_WARN_INT_CONVERSION = YES; 194 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 195 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 196 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 197 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 198 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 199 | CLANG_WARN_STRICT_PROTOTYPES = YES; 200 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 201 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 202 | CLANG_WARN_UNREACHABLE_CODE = YES; 203 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 204 | CODE_SIGN_IDENTITY = "iPhone Developer"; 205 | COPY_PHASE_STRIP = NO; 206 | DEBUG_INFORMATION_FORMAT = dwarf; 207 | ENABLE_STRICT_OBJC_MSGSEND = YES; 208 | ENABLE_TESTABILITY = YES; 209 | GCC_C_LANGUAGE_STANDARD = gnu11; 210 | GCC_DYNAMIC_NO_PIC = NO; 211 | GCC_NO_COMMON_BLOCKS = YES; 212 | GCC_OPTIMIZATION_LEVEL = 0; 213 | GCC_PREPROCESSOR_DEFINITIONS = ( 214 | "DEBUG=1", 215 | "$(inherited)", 216 | ); 217 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 218 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 219 | GCC_WARN_UNDECLARED_SELECTOR = YES; 220 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 221 | GCC_WARN_UNUSED_FUNCTION = YES; 222 | GCC_WARN_UNUSED_VARIABLE = YES; 223 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 224 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 225 | MTL_FAST_MATH = YES; 226 | ONLY_ACTIVE_ARCH = YES; 227 | SDKROOT = iphoneos; 228 | }; 229 | name = Debug; 230 | }; 231 | 4028F8352248660000080169 /* Release */ = { 232 | isa = XCBuildConfiguration; 233 | buildSettings = { 234 | ALWAYS_SEARCH_USER_PATHS = NO; 235 | CLANG_ANALYZER_NONNULL = YES; 236 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 237 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 238 | CLANG_CXX_LIBRARY = "libc++"; 239 | CLANG_ENABLE_MODULES = YES; 240 | CLANG_ENABLE_OBJC_ARC = YES; 241 | CLANG_ENABLE_OBJC_WEAK = YES; 242 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 243 | CLANG_WARN_BOOL_CONVERSION = YES; 244 | CLANG_WARN_COMMA = YES; 245 | CLANG_WARN_CONSTANT_CONVERSION = YES; 246 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 247 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 248 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 249 | CLANG_WARN_EMPTY_BODY = YES; 250 | CLANG_WARN_ENUM_CONVERSION = YES; 251 | CLANG_WARN_INFINITE_RECURSION = YES; 252 | CLANG_WARN_INT_CONVERSION = YES; 253 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 254 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 255 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 256 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 257 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 258 | CLANG_WARN_STRICT_PROTOTYPES = YES; 259 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 260 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 261 | CLANG_WARN_UNREACHABLE_CODE = YES; 262 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 263 | CODE_SIGN_IDENTITY = "iPhone Developer"; 264 | COPY_PHASE_STRIP = NO; 265 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 266 | ENABLE_NS_ASSERTIONS = NO; 267 | ENABLE_STRICT_OBJC_MSGSEND = YES; 268 | GCC_C_LANGUAGE_STANDARD = gnu11; 269 | GCC_NO_COMMON_BLOCKS = YES; 270 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 271 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 272 | GCC_WARN_UNDECLARED_SELECTOR = YES; 273 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 274 | GCC_WARN_UNUSED_FUNCTION = YES; 275 | GCC_WARN_UNUSED_VARIABLE = YES; 276 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 277 | MTL_ENABLE_DEBUG_INFO = NO; 278 | MTL_FAST_MATH = YES; 279 | SDKROOT = iphoneos; 280 | VALIDATE_PRODUCT = YES; 281 | }; 282 | name = Release; 283 | }; 284 | 4028F8372248660000080169 /* Debug */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | CODE_SIGN_STYLE = Automatic; 289 | INFOPLIST_FILE = InsertSortDemo/Info.plist; 290 | LD_RUNPATH_SEARCH_PATHS = ( 291 | "$(inherited)", 292 | "@executable_path/Frameworks", 293 | ); 294 | PRODUCT_BUNDLE_IDENTIFIER = cs.InsertSortDemo; 295 | PRODUCT_NAME = "$(TARGET_NAME)"; 296 | TARGETED_DEVICE_FAMILY = "1,2"; 297 | }; 298 | name = Debug; 299 | }; 300 | 4028F8382248660000080169 /* Release */ = { 301 | isa = XCBuildConfiguration; 302 | buildSettings = { 303 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 304 | CODE_SIGN_STYLE = Automatic; 305 | INFOPLIST_FILE = InsertSortDemo/Info.plist; 306 | LD_RUNPATH_SEARCH_PATHS = ( 307 | "$(inherited)", 308 | "@executable_path/Frameworks", 309 | ); 310 | PRODUCT_BUNDLE_IDENTIFIER = cs.InsertSortDemo; 311 | PRODUCT_NAME = "$(TARGET_NAME)"; 312 | TARGETED_DEVICE_FAMILY = "1,2"; 313 | }; 314 | name = Release; 315 | }; 316 | /* End XCBuildConfiguration section */ 317 | 318 | /* Begin XCConfigurationList section */ 319 | 4028F81B224865FF00080169 /* Build configuration list for PBXProject "InsertSortDemo" */ = { 320 | isa = XCConfigurationList; 321 | buildConfigurations = ( 322 | 4028F8342248660000080169 /* Debug */, 323 | 4028F8352248660000080169 /* Release */, 324 | ); 325 | defaultConfigurationIsVisible = 0; 326 | defaultConfigurationName = Release; 327 | }; 328 | 4028F8362248660000080169 /* Build configuration list for PBXNativeTarget "InsertSortDemo" */ = { 329 | isa = XCConfigurationList; 330 | buildConfigurations = ( 331 | 4028F8372248660000080169 /* Debug */, 332 | 4028F8382248660000080169 /* Release */, 333 | ); 334 | defaultConfigurationIsVisible = 0; 335 | defaultConfigurationName = Release; 336 | }; 337 | /* End XCConfigurationList section */ 338 | }; 339 | rootObject = 4028F818224865FF00080169 /* Project object */; 340 | } 341 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo.xcodeproj/project.xcworkspace/xcuserdata/cs.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenshuangsmart/ArithmeticDemo/0c081877c5e916e13075bcb0c2544f3efd072168/InsertSortDemo/InsertSortDemo.xcodeproj/project.xcworkspace/xcuserdata/cs.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo.xcodeproj/xcuserdata/cs.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | InsertSortDemo.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // InsertSortDemo 4 | // 5 | // Created by cs on 2019/3/25. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // InsertSortDemo 4 | // 5 | // Created by cs on 2019/3/25. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // InsertSortDemo 4 | // 5 | // Created by cs on 2019/3/25. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // InsertSortDemo 4 | // 5 | // Created by cs on 2019/3/25. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view, typically from a nib. 20 | 21 | NSArray *array = @[@3,@2, @6, @9, @8, @5, @7, @1, @4]; 22 | [self inserSort:[NSMutableArray arrayWithArray:array]]; 23 | } 24 | 25 | /** 26 | 插入排序 27 | 28 | @param array 需要排序的Array 29 | */ 30 | - (void)inserSort:(NSMutableArray *)array{ 31 | // 插入排序的原理:始终定义第一个元素为有序的,将元素逐个插入到有序排列之中,其特点是要不断的 32 | 33 | // 移动数据,空出一个适当的位置,把待插入的元素放到里面去。 34 | for (int i = 0; i < array.count; i++) { 35 | 36 | NSNumber *temp = array[i]; 37 | // temp 为待排元素 i为其位置 j为已排元素最后一个元素的位置(即取下一个元素,在已经排好序的元素序列中从后向前扫描) 38 | 39 | int j = i-1; 40 | 41 | // 当j < 0 时, i 为第一个元素 该元素认为已经是排好序的 所以不进入while循环 42 | while (j >= 0 && [array[j] intValue] > [temp intValue]) { 43 | //如果已经排好序的序列中元素大于新元素,则将该元素往右移动一个位置 44 | [array replaceObjectAtIndex:j+1 withObject:array[j]]; 45 | j--; 46 | } 47 | // 跳出while循环时,j的元素小于或等于i的元素(待排元素)。插入新元素 a[j+1] = temp,即将空出来的位置插入待排序的值 48 | [array replaceObjectAtIndex:j+1 withObject:temp]; 49 | NSLog(@"插入排序排序中:%@",[self getArrayStr:array]); 50 | } 51 | } 52 | 53 | - (NSString *)getArrayStr:(NSArray *)array { 54 | NSMutableString *strM = [NSMutableString string]; 55 | for (NSNumber *num in array) { 56 | [strM appendString:[NSString stringWithFormat:@"%@,",num]]; 57 | } 58 | return strM.copy; 59 | } 60 | 61 | @end 62 | -------------------------------------------------------------------------------- /InsertSortDemo/InsertSortDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // InsertSortDemo 4 | // 5 | // Created by cs on 2019/3/25. 6 | // Copyright © 2019 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4022190F21ACCBE0003E48AE /* Permutation_38.m in Sources */ = {isa = PBXBuildFile; fileRef = 4022190E21ACCBE0003E48AE /* Permutation_38.m */; }; 11 | 4022191521ACCFB5003E48AE /* MoreThanHalfNumSolution_39.m in Sources */ = {isa = PBXBuildFile; fileRef = 4022191421ACCFB5003E48AE /* MoreThanHalfNumSolution_39.m */; }; 12 | 4022192321AF6E17003E48AE /* FindKthSmallest_40.m in Sources */ = {isa = PBXBuildFile; fileRef = 4022192221AF6E17003E48AE /* FindKthSmallest_40.m */; }; 13 | 402950F22184038600E492BF /* NSArray+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = 402950F12184038600E492BF /* NSArray+Extension.m */; }; 14 | 402950F6218469E000E492BF /* NSString+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = 402950F5218469E000E492BF /* NSString+Extension.m */; }; 15 | 40636FC02159F37500DE29C4 /* Stack.m in Sources */ = {isa = PBXBuildFile; fileRef = 40636FBF2159F37500DE29C4 /* Stack.m */; }; 16 | 40636FC32159F4D700DE29C4 /* ListNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 40636FC22159F4D700DE29C4 /* ListNode.m */; }; 17 | 40636FC62159F6B800DE29C4 /* LinkedArray.m in Sources */ = {isa = PBXBuildFile; fileRef = 40636FC52159F6B800DE29C4 /* LinkedArray.m */; }; 18 | 40636FEE215C959100DE29C4 /* BinaryTreeNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 40636FED215C959100DE29C4 /* BinaryTreeNode.m */; }; 19 | 40636FF1215DB5E600DE29C4 /* HashMap.m in Sources */ = {isa = PBXBuildFile; fileRef = 40636FF0215DB5E600DE29C4 /* HashMap.m */; }; 20 | 4063D6522136253100A5E3D4 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4063D6512136253100A5E3D4 /* AppDelegate.m */; }; 21 | 4063D6582136253100A5E3D4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4063D6562136253100A5E3D4 /* Main.storyboard */; }; 22 | 4063D65A2136253200A5E3D4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4063D6592136253200A5E3D4 /* Assets.xcassets */; }; 23 | 4063D65D2136253200A5E3D4 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4063D65B2136253200A5E3D4 /* LaunchScreen.storyboard */; }; 24 | 4063D6602136253200A5E3D4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4063D65F2136253200A5E3D4 /* main.m */; }; 25 | 4064B0DE214F666600F3D34B /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4064B0DD214F666600F3D34B /* ViewController.m */; }; 26 | 406C10212199040400216EAA /* FindKthToTail_22.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FEE2199040300216EAA /* FindKthToTail_22.m */; }; 27 | 406C10222199040400216EAA /* MirrorTree_27.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FF02199040300216EAA /* MirrorTree_27.m */; }; 28 | 406C10232199040400216EAA /* IsSymmetrical_28.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FF12199040300216EAA /* IsSymmetrical_28.m */; }; 29 | 406C10242199040400216EAA /* MergeTwoListNode_25.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FF42199040300216EAA /* MergeTwoListNode_25.m */; }; 30 | 406C10252199040400216EAA /* HasSubTree_26.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FF62199040300216EAA /* HasSubTree_26.m */; }; 31 | 406C10262199040400216EAA /* PrintMatrixClockwise_29.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FF72199040300216EAA /* PrintMatrixClockwise_29.m */; }; 32 | 406C10272199040400216EAA /* reOrderArray_21.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FF82199040300216EAA /* reOrderArray_21.m */; }; 33 | 406C10282199040400216EAA /* EntryNodeOfLoop_23.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FFA2199040300216EAA /* EntryNodeOfLoop_23.m */; }; 34 | 406C102A2199040400216EAA /* ReverseList_24.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C0FFF2199040300216EAA /* ReverseList_24.m */; }; 35 | 406C102B2199040400216EAA /* DeleteNode_18.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10012199040400216EAA /* DeleteNode_18.m */; }; 36 | 406C102C2199040400216EAA /* MatrixHasPath.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10032199040400216EAA /* MatrixHasPath.m */; }; 37 | 406C102D2199040400216EAA /* NumberOfOne_15.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10092199040400216EAA /* NumberOfOne_15.m */; }; 38 | 406C102E2199040400216EAA /* MinNumberInRotateArray.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C100A2199040400216EAA /* MinNumberInRotateArray.m */; }; 39 | 406C102F2199040400216EAA /* RegularExpressionMath_19.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C100B2199040400216EAA /* RegularExpressionMath_19.m */; }; 40 | 406C10302199040400216EAA /* MovingCount_13.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C100E2199040400216EAA /* MovingCount_13.m */; }; 41 | 406C10312199040400216EAA /* printOneToMaxOfNDigits_17.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C100F2199040400216EAA /* printOneToMaxOfNDigits_17.m */; }; 42 | 406C10322199040400216EAA /* power_16.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10102199040400216EAA /* power_16.m */; }; 43 | 406C10332199040400216EAA /* TwoDimensionalArrayFind_04.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10142199040400216EAA /* TwoDimensionalArrayFind_04.m */; }; 44 | 406C10342199040400216EAA /* ReConstructBinaryTree_07.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10162199040400216EAA /* ReConstructBinaryTree_07.m */; }; 45 | 406C10352199040400216EAA /* TwoStackQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10172199040400216EAA /* TwoStackQueue.m */; }; 46 | 406C10362199040400216EAA /* PrintListFromTailToHead_06.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10182199040400216EAA /* PrintListFromTailToHead_06.m */; }; 47 | 406C10372199040400216EAA /* Fibonacci.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C10192199040400216EAA /* Fibonacci.m */; }; 48 | 406C10382199040400216EAA /* ReplaceBlank_05.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C101A2199040400216EAA /* ReplaceBlank_05.m */; }; 49 | 406C10392199040400216EAA /* 03-repeatNumber.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C101B2199040400216EAA /* 03-repeatNumber.m */; }; 50 | 406C103C219904B600216EAA /* IsNumeric_20.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C103A219904B600216EAA /* IsNumeric_20.m */; }; 51 | 406C103F2199066500216EAA /* IsPopOrder_31.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C103E2199066500216EAA /* IsPopOrder_31.m */; }; 52 | 406C106C219A52B800216EAA /* PrintFromTopToBottom_32.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C106B219A52B800216EAA /* PrintFromTopToBottom_32.m */; }; 53 | 406C106F219A556C00216EAA /* VerifySquenceOfBST_33.m in Sources */ = {isa = PBXBuildFile; fileRef = 406C106E219A556C00216EAA /* VerifySquenceOfBST_33.m */; }; 54 | 40880455219CF89A0001441C /* Backtracking_34.m in Sources */ = {isa = PBXBuildFile; fileRef = 40880454219CF89A0001441C /* Backtracking_34.m */; }; 55 | 40880458219CF9270001441C /* RandomListNode_35.m in Sources */ = {isa = PBXBuildFile; fileRef = 40880457219CF9270001441C /* RandomListNode_35.m */; }; 56 | 4088050F21A23FA70001441C /* InOrder_36.m in Sources */ = {isa = PBXBuildFile; fileRef = 4088050E21A23FA70001441C /* InOrder_36.m */; }; 57 | 4088051221A2413A0001441C /* TreeNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 4088051121A2413A0001441C /* TreeNode.m */; }; 58 | 4088055221A3918A0001441C /* Serializer_37.m in Sources */ = {isa = PBXBuildFile; fileRef = 4088055121A3918A0001441C /* Serializer_37.m */; }; 59 | /* End PBXBuildFile section */ 60 | 61 | /* Begin PBXFileReference section */ 62 | 4022190D21ACCBE0003E48AE /* Permutation_38.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Permutation_38.h; sourceTree = ""; }; 63 | 4022190E21ACCBE0003E48AE /* Permutation_38.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Permutation_38.m; sourceTree = ""; }; 64 | 4022191321ACCFB5003E48AE /* MoreThanHalfNumSolution_39.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MoreThanHalfNumSolution_39.h; sourceTree = ""; }; 65 | 4022191421ACCFB5003E48AE /* MoreThanHalfNumSolution_39.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MoreThanHalfNumSolution_39.m; sourceTree = ""; }; 66 | 4022192121AF6E17003E48AE /* FindKthSmallest_40.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FindKthSmallest_40.h; sourceTree = ""; }; 67 | 4022192221AF6E17003E48AE /* FindKthSmallest_40.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FindKthSmallest_40.m; sourceTree = ""; }; 68 | 402950F02184038600E492BF /* NSArray+Extension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSArray+Extension.h"; sourceTree = ""; }; 69 | 402950F12184038600E492BF /* NSArray+Extension.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Extension.m"; sourceTree = ""; }; 70 | 402950F321840E7100E492BF /* PrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = ""; }; 71 | 402950F4218469E000E492BF /* NSString+Extension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSString+Extension.h"; sourceTree = ""; }; 72 | 402950F5218469E000E492BF /* NSString+Extension.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSString+Extension.m"; sourceTree = ""; }; 73 | 40636FBE2159F37500DE29C4 /* Stack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Stack.h; sourceTree = ""; }; 74 | 40636FBF2159F37500DE29C4 /* Stack.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Stack.m; sourceTree = ""; }; 75 | 40636FC12159F4D700DE29C4 /* ListNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ListNode.h; sourceTree = ""; }; 76 | 40636FC22159F4D700DE29C4 /* ListNode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ListNode.m; sourceTree = ""; }; 77 | 40636FC42159F6B800DE29C4 /* LinkedArray.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LinkedArray.h; sourceTree = ""; }; 78 | 40636FC52159F6B800DE29C4 /* LinkedArray.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LinkedArray.m; sourceTree = ""; }; 79 | 40636FEC215C959100DE29C4 /* BinaryTreeNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BinaryTreeNode.h; sourceTree = ""; }; 80 | 40636FED215C959100DE29C4 /* BinaryTreeNode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BinaryTreeNode.m; sourceTree = ""; }; 81 | 40636FEF215DB5E600DE29C4 /* HashMap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HashMap.h; sourceTree = ""; }; 82 | 40636FF0215DB5E600DE29C4 /* HashMap.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HashMap.m; sourceTree = ""; }; 83 | 4063D64D2136253000A5E3D4 /* OC_算法.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "OC_算法.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 84 | 4063D6502136253100A5E3D4 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 85 | 4063D6512136253100A5E3D4 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 86 | 4063D6532136253100A5E3D4 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 87 | 4063D6572136253100A5E3D4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 88 | 4063D6592136253200A5E3D4 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 89 | 4063D65C2136253200A5E3D4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 90 | 4063D65E2136253200A5E3D4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 91 | 4063D65F2136253200A5E3D4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 92 | 4064B0DD214F666600F3D34B /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 93 | 406C0FEC2199040300216EAA /* PrintMatrixClockwise_29.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrintMatrixClockwise_29.h; sourceTree = ""; }; 94 | 406C0FED2199040300216EAA /* HasSubTree_26.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HasSubTree_26.h; sourceTree = ""; }; 95 | 406C0FEE2199040300216EAA /* FindKthToTail_22.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FindKthToTail_22.m; sourceTree = ""; }; 96 | 406C0FEF2199040300216EAA /* reOrderArray_21.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = reOrderArray_21.h; sourceTree = ""; }; 97 | 406C0FF02199040300216EAA /* MirrorTree_27.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MirrorTree_27.m; sourceTree = ""; }; 98 | 406C0FF12199040300216EAA /* IsSymmetrical_28.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IsSymmetrical_28.m; sourceTree = ""; }; 99 | 406C0FF22199040300216EAA /* EntryNodeOfLoop_23.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EntryNodeOfLoop_23.h; sourceTree = ""; }; 100 | 406C0FF32199040300216EAA /* ReverseList_24.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReverseList_24.h; sourceTree = ""; }; 101 | 406C0FF42199040300216EAA /* MergeTwoListNode_25.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MergeTwoListNode_25.m; sourceTree = ""; }; 102 | 406C0FF62199040300216EAA /* HasSubTree_26.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HasSubTree_26.m; sourceTree = ""; }; 103 | 406C0FF72199040300216EAA /* PrintMatrixClockwise_29.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PrintMatrixClockwise_29.m; sourceTree = ""; }; 104 | 406C0FF82199040300216EAA /* reOrderArray_21.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = reOrderArray_21.m; sourceTree = ""; }; 105 | 406C0FF92199040300216EAA /* FindKthToTail_22.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FindKthToTail_22.h; sourceTree = ""; }; 106 | 406C0FFA2199040300216EAA /* EntryNodeOfLoop_23.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EntryNodeOfLoop_23.m; sourceTree = ""; }; 107 | 406C0FFB2199040300216EAA /* IsSymmetrical_28.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IsSymmetrical_28.h; sourceTree = ""; }; 108 | 406C0FFC2199040300216EAA /* MirrorTree_27.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MirrorTree_27.h; sourceTree = ""; }; 109 | 406C0FFE2199040300216EAA /* MergeTwoListNode_25.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MergeTwoListNode_25.h; sourceTree = ""; }; 110 | 406C0FFF2199040300216EAA /* ReverseList_24.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReverseList_24.m; sourceTree = ""; }; 111 | 406C10012199040400216EAA /* DeleteNode_18.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeleteNode_18.m; sourceTree = ""; }; 112 | 406C10022199040400216EAA /* RegularExpressionMath_19.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RegularExpressionMath_19.h; sourceTree = ""; }; 113 | 406C10032199040400216EAA /* MatrixHasPath.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MatrixHasPath.m; sourceTree = ""; }; 114 | 406C10042199040400216EAA /* MinNumberInRotateArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MinNumberInRotateArray.h; sourceTree = ""; }; 115 | 406C10052199040400216EAA /* NumberOfOne_15.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NumberOfOne_15.h; sourceTree = ""; }; 116 | 406C10062199040400216EAA /* MovingCount_13.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MovingCount_13.h; sourceTree = ""; }; 117 | 406C10072199040400216EAA /* power_16.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = power_16.h; sourceTree = ""; }; 118 | 406C10082199040400216EAA /* printOneToMaxOfNDigits_17.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = printOneToMaxOfNDigits_17.h; sourceTree = ""; }; 119 | 406C10092199040400216EAA /* NumberOfOne_15.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NumberOfOne_15.m; sourceTree = ""; }; 120 | 406C100A2199040400216EAA /* MinNumberInRotateArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MinNumberInRotateArray.m; sourceTree = ""; }; 121 | 406C100B2199040400216EAA /* RegularExpressionMath_19.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RegularExpressionMath_19.m; sourceTree = ""; }; 122 | 406C100C2199040400216EAA /* MatrixHasPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MatrixHasPath.h; sourceTree = ""; }; 123 | 406C100D2199040400216EAA /* DeleteNode_18.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeleteNode_18.h; sourceTree = ""; }; 124 | 406C100E2199040400216EAA /* MovingCount_13.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MovingCount_13.m; sourceTree = ""; }; 125 | 406C100F2199040400216EAA /* printOneToMaxOfNDigits_17.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = printOneToMaxOfNDigits_17.m; sourceTree = ""; }; 126 | 406C10102199040400216EAA /* power_16.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = power_16.m; sourceTree = ""; }; 127 | 406C10132199040400216EAA /* ReplaceBlank_05.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReplaceBlank_05.h; sourceTree = ""; }; 128 | 406C10142199040400216EAA /* TwoDimensionalArrayFind_04.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TwoDimensionalArrayFind_04.m; sourceTree = ""; }; 129 | 406C10152199040400216EAA /* 03-repeatNumber.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "03-repeatNumber.h"; sourceTree = ""; }; 130 | 406C10162199040400216EAA /* ReConstructBinaryTree_07.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReConstructBinaryTree_07.m; sourceTree = ""; }; 131 | 406C10172199040400216EAA /* TwoStackQueue.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TwoStackQueue.m; sourceTree = ""; }; 132 | 406C10182199040400216EAA /* PrintListFromTailToHead_06.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PrintListFromTailToHead_06.m; sourceTree = ""; }; 133 | 406C10192199040400216EAA /* Fibonacci.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Fibonacci.m; sourceTree = ""; }; 134 | 406C101A2199040400216EAA /* ReplaceBlank_05.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReplaceBlank_05.m; sourceTree = ""; }; 135 | 406C101B2199040400216EAA /* 03-repeatNumber.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "03-repeatNumber.m"; sourceTree = ""; }; 136 | 406C101C2199040400216EAA /* TwoDimensionalArrayFind_04.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwoDimensionalArrayFind_04.h; sourceTree = ""; }; 137 | 406C101D2199040400216EAA /* PrintListFromTailToHead_06.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrintListFromTailToHead_06.h; sourceTree = ""; }; 138 | 406C101E2199040400216EAA /* ReConstructBinaryTree_07.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReConstructBinaryTree_07.h; sourceTree = ""; }; 139 | 406C101F2199040400216EAA /* TwoStackQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwoStackQueue.h; sourceTree = ""; }; 140 | 406C10202199040400216EAA /* Fibonacci.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Fibonacci.h; sourceTree = ""; }; 141 | 406C103A219904B600216EAA /* IsNumeric_20.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = IsNumeric_20.m; sourceTree = ""; }; 142 | 406C103B219904B600216EAA /* IsNumeric_20.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IsNumeric_20.h; sourceTree = ""; }; 143 | 406C103D2199066500216EAA /* IsPopOrder_31.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IsPopOrder_31.h; sourceTree = ""; }; 144 | 406C103E2199066500216EAA /* IsPopOrder_31.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IsPopOrder_31.m; sourceTree = ""; }; 145 | 406C106A219A52B800216EAA /* PrintFromTopToBottom_32.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrintFromTopToBottom_32.h; sourceTree = ""; }; 146 | 406C106B219A52B800216EAA /* PrintFromTopToBottom_32.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PrintFromTopToBottom_32.m; sourceTree = ""; }; 147 | 406C106D219A556C00216EAA /* VerifySquenceOfBST_33.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VerifySquenceOfBST_33.h; sourceTree = ""; }; 148 | 406C106E219A556C00216EAA /* VerifySquenceOfBST_33.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = VerifySquenceOfBST_33.m; sourceTree = ""; }; 149 | 40880453219CF89A0001441C /* Backtracking_34.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Backtracking_34.h; sourceTree = ""; }; 150 | 40880454219CF89A0001441C /* Backtracking_34.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Backtracking_34.m; sourceTree = ""; }; 151 | 40880456219CF9270001441C /* RandomListNode_35.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RandomListNode_35.h; sourceTree = ""; }; 152 | 40880457219CF9270001441C /* RandomListNode_35.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RandomListNode_35.m; sourceTree = ""; }; 153 | 4088050D21A23FA70001441C /* InOrder_36.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InOrder_36.h; sourceTree = ""; }; 154 | 4088050E21A23FA70001441C /* InOrder_36.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InOrder_36.m; sourceTree = ""; }; 155 | 4088051021A2413A0001441C /* TreeNode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TreeNode.h; sourceTree = ""; }; 156 | 4088051121A2413A0001441C /* TreeNode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TreeNode.m; sourceTree = ""; }; 157 | 4088055021A3918A0001441C /* Serializer_37.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Serializer_37.h; sourceTree = ""; }; 158 | 4088055121A3918A0001441C /* Serializer_37.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Serializer_37.m; sourceTree = ""; }; 159 | /* End PBXFileReference section */ 160 | 161 | /* Begin PBXFrameworksBuildPhase section */ 162 | 4063D64A2136253000A5E3D4 /* Frameworks */ = { 163 | isa = PBXFrameworksBuildPhase; 164 | buildActionMask = 2147483647; 165 | files = ( 166 | ); 167 | runOnlyForDeploymentPostprocessing = 0; 168 | }; 169 | /* End PBXFrameworksBuildPhase section */ 170 | 171 | /* Begin PBXGroup section */ 172 | 402950EF2184037300E492BF /* Category */ = { 173 | isa = PBXGroup; 174 | children = ( 175 | 402950F02184038600E492BF /* NSArray+Extension.h */, 176 | 402950F12184038600E492BF /* NSArray+Extension.m */, 177 | 402950F4218469E000E492BF /* NSString+Extension.h */, 178 | 402950F5218469E000E492BF /* NSString+Extension.m */, 179 | ); 180 | path = Category; 181 | sourceTree = ""; 182 | }; 183 | 40636FBD2159F33800DE29C4 /* Base */ = { 184 | isa = PBXGroup; 185 | children = ( 186 | 40636FBE2159F37500DE29C4 /* Stack.h */, 187 | 40636FBF2159F37500DE29C4 /* Stack.m */, 188 | 40636FC12159F4D700DE29C4 /* ListNode.h */, 189 | 40636FC22159F4D700DE29C4 /* ListNode.m */, 190 | 40636FC42159F6B800DE29C4 /* LinkedArray.h */, 191 | 40636FC52159F6B800DE29C4 /* LinkedArray.m */, 192 | 40636FEC215C959100DE29C4 /* BinaryTreeNode.h */, 193 | 40636FED215C959100DE29C4 /* BinaryTreeNode.m */, 194 | 4088051021A2413A0001441C /* TreeNode.h */, 195 | 4088051121A2413A0001441C /* TreeNode.m */, 196 | 40636FEF215DB5E600DE29C4 /* HashMap.h */, 197 | 40636FF0215DB5E600DE29C4 /* HashMap.m */, 198 | ); 199 | path = Base; 200 | sourceTree = ""; 201 | }; 202 | 4063D6442136253000A5E3D4 = { 203 | isa = PBXGroup; 204 | children = ( 205 | 4063D64F2136253100A5E3D4 /* OC_算法 */, 206 | 4063D64E2136253000A5E3D4 /* Products */, 207 | ); 208 | sourceTree = ""; 209 | }; 210 | 4063D64E2136253000A5E3D4 /* Products */ = { 211 | isa = PBXGroup; 212 | children = ( 213 | 4063D64D2136253000A5E3D4 /* OC_算法.app */, 214 | ); 215 | name = Products; 216 | sourceTree = ""; 217 | }; 218 | 4063D64F2136253100A5E3D4 /* OC_算法 */ = { 219 | isa = PBXGroup; 220 | children = ( 221 | 402950EF2184037300E492BF /* Category */, 222 | 40636FBD2159F33800DE29C4 /* Base */, 223 | 4064B0DF214F66A900F3D34B /* 算法 */, 224 | 4063D6502136253100A5E3D4 /* AppDelegate.h */, 225 | 4063D6512136253100A5E3D4 /* AppDelegate.m */, 226 | 4063D6532136253100A5E3D4 /* ViewController.h */, 227 | 4064B0DD214F666600F3D34B /* ViewController.m */, 228 | 4063D6562136253100A5E3D4 /* Main.storyboard */, 229 | 4063D6592136253200A5E3D4 /* Assets.xcassets */, 230 | 4063D65B2136253200A5E3D4 /* LaunchScreen.storyboard */, 231 | 4063D65E2136253200A5E3D4 /* Info.plist */, 232 | 4063D65F2136253200A5E3D4 /* main.m */, 233 | 402950F321840E7100E492BF /* PrefixHeader.pch */, 234 | ); 235 | path = "OC_算法"; 236 | sourceTree = ""; 237 | }; 238 | 4064B0DF214F66A900F3D34B /* 算法 */ = { 239 | isa = PBXGroup; 240 | children = ( 241 | 406C10122199040400216EAA /* 1-10 */, 242 | 406C10002199040400216EAA /* 11-20 */, 243 | 406C0FEB2199040300216EAA /* 21-30 */, 244 | 406C10112199040400216EAA /* 31-40 */, 245 | ); 246 | path = "算法"; 247 | sourceTree = ""; 248 | }; 249 | 406C0FEB2199040300216EAA /* 21-30 */ = { 250 | isa = PBXGroup; 251 | children = ( 252 | 406C0FEF2199040300216EAA /* reOrderArray_21.h */, 253 | 406C0FF82199040300216EAA /* reOrderArray_21.m */, 254 | 406C0FF92199040300216EAA /* FindKthToTail_22.h */, 255 | 406C0FEE2199040300216EAA /* FindKthToTail_22.m */, 256 | 406C0FF22199040300216EAA /* EntryNodeOfLoop_23.h */, 257 | 406C0FFA2199040300216EAA /* EntryNodeOfLoop_23.m */, 258 | 406C0FF32199040300216EAA /* ReverseList_24.h */, 259 | 406C0FFF2199040300216EAA /* ReverseList_24.m */, 260 | 406C0FFE2199040300216EAA /* MergeTwoListNode_25.h */, 261 | 406C0FF42199040300216EAA /* MergeTwoListNode_25.m */, 262 | 406C0FED2199040300216EAA /* HasSubTree_26.h */, 263 | 406C0FF62199040300216EAA /* HasSubTree_26.m */, 264 | 406C0FFC2199040300216EAA /* MirrorTree_27.h */, 265 | 406C0FF02199040300216EAA /* MirrorTree_27.m */, 266 | 406C0FFB2199040300216EAA /* IsSymmetrical_28.h */, 267 | 406C0FF12199040300216EAA /* IsSymmetrical_28.m */, 268 | 406C0FEC2199040300216EAA /* PrintMatrixClockwise_29.h */, 269 | 406C0FF72199040300216EAA /* PrintMatrixClockwise_29.m */, 270 | ); 271 | path = "21-30"; 272 | sourceTree = ""; 273 | }; 274 | 406C10002199040400216EAA /* 11-20 */ = { 275 | isa = PBXGroup; 276 | children = ( 277 | 406C10062199040400216EAA /* MovingCount_13.h */, 278 | 406C100E2199040400216EAA /* MovingCount_13.m */, 279 | 406C10052199040400216EAA /* NumberOfOne_15.h */, 280 | 406C10092199040400216EAA /* NumberOfOne_15.m */, 281 | 406C10072199040400216EAA /* power_16.h */, 282 | 406C10102199040400216EAA /* power_16.m */, 283 | 406C10082199040400216EAA /* printOneToMaxOfNDigits_17.h */, 284 | 406C100F2199040400216EAA /* printOneToMaxOfNDigits_17.m */, 285 | 406C100D2199040400216EAA /* DeleteNode_18.h */, 286 | 406C10012199040400216EAA /* DeleteNode_18.m */, 287 | 406C10022199040400216EAA /* RegularExpressionMath_19.h */, 288 | 406C100B2199040400216EAA /* RegularExpressionMath_19.m */, 289 | 406C100C2199040400216EAA /* MatrixHasPath.h */, 290 | 406C10032199040400216EAA /* MatrixHasPath.m */, 291 | 406C10042199040400216EAA /* MinNumberInRotateArray.h */, 292 | 406C100A2199040400216EAA /* MinNumberInRotateArray.m */, 293 | 406C103B219904B600216EAA /* IsNumeric_20.h */, 294 | 406C103A219904B600216EAA /* IsNumeric_20.m */, 295 | ); 296 | path = "11-20"; 297 | sourceTree = ""; 298 | }; 299 | 406C10112199040400216EAA /* 31-40 */ = { 300 | isa = PBXGroup; 301 | children = ( 302 | 406C103D2199066500216EAA /* IsPopOrder_31.h */, 303 | 406C103E2199066500216EAA /* IsPopOrder_31.m */, 304 | 406C106A219A52B800216EAA /* PrintFromTopToBottom_32.h */, 305 | 406C106B219A52B800216EAA /* PrintFromTopToBottom_32.m */, 306 | 406C106D219A556C00216EAA /* VerifySquenceOfBST_33.h */, 307 | 406C106E219A556C00216EAA /* VerifySquenceOfBST_33.m */, 308 | 40880453219CF89A0001441C /* Backtracking_34.h */, 309 | 40880454219CF89A0001441C /* Backtracking_34.m */, 310 | 40880456219CF9270001441C /* RandomListNode_35.h */, 311 | 40880457219CF9270001441C /* RandomListNode_35.m */, 312 | 4088050D21A23FA70001441C /* InOrder_36.h */, 313 | 4088050E21A23FA70001441C /* InOrder_36.m */, 314 | 4088055021A3918A0001441C /* Serializer_37.h */, 315 | 4088055121A3918A0001441C /* Serializer_37.m */, 316 | 4022190D21ACCBE0003E48AE /* Permutation_38.h */, 317 | 4022190E21ACCBE0003E48AE /* Permutation_38.m */, 318 | 4022191321ACCFB5003E48AE /* MoreThanHalfNumSolution_39.h */, 319 | 4022191421ACCFB5003E48AE /* MoreThanHalfNumSolution_39.m */, 320 | 4022192121AF6E17003E48AE /* FindKthSmallest_40.h */, 321 | 4022192221AF6E17003E48AE /* FindKthSmallest_40.m */, 322 | ); 323 | path = "31-40"; 324 | sourceTree = ""; 325 | }; 326 | 406C10122199040400216EAA /* 1-10 */ = { 327 | isa = PBXGroup; 328 | children = ( 329 | 406C10152199040400216EAA /* 03-repeatNumber.h */, 330 | 406C101B2199040400216EAA /* 03-repeatNumber.m */, 331 | 406C101C2199040400216EAA /* TwoDimensionalArrayFind_04.h */, 332 | 406C10142199040400216EAA /* TwoDimensionalArrayFind_04.m */, 333 | 406C10132199040400216EAA /* ReplaceBlank_05.h */, 334 | 406C101A2199040400216EAA /* ReplaceBlank_05.m */, 335 | 406C101D2199040400216EAA /* PrintListFromTailToHead_06.h */, 336 | 406C10182199040400216EAA /* PrintListFromTailToHead_06.m */, 337 | 406C101E2199040400216EAA /* ReConstructBinaryTree_07.h */, 338 | 406C10162199040400216EAA /* ReConstructBinaryTree_07.m */, 339 | 406C101F2199040400216EAA /* TwoStackQueue.h */, 340 | 406C10172199040400216EAA /* TwoStackQueue.m */, 341 | 406C10202199040400216EAA /* Fibonacci.h */, 342 | 406C10192199040400216EAA /* Fibonacci.m */, 343 | ); 344 | path = "1-10"; 345 | sourceTree = ""; 346 | }; 347 | /* End PBXGroup section */ 348 | 349 | /* Begin PBXNativeTarget section */ 350 | 4063D64C2136253000A5E3D4 /* OC_算法 */ = { 351 | isa = PBXNativeTarget; 352 | buildConfigurationList = 4063D6632136253200A5E3D4 /* Build configuration list for PBXNativeTarget "OC_算法" */; 353 | buildPhases = ( 354 | 4063D6492136253000A5E3D4 /* Sources */, 355 | 4063D64A2136253000A5E3D4 /* Frameworks */, 356 | 4063D64B2136253000A5E3D4 /* Resources */, 357 | ); 358 | buildRules = ( 359 | ); 360 | dependencies = ( 361 | ); 362 | name = "OC_算法"; 363 | productName = "OC_算法"; 364 | productReference = 4063D64D2136253000A5E3D4 /* OC_算法.app */; 365 | productType = "com.apple.product-type.application"; 366 | }; 367 | /* End PBXNativeTarget section */ 368 | 369 | /* Begin PBXProject section */ 370 | 4063D6452136253000A5E3D4 /* Project object */ = { 371 | isa = PBXProject; 372 | attributes = { 373 | LastUpgradeCheck = 0940; 374 | ORGANIZATIONNAME = cs; 375 | TargetAttributes = { 376 | 4063D64C2136253000A5E3D4 = { 377 | CreatedOnToolsVersion = 9.4.1; 378 | }; 379 | }; 380 | }; 381 | buildConfigurationList = 4063D6482136253000A5E3D4 /* Build configuration list for PBXProject "OC_算法" */; 382 | compatibilityVersion = "Xcode 9.3"; 383 | developmentRegion = en; 384 | hasScannedForEncodings = 0; 385 | knownRegions = ( 386 | en, 387 | Base, 388 | ); 389 | mainGroup = 4063D6442136253000A5E3D4; 390 | productRefGroup = 4063D64E2136253000A5E3D4 /* Products */; 391 | projectDirPath = ""; 392 | projectRoot = ""; 393 | targets = ( 394 | 4063D64C2136253000A5E3D4 /* OC_算法 */, 395 | ); 396 | }; 397 | /* End PBXProject section */ 398 | 399 | /* Begin PBXResourcesBuildPhase section */ 400 | 4063D64B2136253000A5E3D4 /* Resources */ = { 401 | isa = PBXResourcesBuildPhase; 402 | buildActionMask = 2147483647; 403 | files = ( 404 | 4063D65D2136253200A5E3D4 /* LaunchScreen.storyboard in Resources */, 405 | 4063D65A2136253200A5E3D4 /* Assets.xcassets in Resources */, 406 | 4063D6582136253100A5E3D4 /* Main.storyboard in Resources */, 407 | ); 408 | runOnlyForDeploymentPostprocessing = 0; 409 | }; 410 | /* End PBXResourcesBuildPhase section */ 411 | 412 | /* Begin PBXSourcesBuildPhase section */ 413 | 4063D6492136253000A5E3D4 /* Sources */ = { 414 | isa = PBXSourcesBuildPhase; 415 | buildActionMask = 2147483647; 416 | files = ( 417 | 406C106F219A556C00216EAA /* VerifySquenceOfBST_33.m in Sources */, 418 | 40636FC02159F37500DE29C4 /* Stack.m in Sources */, 419 | 40636FF1215DB5E600DE29C4 /* HashMap.m in Sources */, 420 | 4022191521ACCFB5003E48AE /* MoreThanHalfNumSolution_39.m in Sources */, 421 | 40880455219CF89A0001441C /* Backtracking_34.m in Sources */, 422 | 406C10382199040400216EAA /* ReplaceBlank_05.m in Sources */, 423 | 4022190F21ACCBE0003E48AE /* Permutation_38.m in Sources */, 424 | 406C10242199040400216EAA /* MergeTwoListNode_25.m in Sources */, 425 | 4088055221A3918A0001441C /* Serializer_37.m in Sources */, 426 | 406C102D2199040400216EAA /* NumberOfOne_15.m in Sources */, 427 | 406C102E2199040400216EAA /* MinNumberInRotateArray.m in Sources */, 428 | 40880458219CF9270001441C /* RandomListNode_35.m in Sources */, 429 | 40636FC62159F6B800DE29C4 /* LinkedArray.m in Sources */, 430 | 406C10282199040400216EAA /* EntryNodeOfLoop_23.m in Sources */, 431 | 406C103C219904B600216EAA /* IsNumeric_20.m in Sources */, 432 | 406C10262199040400216EAA /* PrintMatrixClockwise_29.m in Sources */, 433 | 406C102F2199040400216EAA /* RegularExpressionMath_19.m in Sources */, 434 | 406C10312199040400216EAA /* printOneToMaxOfNDigits_17.m in Sources */, 435 | 4064B0DE214F666600F3D34B /* ViewController.m in Sources */, 436 | 406C10342199040400216EAA /* ReConstructBinaryTree_07.m in Sources */, 437 | 406C106C219A52B800216EAA /* PrintFromTopToBottom_32.m in Sources */, 438 | 4088050F21A23FA70001441C /* InOrder_36.m in Sources */, 439 | 406C10372199040400216EAA /* Fibonacci.m in Sources */, 440 | 406C102A2199040400216EAA /* ReverseList_24.m in Sources */, 441 | 406C10252199040400216EAA /* HasSubTree_26.m in Sources */, 442 | 4022192321AF6E17003E48AE /* FindKthSmallest_40.m in Sources */, 443 | 406C102C2199040400216EAA /* MatrixHasPath.m in Sources */, 444 | 406C102B2199040400216EAA /* DeleteNode_18.m in Sources */, 445 | 40636FC32159F4D700DE29C4 /* ListNode.m in Sources */, 446 | 406C103F2199066500216EAA /* IsPopOrder_31.m in Sources */, 447 | 406C10392199040400216EAA /* 03-repeatNumber.m in Sources */, 448 | 402950F6218469E000E492BF /* NSString+Extension.m in Sources */, 449 | 4063D6602136253200A5E3D4 /* main.m in Sources */, 450 | 402950F22184038600E492BF /* NSArray+Extension.m in Sources */, 451 | 406C10322199040400216EAA /* power_16.m in Sources */, 452 | 406C10212199040400216EAA /* FindKthToTail_22.m in Sources */, 453 | 406C10222199040400216EAA /* MirrorTree_27.m in Sources */, 454 | 406C10362199040400216EAA /* PrintListFromTailToHead_06.m in Sources */, 455 | 40636FEE215C959100DE29C4 /* BinaryTreeNode.m in Sources */, 456 | 4088051221A2413A0001441C /* TreeNode.m in Sources */, 457 | 406C10232199040400216EAA /* IsSymmetrical_28.m in Sources */, 458 | 406C10352199040400216EAA /* TwoStackQueue.m in Sources */, 459 | 4063D6522136253100A5E3D4 /* AppDelegate.m in Sources */, 460 | 406C10332199040400216EAA /* TwoDimensionalArrayFind_04.m in Sources */, 461 | 406C10272199040400216EAA /* reOrderArray_21.m in Sources */, 462 | 406C10302199040400216EAA /* MovingCount_13.m in Sources */, 463 | ); 464 | runOnlyForDeploymentPostprocessing = 0; 465 | }; 466 | /* End PBXSourcesBuildPhase section */ 467 | 468 | /* Begin PBXVariantGroup section */ 469 | 4063D6562136253100A5E3D4 /* Main.storyboard */ = { 470 | isa = PBXVariantGroup; 471 | children = ( 472 | 4063D6572136253100A5E3D4 /* Base */, 473 | ); 474 | name = Main.storyboard; 475 | sourceTree = ""; 476 | }; 477 | 4063D65B2136253200A5E3D4 /* LaunchScreen.storyboard */ = { 478 | isa = PBXVariantGroup; 479 | children = ( 480 | 4063D65C2136253200A5E3D4 /* Base */, 481 | ); 482 | name = LaunchScreen.storyboard; 483 | sourceTree = ""; 484 | }; 485 | /* End PBXVariantGroup section */ 486 | 487 | /* Begin XCBuildConfiguration section */ 488 | 4063D6612136253200A5E3D4 /* Debug */ = { 489 | isa = XCBuildConfiguration; 490 | buildSettings = { 491 | ALWAYS_SEARCH_USER_PATHS = NO; 492 | CLANG_ANALYZER_NONNULL = YES; 493 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 494 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 495 | CLANG_CXX_LIBRARY = "libc++"; 496 | CLANG_ENABLE_MODULES = YES; 497 | CLANG_ENABLE_OBJC_ARC = YES; 498 | CLANG_ENABLE_OBJC_WEAK = YES; 499 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 500 | CLANG_WARN_BOOL_CONVERSION = YES; 501 | CLANG_WARN_COMMA = YES; 502 | CLANG_WARN_CONSTANT_CONVERSION = YES; 503 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 504 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 505 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 506 | CLANG_WARN_EMPTY_BODY = YES; 507 | CLANG_WARN_ENUM_CONVERSION = YES; 508 | CLANG_WARN_INFINITE_RECURSION = YES; 509 | CLANG_WARN_INT_CONVERSION = YES; 510 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 511 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 512 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 513 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 514 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 515 | CLANG_WARN_STRICT_PROTOTYPES = YES; 516 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 517 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 518 | CLANG_WARN_UNREACHABLE_CODE = YES; 519 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 520 | CODE_SIGN_IDENTITY = "iPhone Developer"; 521 | COPY_PHASE_STRIP = NO; 522 | DEBUG_INFORMATION_FORMAT = dwarf; 523 | ENABLE_STRICT_OBJC_MSGSEND = YES; 524 | ENABLE_TESTABILITY = YES; 525 | GCC_C_LANGUAGE_STANDARD = gnu11; 526 | GCC_DYNAMIC_NO_PIC = NO; 527 | GCC_NO_COMMON_BLOCKS = YES; 528 | GCC_OPTIMIZATION_LEVEL = 0; 529 | GCC_PREPROCESSOR_DEFINITIONS = ( 530 | "DEBUG=1", 531 | "$(inherited)", 532 | ); 533 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 534 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 535 | GCC_WARN_UNDECLARED_SELECTOR = YES; 536 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 537 | GCC_WARN_UNUSED_FUNCTION = YES; 538 | GCC_WARN_UNUSED_VARIABLE = YES; 539 | IPHONEOS_DEPLOYMENT_TARGET = 11.4; 540 | MTL_ENABLE_DEBUG_INFO = YES; 541 | ONLY_ACTIVE_ARCH = YES; 542 | SDKROOT = iphoneos; 543 | }; 544 | name = Debug; 545 | }; 546 | 4063D6622136253200A5E3D4 /* Release */ = { 547 | isa = XCBuildConfiguration; 548 | buildSettings = { 549 | ALWAYS_SEARCH_USER_PATHS = NO; 550 | CLANG_ANALYZER_NONNULL = YES; 551 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 552 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 553 | CLANG_CXX_LIBRARY = "libc++"; 554 | CLANG_ENABLE_MODULES = YES; 555 | CLANG_ENABLE_OBJC_ARC = YES; 556 | CLANG_ENABLE_OBJC_WEAK = YES; 557 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 558 | CLANG_WARN_BOOL_CONVERSION = YES; 559 | CLANG_WARN_COMMA = YES; 560 | CLANG_WARN_CONSTANT_CONVERSION = YES; 561 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 562 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 563 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 564 | CLANG_WARN_EMPTY_BODY = YES; 565 | CLANG_WARN_ENUM_CONVERSION = YES; 566 | CLANG_WARN_INFINITE_RECURSION = YES; 567 | CLANG_WARN_INT_CONVERSION = YES; 568 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 569 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 570 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 571 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 572 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 573 | CLANG_WARN_STRICT_PROTOTYPES = YES; 574 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 575 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 576 | CLANG_WARN_UNREACHABLE_CODE = YES; 577 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 578 | CODE_SIGN_IDENTITY = "iPhone Developer"; 579 | COPY_PHASE_STRIP = NO; 580 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 581 | ENABLE_NS_ASSERTIONS = NO; 582 | ENABLE_STRICT_OBJC_MSGSEND = YES; 583 | GCC_C_LANGUAGE_STANDARD = gnu11; 584 | GCC_NO_COMMON_BLOCKS = YES; 585 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 586 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 587 | GCC_WARN_UNDECLARED_SELECTOR = YES; 588 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 589 | GCC_WARN_UNUSED_FUNCTION = YES; 590 | GCC_WARN_UNUSED_VARIABLE = YES; 591 | IPHONEOS_DEPLOYMENT_TARGET = 11.4; 592 | MTL_ENABLE_DEBUG_INFO = NO; 593 | SDKROOT = iphoneos; 594 | VALIDATE_PRODUCT = YES; 595 | }; 596 | name = Release; 597 | }; 598 | 4063D6642136253200A5E3D4 /* Debug */ = { 599 | isa = XCBuildConfiguration; 600 | buildSettings = { 601 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 602 | CODE_SIGN_STYLE = Automatic; 603 | DEVELOPMENT_TEAM = C2V259WG54; 604 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 605 | GCC_PREFIX_HEADER = "OC_算法/PrefixHeader.pch"; 606 | INFOPLIST_FILE = "OC_算法/Info.plist"; 607 | LD_RUNPATH_SEARCH_PATHS = ( 608 | "$(inherited)", 609 | "@executable_path/Frameworks", 610 | ); 611 | PRODUCT_BUNDLE_IDENTIFIER = "cs.OC---"; 612 | PRODUCT_NAME = "$(TARGET_NAME)"; 613 | TARGETED_DEVICE_FAMILY = "1,2"; 614 | }; 615 | name = Debug; 616 | }; 617 | 4063D6652136253200A5E3D4 /* Release */ = { 618 | isa = XCBuildConfiguration; 619 | buildSettings = { 620 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 621 | CODE_SIGN_STYLE = Automatic; 622 | DEVELOPMENT_TEAM = C2V259WG54; 623 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 624 | GCC_PREFIX_HEADER = "OC_算法/PrefixHeader.pch"; 625 | INFOPLIST_FILE = "OC_算法/Info.plist"; 626 | LD_RUNPATH_SEARCH_PATHS = ( 627 | "$(inherited)", 628 | "@executable_path/Frameworks", 629 | ); 630 | PRODUCT_BUNDLE_IDENTIFIER = "cs.OC---"; 631 | PRODUCT_NAME = "$(TARGET_NAME)"; 632 | TARGETED_DEVICE_FAMILY = "1,2"; 633 | }; 634 | name = Release; 635 | }; 636 | /* End XCBuildConfiguration section */ 637 | 638 | /* Begin XCConfigurationList section */ 639 | 4063D6482136253000A5E3D4 /* Build configuration list for PBXProject "OC_算法" */ = { 640 | isa = XCConfigurationList; 641 | buildConfigurations = ( 642 | 4063D6612136253200A5E3D4 /* Debug */, 643 | 4063D6622136253200A5E3D4 /* Release */, 644 | ); 645 | defaultConfigurationIsVisible = 0; 646 | defaultConfigurationName = Release; 647 | }; 648 | 4063D6632136253200A5E3D4 /* Build configuration list for PBXNativeTarget "OC_算法" */ = { 649 | isa = XCConfigurationList; 650 | buildConfigurations = ( 651 | 4063D6642136253200A5E3D4 /* Debug */, 652 | 4063D6652136253200A5E3D4 /* Release */, 653 | ); 654 | defaultConfigurationIsVisible = 0; 655 | defaultConfigurationName = Release; 656 | }; 657 | /* End XCConfigurationList section */ 658 | }; 659 | rootObject = 4063D6452136253000A5E3D4 /* Project object */; 660 | } 661 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/project.xcworkspace/xcuserdata/cs.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenshuangsmart/ArithmeticDemo/0c081877c5e916e13075bcb0c2544f3efd072168/OC_算法_1/OC_算法.xcodeproj/project.xcworkspace/xcuserdata/cs.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/xcuserdata/cs.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/xcuserdata/cs.xcuserdatad/xcschemes/OC_算法.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 69 | 70 | 71 | 72 | 73 | 74 | 80 | 82 | 88 | 89 | 90 | 91 | 93 | 94 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法.xcodeproj/xcuserdata/cs.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | OC_算法.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 4063D64C2136253000A5E3D4 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/8/29. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/8/29. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/BinaryTreeNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // BinaryTreeNode.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/27. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 二叉树节点 13 | */ 14 | @interface BinaryTreeNode : NSObject 15 | /** 16 | * 值 17 | */ 18 | @property (nonatomic, assign) NSInteger value; 19 | /** 20 | * 左节点 21 | */ 22 | @property (nonatomic, strong) BinaryTreeNode *leftNode; 23 | /** 24 | * 右节点 25 | */ 26 | @property (nonatomic, strong) BinaryTreeNode *rightNode; 27 | 28 | /** 29 | 类工厂方法 30 | 31 | @param value 节点值 32 | @return 二叉树节点 33 | */ 34 | + (BinaryTreeNode *)binaryTreeNodeWithValue:(NSInteger)value; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/BinaryTreeNode.m: -------------------------------------------------------------------------------- 1 | // 2 | // BinaryTreeNode.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/27. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "BinaryTreeNode.h" 10 | 11 | @implementation BinaryTreeNode 12 | 13 | + (BinaryTreeNode *)binaryTreeNodeWithValue:(NSInteger)value { 14 | BinaryTreeNode *treeNode = [[BinaryTreeNode alloc] init]; 15 | treeNode.value = value; 16 | return treeNode; 17 | } 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/HashMap.h: -------------------------------------------------------------------------------- 1 | // 2 | // HashMap.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/28. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 模拟哈希表 13 | */ 14 | @interface HashMap : NSObject 15 | 16 | /** 17 | 插入新值 18 | 19 | @param value 值 20 | @param index 索引 21 | */ 22 | - (void)put:(int)value index:(int)index; 23 | 24 | /** 25 | 根据值,返回其索引 26 | 27 | @param value 值 28 | @return 索引 29 | */ 30 | - (int)get:(int)value; 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/HashMap.m: -------------------------------------------------------------------------------- 1 | // 2 | // HashMap.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/28. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "HashMap.h" 10 | 11 | @implementation HashMap { 12 | NSMutableDictionary *_hashDict; // 保存数据 13 | } 14 | 15 | - (instancetype)init { 16 | self = [super init]; 17 | if (self) { 18 | _hashDict = [NSMutableDictionary dictionary]; 19 | } 20 | return self; 21 | } 22 | 23 | /** 24 | 插入新值 25 | */ 26 | - (void)put:(int)val index:(int)index { 27 | NSString *key = [NSString stringWithFormat:@"%d",index]; 28 | NSString *value = [NSString stringWithFormat:@"%d",val]; 29 | [_hashDict setValue:value forKey:key]; 30 | } 31 | 32 | /** 33 | 根据值,返回其索引 34 | */ 35 | - (int)get:(int)value { 36 | NSArray *allKeys = [_hashDict allKeys]; 37 | if (allKeys.count == 0) { 38 | return 0; 39 | } 40 | int index = 0; 41 | for (NSString *key in allKeys) { 42 | NSString *valueStr = [_hashDict valueForKey:key]; 43 | if ([valueStr intValue] == value) { 44 | index = [key intValue]; 45 | break; 46 | } 47 | } 48 | return index; 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/LinkedArray.h: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedArray.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ListNode.h" 11 | 12 | /** 13 | 模拟链表实现 14 | */ 15 | @interface LinkedArray : NSObject 16 | 17 | /** 数组长度 */ 18 | @property (assign , nonatomic) NSUInteger size; 19 | 20 | /** 数组初始化 */ 21 | + (instancetype)array; 22 | 23 | /** 通过一个 number 数组返回一个链表 */ 24 | - (instancetype)initLiknedArrayWithNunbers:(NSArray *)numbers; 25 | 26 | /** 添加元素 */ 27 | - (void)addObject:(NSObject *)obj; 28 | 29 | /** 移除指定元素 */ 30 | - (void)remove:(NSObject *)obj; 31 | 32 | /** 移除指定索引元素 */ 33 | - (void)removeAtIndex:(NSInteger)index; 34 | 35 | /** 获取指定位置的值 */ 36 | - (NSObject *)objectAtIndex:(NSInteger)index; 37 | 38 | /** 获取第一个节点 */ 39 | - (ListNode *)getFirstListNode; 40 | 41 | /** 获取最后换一个节点 */ 42 | - (ListNode *)getLastListNode; 43 | 44 | /** 获取指定位置节点 */ 45 | - (ListNode *)getListNodeAtIndex:(int)index; 46 | 47 | /** 打印所有节点 */ 48 | - (void)printAllListNode; 49 | 50 | @end 51 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/LinkedArray.m: -------------------------------------------------------------------------------- 1 | // 2 | // LinkedArray.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "LinkedArray.h" 10 | 11 | @interface LinkedArray() 12 | 13 | @property (nonatomic, strong) ListNode *first; //首个节点 14 | 15 | @property (nonatomic, strong) ListNode *last; //最后节点 16 | 17 | @end 18 | 19 | @implementation LinkedArray 20 | 21 | //构造方法 22 | + (instancetype)array { 23 | return [[self alloc]init]; 24 | } 25 | 26 | /** 通过一个 number 数组返回一个链表 */ 27 | - (instancetype)initLiknedArrayWithNunbers:(NSArray *)numbers { 28 | self = [super init]; 29 | if (self) { 30 | [self createLinkedArray:numbers]; 31 | } 32 | return self; 33 | } 34 | 35 | #pragma mark - add 36 | 37 | //添加元素 38 | - (void)addObject:(NSObject *)obj { 39 | 40 | if (!obj) return; 41 | _size ++ ; 42 | 43 | ListNode *node = [[ListNode alloc]init]; 44 | //首个节点为空 45 | if (!_first) { 46 | _first = node; 47 | _last = node; 48 | node.previous = nil; 49 | node.next = nil; 50 | node.content = obj; 51 | return; 52 | } 53 | 54 | //数组中有值 55 | node.previous = _last; 56 | _last.next = node; 57 | node.next = nil; 58 | node.content = obj; 59 | _last = node; 60 | } 61 | 62 | #pragma mark - remove 63 | 64 | //移除元素 65 | - (void)remove:(NSObject *)obj { 66 | if (!obj||!_size) return; 67 | 68 | ListNode *tmpNode = _first; 69 | 70 | for (NSInteger index = 0; index < _size; index ++) { 71 | if ([tmpNode.content isEqual:obj]) { 72 | [self removeNode:tmpNode]; //移除节点 73 | break; 74 | } 75 | } 76 | } 77 | 78 | //根据索引移除元素 79 | - (void)removeAtIndex:(NSInteger)index { 80 | if (index<0||index>=_size) return; 81 | 82 | ListNode *tmpNode = _first; 83 | 84 | for (NSInteger i = 0; i < _size; i ++) { 85 | if (i == index) { 86 | [self removeNode:tmpNode]; //移除节点 87 | break; 88 | } 89 | tmpNode = tmpNode.next; 90 | } 91 | } 92 | 93 | #pragma mark - get 94 | 95 | //获取指定索引元素 96 | - (NSObject *)objectAtIndex:(NSInteger)index { 97 | if (index<0||index>=_size) return nil; 98 | 99 | ListNode *tmpNode = _first; 100 | 101 | for (NSInteger i = 0; i < _size; i ++) { 102 | if (i == index) { 103 | return tmpNode.content; 104 | } 105 | tmpNode = tmpNode.next; 106 | } 107 | return nil; 108 | } 109 | 110 | #pragma mark - get 111 | 112 | - (ListNode *)getFirstListNode { 113 | return _first; 114 | } 115 | 116 | - (ListNode *)getLastListNode { 117 | return _last; 118 | } 119 | 120 | // 获取指定位置节点 121 | - (ListNode *)getListNodeAtIndex:(int)index { 122 | if (index >= _size) { 123 | return nil; 124 | } 125 | if (_first == nil) { 126 | return nil; 127 | } 128 | ListNode *curListNode = _first.next; 129 | for (int i = 0; i < index; i++) { 130 | curListNode = curListNode.next; 131 | } 132 | 133 | return curListNode; 134 | } 135 | 136 | #pragma mark - other function 137 | 138 | // 打印所有节点 139 | - (void)printAllListNode { 140 | ListNode *curNode = [self getFirstListNode]; 141 | if (curNode == nil) { 142 | return; 143 | } 144 | while (curNode) { 145 | ListNode *preNode = curNode.previous; 146 | ListNode *nextNode = curNode.next; 147 | NSLog(@"curNode=%p, value=%d, preNode=%p, nextNode=%p",curNode, curNode.value, preNode, nextNode); 148 | curNode = curNode.next; 149 | } 150 | } 151 | 152 | #pragma mark - private 153 | 154 | //私有 155 | - (void)removeNode:(ListNode *)node { 156 | //连接上下节点 157 | ListNode *preNode = node.previous; 158 | ListNode *nextNode = node.next; 159 | preNode.next = nextNode; 160 | nextNode.previous = preNode; 161 | node.content = nil; //清空被移除节点内容 162 | _size -- ;//长度更新 163 | } 164 | 165 | // 初始化一个链表 166 | - (void)createLinkedArray:(NSArray *)numbers { 167 | if (numbers == nil || numbers.count == 0) { 168 | return; 169 | } 170 | // 生成一个链表 171 | for (int i = 0; i < numbers.count; i++) { 172 | [self addObject:numbers[i]]; 173 | } 174 | } 175 | 176 | @end 177 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/ListNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // ListNode.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 模拟链表实现 13 | */ 14 | @interface ListNode : NSObject 15 | 16 | /** 上个节点 */ 17 | @property (strong , nonatomic) ListNode *previous; 18 | 19 | /** 下个节点 */ 20 | @property (strong , nonatomic) ListNode *next; 21 | 22 | /** 当前节点内容 */ 23 | @property (strong , nonatomic) id content; 24 | 25 | /** int */ 26 | @property(nonatomic, assign) int value; 27 | 28 | /** 打印从当前节点开始之后所有的节点数据 */ 29 | - (void)printAllListNode; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/ListNode.m: -------------------------------------------------------------------------------- 1 | // 2 | // ListNode.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "ListNode.h" 10 | 11 | @implementation ListNode 12 | 13 | - (int)value { 14 | if (_content != nil && [_content isKindOfClass:NSClassFromString(@"NSNumber")]) { 15 | return [(NSNumber *)_content intValue]; 16 | } 17 | return 0; 18 | } 19 | 20 | // 打印从当前节点开始之后所有的节点数据 21 | - (void)printAllListNode { 22 | ListNode *curNode = self; 23 | while (curNode) { 24 | ListNode *preNode = curNode.previous; 25 | ListNode *nextNode = curNode.next; 26 | NSLog(@"curNode=%p, value=%d, preNode=%p, nextNode=%p",curNode, curNode.value, preNode, nextNode); 27 | curNode = curNode.next; 28 | } 29 | } 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/Stack.h: -------------------------------------------------------------------------------- 1 | // 2 | // Stack.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 定义block 13 | @param obj 回调值 14 | */ 15 | typedef void(^StackBlock)(id obj); 16 | 17 | /** 18 | 模拟栈实现 - 简单实现 19 | */ 20 | @interface Stack : NSObject 21 | 22 | // 初始化操作 23 | - (instancetype)initWithNumbers:(NSArray *)numbers; 24 | 25 | /** 入栈 @param obj 指定入栈对象 */ 26 | - (void)push:(id)obj; 27 | 28 | /** 出栈 */ 29 | - (id)popObj; 30 | 31 | /** 是否为空 */ 32 | - (BOOL)isEmpty; 33 | 34 | /** 栈的长度 */ 35 | - (NSInteger)stackLength; 36 | 37 | /** 从栈底开始遍历 @param block 回调遍历的结果 */ 38 | -(void)enumerateObjectsFromBottom:(StackBlock)block; 39 | 40 | /** 从顶部开始遍历 */ 41 | -(void)enumerateObjectsFromtop:(StackBlock)block; 42 | 43 | /** 所有元素出栈,一边出栈一边返回元素 */ 44 | -(void)enumerateObjectsPopStack:(StackBlock)block; 45 | 46 | /** 清空 */ 47 | -(void)removeAllObjects; 48 | 49 | /** 返回栈顶元素 */ 50 | -(id)topObj; 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/Stack.m: -------------------------------------------------------------------------------- 1 | // 2 | // Stack.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "Stack.h" 10 | 11 | @interface Stack () 12 | // 存储栈数据 13 | @property (nonatomic, strong) NSMutableArray *stackArray; 14 | 15 | @end 16 | 17 | @implementation Stack 18 | 19 | // 初始化操作 20 | - (instancetype)initWithNumbers:(NSArray *)numbers { 21 | self = [super init]; 22 | if (self) { 23 | for (NSNumber *number in numbers) { 24 | [self.stackArray addObject:number]; 25 | } 26 | } 27 | return self; 28 | } 29 | 30 | #pragma mark - push 31 | 32 | - (void)push:(id)obj { 33 | [self.stackArray addObject:obj]; 34 | } 35 | 36 | #pragma mark - get 37 | 38 | - (id)popObj { 39 | if ([self isEmpty]) { 40 | return nil; 41 | } else { 42 | id lastObject = self.stackArray.lastObject; 43 | [self.stackArray removeLastObject]; 44 | return lastObject; 45 | } 46 | } 47 | 48 | -(id)topObj { 49 | if ([self isEmpty]) { 50 | return nil; 51 | } else { 52 | return self.stackArray.lastObject; 53 | } 54 | } 55 | 56 | - (BOOL)isEmpty { 57 | return !self.stackArray.count; 58 | } 59 | 60 | - (NSInteger)stackLength { 61 | return self.stackArray.count; 62 | } 63 | 64 | #pragma mark - 遍历 65 | 66 | // 从栈底开始遍历 67 | -(void)enumerateObjectsFromBottom:(StackBlock)block { 68 | [self.stackArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 69 | block ? block(obj) : nil; 70 | }]; 71 | } 72 | 73 | // 从顶部开始遍历 74 | -(void)enumerateObjectsFromtop:(StackBlock)block { 75 | [self.stackArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 76 | block ? block(obj) : nil; 77 | }]; 78 | } 79 | 80 | // 所有元素出栈,一边出栈一边返回元素 81 | -(void)enumerateObjectsPopStack:(StackBlock)block { 82 | __weak typeof(self) weakSelf = self; 83 | NSUInteger count = self.stackArray.count; 84 | for (NSUInteger i = count; i > 0; i --) { 85 | if (block) { 86 | block(weakSelf.stackArray.lastObject); 87 | [self.stackArray removeLastObject]; 88 | } 89 | } 90 | } 91 | 92 | #pragma mark - remove 93 | 94 | -(void)removeAllObjects { 95 | [self.stackArray removeAllObjects]; 96 | } 97 | 98 | #pragma mark - lazy 99 | 100 | - (NSMutableArray *)stackArray { 101 | if (!_stackArray) { 102 | _stackArray = [NSMutableArray array]; 103 | } 104 | return _stackArray; 105 | } 106 | 107 | @end 108 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/TreeNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // TreeNode.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/19. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 二叉树节点 13 | */ 14 | @interface TreeNode : NSObject 15 | /** 16 | * 值 17 | */ 18 | @property (nonatomic, assign) NSInteger value; 19 | /** 20 | * 左节点 21 | */ 22 | @property (nonatomic, strong) TreeNode *leftNode; 23 | /** 24 | * 右节点 25 | */ 26 | @property (nonatomic, strong) TreeNode *rightNode; 27 | 28 | /** 29 | 类工厂方法 30 | 31 | @param value 节点值 32 | @return 二叉树节点 33 | */ 34 | + (TreeNode *)treeNodeWithValue:(NSInteger)value; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Base/TreeNode.m: -------------------------------------------------------------------------------- 1 | // 2 | // TreeNode.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/19. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "TreeNode.h" 10 | 11 | @implementation TreeNode 12 | 13 | + (TreeNode *)treeNodeWithValue:(NSInteger)value { 14 | TreeNode *treeNode = [[TreeNode alloc] init]; 15 | treeNode.value = value; 16 | return treeNode; 17 | } 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Category/NSArray+Extension.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+Extension.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSArray (Extension) 12 | 13 | /** 获取数组中所有对象的描述,即将数组的值拼接成一个字符串 - 数组中所有对象都为 NSNumber 对象才可以 */ 14 | - (NSString *)getAllObjectsDescription; 15 | 16 | /** 获取第 i 个位置的值 */ 17 | - (int)vFI:(NSUInteger)i; 18 | 19 | /** 获取第 i行 j 列 位置的值 */ 20 | - (int)vFI:(NSUInteger)i j:(NSUInteger)j; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Category/NSArray+Extension.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+Extension.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "NSArray+Extension.h" 10 | 11 | @implementation NSArray (Extension) 12 | 13 | /** 打印所有对象 */ 14 | - (NSString *)getAllObjectsDescription { 15 | if (self.count == 0) { 16 | return nil; 17 | } 18 | NSMutableString *strM = [NSMutableString string]; 19 | for (int i = 0; i < self.count; i++) { 20 | if ([self[i] isKindOfClass:[NSNumber class]]) { 21 | NSNumber *number = (NSNumber *)self[i]; 22 | [strM appendString:[NSString stringWithFormat:@"%d ",number.intValue]]; 23 | } else if ([self[i] isKindOfClass:[NSArray class]]) { 24 | [strM appendString:[NSString stringWithFormat:@"%@ \n",[self[i] getAllObjectsDescription]]]; 25 | } 26 | } 27 | return strM; 28 | } 29 | 30 | /** 获取第 i 个位置的值 */ 31 | - (int)vFI:(NSUInteger)i { 32 | if (i >= self.count) { 33 | return 0; 34 | } 35 | if (![[self objectAtIndex:i] isKindOfClass:[NSNumber class]]) { 36 | return 0; 37 | } 38 | 39 | return [[self objectAtIndex:i] intValue]; 40 | } 41 | 42 | /** 获取第 i行 j 列 位置的值 */ 43 | - (int)vFI:(NSUInteger)i j:(NSUInteger)j { 44 | if (i >= self.count) { 45 | return 0; 46 | } 47 | if (![[self objectAtIndex:i] isKindOfClass:[NSArray class]]) { 48 | return 0; 49 | } 50 | NSArray *numbers = [self objectAtIndex:i]; 51 | if (j >= numbers.count) { 52 | return 0; 53 | } 54 | if (![[numbers objectAtIndex:j] isKindOfClass:[NSNumber class]]) { 55 | return 0; 56 | } 57 | 58 | return [[numbers objectAtIndex:j] intValue]; 59 | } 60 | 61 | @end 62 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Category/NSString+Extension.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+Extension.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSString (Extension) 12 | 13 | // 十进制转二进制 14 | - (NSString *)convertBinarySystemFromDecimalSystem; 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Category/NSString+Extension.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+Extension.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "NSString+Extension.h" 10 | 11 | @implementation NSString (Extension) 12 | 13 | /** 十进制数转二进制数 */ 14 | - (NSString *)convertBinarySystemFromDecimalSystem { 15 | NSInteger num = [self intValue]; 16 | NSInteger remainder = 0; //余数 17 | NSInteger divisor = 0; //除数 18 | 19 | NSString * prepare = @""; 20 | 21 | while (true){ 22 | remainder = num % 2; 23 | divisor = num / 2; 24 | num = divisor; 25 | prepare = [prepare stringByAppendingFormat:@"%ld",remainder]; 26 | 27 | if (divisor == 0){ 28 | break; 29 | } 30 | } 31 | 32 | NSString * result = @""; 33 | 34 | for (NSInteger i = prepare.length - 1; i >= 0; i --){ 35 | result = [result stringByAppendingFormat:@"%@",[prepare substringWithRange:NSMakeRange(i , 1)]]; 36 | } 37 | 38 | // 补齐8位显示 39 | while (result.length < 8) { 40 | result = [NSString stringWithFormat:@"0%@",result]; 41 | } 42 | 43 | return result; 44 | } 45 | 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/PrefixHeader.pch: -------------------------------------------------------------------------------- 1 | // 2 | // PrefixHeader.pch 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #ifndef PrefixHeader_pch 10 | #define PrefixHeader_pch 11 | 12 | #import "NSArray+Extension.h" 13 | #import "NSString+Extension.h" 14 | #import "ListNode.h" 15 | #import "LinkedArray.h" 16 | #import "Stack.h" 17 | #import "BinaryTreeNode.h" 18 | #import "TreeNode.h" 19 | 20 | #endif /* PrefixHeader_pch */ 21 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/8/29. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/8/29. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "03-repeatNumber.h" 11 | #import "TwoDimensionalArrayFind_04.h" 12 | #import "ReplaceBlank_05.h" 13 | #import "PrintListFromTailToHead_06.h" 14 | #import "ReConstructBinaryTree_07.h" 15 | #import "BinaryTreeNode.h" 16 | #import "TwoStackQueue.h" 17 | #import "Fibonacci.h" 18 | #import "MinNumberInRotateArray.h" 19 | #import "MatrixHasPath.h" 20 | #import "MovingCount_13.h" 21 | #import "NumberOfOne_15.h" 22 | #import "power_16.h" 23 | #import "DeleteNode_18.h" 24 | #import "NSArray+Extension.h" 25 | #import "reOrderArray_21.h" 26 | #import "FindKthToTail_22.h" 27 | #import "EntryNodeOfLoop_23.h" 28 | #import "ReverseList_24.h" 29 | #import "LinkedArray.h" 30 | #import "MergeTwoListNode_25.h" 31 | #import "MirrorTree_27.h" 32 | #import "IsSymmetrical_28.h" 33 | #import "PrintMatrixClockwise_29.h" 34 | #import "PrintMatrixClockwise_29.h" 35 | #import "VerifySquenceOfBST_33.h" 36 | #import "Serializer_37.h" 37 | #import "MoreThanHalfNumSolution_39.h" 38 | 39 | @interface ViewController () 40 | 41 | @end 42 | 43 | @implementation ViewController 44 | 45 | - (void)viewDidLoad { 46 | [super viewDidLoad]; 47 | // Do any additional setup after loading the view, typically from a nib. 48 | 49 | // 3.数组中重复的数字 50 | // [self repeatNumber]; 51 | 52 | // 4.二维数组中的查找 53 | // [self twoDimensionArrayFind]; 54 | 55 | // 5.替换空格 56 | // [self replaceSpace]; 57 | 58 | // 6.从头到尾打印链表 59 | // [self printListFromTailToHead]; 60 | 61 | // 7.重建二叉树 62 | // [self reConstructBinaryTree]; 63 | 64 | // 9.用两个栈实现队列 65 | // [self twoStackToQueue]; 66 | 67 | // 10.1 斐波那契数列 68 | // [self fibonacci]; 69 | 70 | // 10.2 跳台阶 71 | // [self jumpFloor]; 72 | 73 | // 10.3 变态跳台阶 74 | // [self jumpFloorII]; 75 | 76 | // 10.4 矩形覆盖 77 | // [self rectCover]; 78 | 79 | // 11.1 折半查询 80 | // [self minNumberInRotateArray]; 81 | 82 | // 11.2 折半查询 83 | // [self minNumberInRotateArray2]; 84 | 85 | // 12.矩阵中的路径 86 | // [self matrixHasPath]; 87 | 88 | // 13.机器人的运动范围 89 | // [self movingCount]; 90 | 91 | // 15.二进制中 1 的个数 92 | // [self numberOfOne]; 93 | 94 | // 16.数值的整数次方 95 | // [self power]; 96 | 97 | // 18.1 在 O(1) 时间内删除链表节点 98 | // [self deleteNode]; 99 | 100 | // 18.2 删除链表中重复的结点 101 | // [self deleteDuplication]; 102 | 103 | // 21.调整数组顺序使奇数位于偶数前面 104 | // [self reOrderArray]; 105 | 106 | // 22.链表中倒数第 K 个结点 107 | // [self findKthToTail]; 108 | 109 | // 23.链表中环的入口结点 110 | // [self entryNodeOfLoop]; 111 | 112 | // 24 反转链表 113 | // [self reverseList]; 114 | 115 | // 25 合并两个排序的链表 116 | // [self MergeTwoListNode]; 117 | 118 | // 27.二叉树的镜像 119 | // [self mirror]; 120 | 121 | // 28.对称的二叉树 122 | // [self isSymmetrical]; 123 | 124 | // 29.顺时针打印矩阵 125 | // [self printMatrixClockwise]; 126 | 127 | // 33.二叉搜索树的后序遍历序列 128 | // [self verifySquenceOfBST]; 129 | 130 | // 37.序列号二叉树 131 | // [self serializerBinaryTree]; 132 | 133 | // 39.数组中出现次数超过一半的数字 134 | [self moreThanHalfNum]; 135 | } 136 | 137 | //- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{ 138 | // if(recognizer.direction == UISwipeGestureRecognizerDirectionDown) { 139 | // NSLog(@"swipe down"); 140 | // } 141 | // if(recognizer.direction == UISwipeGestureRecognizerDirectionUp) { 142 | // NSLog(@"swipe up"); 143 | // } 144 | // if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 145 | // NSLog(@"swipe left"); 146 | // } 147 | // if(recognizer.direction == UISwipeGestureRecognizerDirectionRight) { 148 | // NSLog(@"swipe right"); 149 | // } 150 | //} 151 | 152 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 153 | // [self matrixHasPath]; 154 | } 155 | 156 | // 3.数组中重复的数字 157 | - (void)repeatNumber { 158 | NSMutableArray *arrM = [NSMutableArray array]; 159 | for (int i = 0; i < 10; i++) { 160 | NSMutableArray *numbers = [NSMutableArray array]; 161 | for (int j = 0; j < 10; j++) { 162 | [numbers addObject:[NSNumber numberWithInt:arc4random_uniform(10)]]; 163 | } 164 | [arrM addObject:numbers]; 165 | } 166 | 167 | for (int i = 0; i < arrM.count; i++) { 168 | NSArray *numbers = [arrM objectAtIndex:i]; 169 | NSArray *repeatNumbers = [_3_repeatNumber duplicate:numbers]; 170 | NSLog(@"i = %d, 原始数组:%@, 重复数字:%@",i,[numbers getAllObjectsDescription],[repeatNumbers getAllObjectsDescription]); 171 | } 172 | } 173 | 174 | // 4.二维数组中的查找 175 | - (void)twoDimensionArrayFind { 176 | NSArray *targets = @[@16,@6,@30,@20,@15]; 177 | for (int i = 0; i < targets.count; i++) { 178 | bool isFind = [TwoDimensionalArrayFind_04 findNumber: [targets vFI:i] numbers:nil]; 179 | if (!isFind) { 180 | NSLog(@"target = %d, noFind",[targets vFI:i]); 181 | } 182 | } 183 | } 184 | 185 | // 5.替换空格 186 | - (void)replaceSpace { 187 | NSString *str = @"We Are Happy"; 188 | NSLog(@"oldStr = %@",str); 189 | NSString *newStr = [ReplaceBlank_05 replaceSpace:str]; 190 | NSLog(@"newStr = %@",newStr); 191 | } 192 | 193 | // 6.从头到尾打印链表 194 | - (void)printListFromTailToHead { 195 | // 1.使用栈 196 | // NSArray *arr = [PrintListFromTailToHead_06 printListFromTailToHeadByShed:@[@1,@2,@3]]; 197 | // NSLog(@"arr = %@",arr); 198 | 199 | // 2.使用递归 200 | // NSArray *arr = [PrintListFromTailToHead_06 printListFromTailToHeadByRecursion:@[@1,@2,@3]]; 201 | // NSLog(@"arr = %@",arr); 202 | 203 | // 3.使用头表插入法 204 | // NSArray *arr = [PrintListFromTailToHead_06 printListFromTailToHeadByInsert:@[@1,@2,@3]]; 205 | // NSLog(@"arr = %@",arr); 206 | 207 | // 4.使用reverse 反序 208 | NSArray *arr = [PrintListFromTailToHead_06 printListFromTailToHeadByReverse:@[@1,@2,@3]]; 209 | NSLog(@"arr = %@",arr); 210 | } 211 | 212 | // 7.重建二叉树 213 | - (void)reConstructBinaryTree { 214 | NSArray *preorders = @[@3,@9,@20,@15,@7]; 215 | NSArray *inorders = @[@9,@3,@15,@20,@7]; 216 | BinaryTreeNode *treeNode = [ReConstructBinaryTree_07 reConstructBinaryTree:preorders inorders:inorders]; 217 | NSLog(@"treeNode = %@",treeNode); 218 | } 219 | 220 | // 9.用两个栈实现队列 221 | - (void)twoStackToQueue { 222 | NSArray *numbers = @[@1,@2,@3,@4]; 223 | NSNumber *lastNumber = [TwoStackQueue twoStackToQueue:numbers]; 224 | NSLog(@"lastNumber = %@",lastNumber); 225 | } 226 | 227 | // 10.1 斐波那契数列 228 | - (void)fibonacci { 229 | for (int i = 0; i < 20; i++) { 230 | NSLog(@"i = %d, total = %d",i,[Fibonacci Fibonacci:i]); 231 | } 232 | } 233 | 234 | // 10.2 跳台阶 235 | - (void)jumpFloor { 236 | for (int i = 0; i < 10; i++) { 237 | NSLog(@"i = %d, total = %d",i,[Fibonacci jumpFloor:i]); 238 | } 239 | } 240 | 241 | // 10.3 变态跳台阶 242 | - (void)jumpFloorII { 243 | for (int i = 0; i < 10; i++) { 244 | NSLog(@"i = %d, total = %d",i,[Fibonacci jumpFloor2:i]); 245 | } 246 | } 247 | 248 | // 10.4 矩形覆盖 249 | - (void)rectCover { 250 | for (int i = 0; i < 10; i++) { 251 | NSLog(@"i = %d, total = %d",i,[Fibonacci rectCover:i]); 252 | } 253 | } 254 | 255 | // 11.1 折半查询 256 | - (void)minNumberInRotateArray { 257 | int number = [MinNumberInRotateArray minNumberInRotateArray:@[@1,@4,@8,@11,@20]]; 258 | NSLog(@"number = %d",number); 259 | } 260 | 261 | // 11.2 折半查询 262 | - (void)minNumberInRotateArray2 { 263 | int number = [MinNumberInRotateArray minNumberInRotateArray2:@[@3,@4,@2,@1,@3]]; 264 | NSLog(@"number = %d",number); 265 | } 266 | 267 | // 12.矩阵中的路径 268 | - (void)matrixHasPath { 269 | MatrixHasPath *hasPath = [[MatrixHasPath alloc] init]; 270 | NSString *pathStr = @"abtgcfcsjdeh"; 271 | 272 | NSMutableArray *tagPaths = [NSMutableArray array]; 273 | [tagPaths addObject:@"bfce"]; 274 | [tagPaths addObject:@"bfceh"]; 275 | [tagPaths addObject:@"acfde"]; 276 | [tagPaths addObject:@"bcjd"]; 277 | [tagPaths addObject:@"abfceh"]; 278 | [tagPaths addObject:@"abtch"]; 279 | [tagPaths addObject:@"abtsh"]; 280 | 281 | for (int i = 0; i < tagPaths.count; i++) { 282 | NSString *tagPath = [tagPaths objectAtIndex:i]; 283 | bool result = [hasPath hasPath:pathStr rows:3 cols:4 str:tagPath]; 284 | NSLog(@"i = %d, path = %@, result = %d",i,tagPath, result); 285 | NSLog(@"----------------------------------------"); 286 | } 287 | } 288 | 289 | // 13.机器人的运动范围 290 | - (void)movingCount { 291 | MovingCount_13 *movingCount = [[MovingCount_13 alloc] init]; 292 | int count = [movingCount movintCount:18 rows:10 cols:10]; 293 | NSLog(@"count = %d",count); 294 | } 295 | 296 | // 15.二进制中 1 的个数 297 | - (void)numberOfOne { 298 | for (int i = 0; i < 20; i++) { 299 | NSLog(@"i = %d, decimal = %@, count = %d",i,[[NSString stringWithFormat:@"%d",i] convertBinarySystemFromDecimalSystem],[NumberOfOne_15 numberOfOne:i]); 300 | } 301 | } 302 | 303 | // 16.数值的整数次方 304 | - (void)power { 305 | for (int i = 0; i < 10; i++) { 306 | NSLog(@"base = 2, exponent = %d, result = %f",i,[power_16 power:2 exponent:i]); 307 | } 308 | } 309 | 310 | // 18.在 O(1) 时间内删除链表节点 311 | - (void)deleteNode { 312 | DeleteNode_18 *deleteNode = [[DeleteNode_18 alloc] init]; 313 | [deleteNode deleteNode]; 314 | } 315 | 316 | // 18.2 删除链表中重复的结点 317 | - (void)deleteDuplication { 318 | DeleteNode_18 *deleteNode = [[DeleteNode_18 alloc] init]; 319 | [deleteNode deleteDuplicationNode]; 320 | } 321 | 322 | // 21.调整数组顺序使奇数位于偶数前面 323 | - (void)reOrderArray { 324 | NSMutableArray *tempArrM = [NSMutableArray array]; 325 | for (int i = 0; i < 20; i++) { 326 | int number = arc4random_uniform(10); 327 | [tempArrM addObject:[NSNumber numberWithInt:number]]; 328 | } 329 | NSLog(@"原始数组:%@",[tempArrM getAllObjectsDescription]); 330 | NSArray *arr = [reOrderArray_21 reOrderArray:tempArrM.copy]; 331 | NSLog(@"原始数组:%@",[arr getAllObjectsDescription]); 332 | } 333 | 334 | // 22.链表中倒数第 K 个结点 335 | - (void)findKthToTail { 336 | NSMutableArray *numbers = [NSMutableArray array]; 337 | for (int i = 0; i < 10; i++) { 338 | [numbers addObject:[NSNumber numberWithInt:i]]; 339 | } 340 | ListNode *listNode = [FindKthToTail_22 findKthToTail:numbers.copy index:6]; 341 | [listNode printAllListNode]; 342 | } 343 | 344 | // 23.链表中环的入口结点 345 | - (void)entryNodeOfLoop { 346 | ListNode *entryNode = [EntryNodeOfLoop_23 entryNodeOfLoop]; 347 | NSLog(@"entryNode content = %d",entryNode.value); 348 | } 349 | 350 | // 24 反转链表 351 | - (void)reverseList { 352 | NSMutableArray *arrM = [NSMutableArray array]; 353 | for (int i = 0; i < 10; i++) { 354 | [arrM addObject:[NSNumber numberWithInt:i]]; 355 | } 356 | LinkedArray *listArr = [[LinkedArray alloc] initLiknedArrayWithNunbers:arrM.copy]; 357 | NSLog(@"-------------之前链表--------------"); 358 | [listArr printAllListNode]; 359 | ListNode *newHead = [ReverseList_24 reverseList:arrM.copy]; 360 | NSLog(@"-------------反转链表--------------"); 361 | [newHead printAllListNode]; 362 | } 363 | 364 | // 25 合并两个排序的链表 365 | - (void)MergeTwoListNode { 366 | NSArray *array1 = @[@1,@3,@5]; 367 | NSArray *array2 = @[@2,@4,@6]; 368 | 369 | ListNode *listNode1 = [MergeTwoListNode_25 mergeByRecursionWith:array1 numbers2:array2]; 370 | [listNode1 printAllListNode]; 371 | NSLog(@"-------------分割线--------------"); 372 | ListNode *listNode2 = [MergeTwoListNode_25 mergeByIterationWith:array1 numbers2:array2]; 373 | [listNode2 printAllListNode]; 374 | } 375 | 376 | // 27.二叉树的镜像 377 | - (void)mirror { 378 | [MirrorTree_27 mirror]; 379 | } 380 | 381 | // 28.对称的二叉树 382 | - (void)isSymmetrical { 383 | bool isSymetrical = [IsSymmetrical_28 isSymmetrical]; 384 | NSLog(@"isSymetrical = %d",isSymetrical); 385 | } 386 | 387 | // 29.顺时针打印矩阵 388 | - (void)printMatrixClockwise { 389 | NSMutableArray *numbers = [NSMutableArray array]; 390 | for (int i = 1; i < 21; i++) { 391 | [numbers addObject:@(i)]; 392 | } 393 | NSArray *result = [PrintMatrixClockwise_29 printMatrix:numbers]; 394 | NSString *resultStr = [result getAllObjectsDescription]; 395 | NSLog(@"resultStr = %@",resultStr); 396 | } 397 | 398 | // 33.二叉搜索树的后序遍历序列 399 | - (void)verifySquenceOfBST { 400 | NSArray *numbers = @[@1,@2,@3]; 401 | bool result = [VerifySquenceOfBST_33 verifySquenceOfBST:numbers]; 402 | NSLog(@"result = %d",result); 403 | } 404 | 405 | // 37.序列号二叉树 406 | - (void)serializerBinaryTree { 407 | NSString *str = [Serializer_37 serialize]; 408 | NSLog(@"result = %@",str); 409 | 410 | // 反序列化 411 | TreeNode *node = [Serializer_37 deserialize:str]; 412 | NSLog(@"node = %@",node); 413 | } 414 | 415 | // 39.数组中出现次数超过一半的数字 416 | - (void)moreThanHalfNum { 417 | NSMutableArray *numM = [NSMutableArray array]; 418 | for (int i = 0; i < 20; i++) { 419 | [numM addObject:[NSNumber numberWithInt:arc4random_uniform(2)]]; 420 | } 421 | int number = [MoreThanHalfNumSolution_39 MoreThanHalfNum_Solution:numM]; 422 | NSLog(@"number = %d",number); 423 | } 424 | 425 | @end 426 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/8/29. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/03-repeatNumber.h: -------------------------------------------------------------------------------- 1 | // 2 | // 03-repeatNumber.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/17. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 3. 数组中重复的数字 */ 12 | @interface _3_repeatNumber : NSObject 13 | 14 | /** 15 | 找出数组中重复的数字 16 | 17 | @param numbers 数组 18 | @return 重复的数字,如果没有返回nil 19 | */ 20 | + (NSArray *)duplicate:(NSArray *)numbers; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/03-repeatNumber.m: -------------------------------------------------------------------------------- 1 | // 2 | // 03-repeatNumber.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/17. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "03-repeatNumber.h" 10 | 11 | /** 12 | 3.数组中重复的数字 13 | 14 | 题目描述:在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 15 | Input: 16 | {2, 3, 1, 0, 2, 5} 17 | 18 | Output: 19 | 2 20 | 21 | 解题思路: 22 | 要求复杂度为 O(N) + O(1),也就是时间复杂度 O(N),空间复杂度 O(1)。因此不能使用排序的方法,也不能使用额外的标记数组。牛客网讨论区这一题的首票答案使用 nums[i] + length 来将元素标记,这么做会有加法溢出问题。 23 | 24 | 这种数组元素在 [0, n-1] 范围内的问题,可以将值为 i 的元素调整到第 i 个位置上。 25 | 26 | 以 (2, 3, 1, 0, 2, 5) 为例: 27 | 28 | position-0 : (2,3,1,0,2,5) // 2 <-> 1 29 | (1,3,2,0,2,5) // 1 <-> 3 30 | (3,1,2,0,2,5) // 3 <-> 0 31 | (0,1,2,3,2,5) // already in position 32 | position-1 : (0,1,2,3,2,5) // already in position 33 | position-2 : (0,1,2,3,2,5) // already in position 34 | position-3 : (0,1,2,3,2,5) // already in position 35 | position-4 : (0,1,2,3,2,5) // nums[i] == nums[nums[i]], exit 36 | 遍历到位置 4 时,该位置上的数为 2,但是第 2 个位置上已经有一个 2 的值了,因此可以知道 2 重复。 37 | 38 | */ 39 | 40 | @implementation _3_repeatNumber 41 | 42 | + (NSArray *)duplicate:(NSArray *)nums { 43 | if (nums == nil || nums.count == 0) { 44 | return nil; 45 | } 46 | 47 | NSMutableArray *numbers = [NSMutableArray arrayWithArray:nums]; 48 | NSMutableArray *temp = [NSMutableArray array]; 49 | 50 | for (int i = 0; i < numbers.count; i++) { 51 | while ([numbers[i] intValue] != i) { 52 | int number = [numbers[i] intValue]; 53 | // 检查 number 与第 number 位置上的值是否相等,如果相等,说明该 number 重复了,否则索引 i 和 number 两者的值 54 | if (number == [numbers[number] intValue]) { 55 | [temp addObject:numbers[i]]; 56 | return temp.copy; 57 | } 58 | [self cs_swap:numbers i:i j:number]; 59 | } 60 | } 61 | 62 | return nil; 63 | } 64 | 65 | // 交换数组中i 和 j 位置上的数字 66 | + (void)cs_swap:(NSMutableArray *)numbers i:(int)i j:(int)j { 67 | if (i >= numbers.count || j >= numbers.count) { 68 | return; 69 | } 70 | NSNumber *number = numbers[i]; 71 | numbers[i] = numbers[j]; 72 | numbers[j] = number; 73 | } 74 | 75 | @end 76 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/Fibonacci.h: -------------------------------------------------------------------------------- 1 | // 2 | // Fibonacci.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/9. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 10.1 斐波那契数列 13 | */ 14 | @interface Fibonacci : NSObject 15 | 16 | // 求斐波那契数列 17 | + (int)Fibonacci:(int)number; 18 | 19 | /** 20 | 10.2 跳台阶 21 | 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 22 | */ 23 | + (int)jumpFloor:(int)number; 24 | 25 | /** 26 | 10.3 变态跳台阶 27 | 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级... 它也可以跳上 n 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 28 | */ 29 | + (int)jumpFloor2:(int)number; 30 | 31 | /** 32 | 10.4 矩形覆盖 33 | 我们可以用 2*1 的小矩形横着或者竖着去覆盖更大的矩形。请问用 n 个 2*1 的小矩形无重叠地覆盖一个 2*n 的大矩形,总共有多少种方法? 34 | */ 35 | + (int)rectCover:(int)number; 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/Fibonacci.m: -------------------------------------------------------------------------------- 1 | // 2 | // Fibonacci.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/9. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "Fibonacci.h" 10 | 11 | /** 12 | 10.1 斐波那契数列 13 | 题目描述:求斐波那契数列的第 n 项,n <= 39。 14 | 0 (n = 0) 15 | f(n) = 1 (n = 1) 16 | f(n - 1) + f(n - 2) (n > 1) 17 | 18 | */ 19 | 20 | @implementation Fibonacci 21 | 22 | // 1.递归是将一个问题划分成多个子问题求解,动态规划也是如此,但是动态规划会把子问题的解缓存起来,从而避免重复求解子问题。 23 | // 求斐波那契数列 24 | //+ (int)Fibonacci:(int)number { 25 | // if (number <= 1) { 26 | // return number; 27 | // } 28 | // 29 | // // 构造一个数组 30 | // NSMutableArray *fib = [NSMutableArray array]; 31 | // for (int i = 0; i < number + 1; i++) { 32 | // [fib addObject:[NSNumber numberWithInt:0]]; 33 | // } 34 | // 35 | // // 递归求解 36 | // fib[1] = [NSNumber numberWithInt:1]; 37 | // for (int i = 2; i < number + 1; i++) { 38 | // fib[i] = [NSNumber numberWithInt:[fib[i - 1] intValue] + [fib[i - 2] intValue]]; 39 | // } 40 | // 41 | // return [fib[number] intValue]; 42 | //} 43 | 44 | // 2.考虑到第 i 项只与第 i-1 和第 i-2 项有关,因此只需要存储前两项的值就能求解第 i 项,从而将空间复杂度由 O(N) 降低为 O(1)。 45 | + (int)Fibonacci:(int)number { 46 | if (number <= 1) { 47 | return number; 48 | } 49 | 50 | int pre2 = 0, pre1 = 1, fib = 0; 51 | 52 | for (int i = 2; i < number + 1; i++) { 53 | fib = pre1 + pre2; 54 | pre2 = pre1; 55 | pre1 = fib; 56 | } 57 | 58 | return fib; 59 | } 60 | 61 | /** 62 | 10.2 跳台阶 63 | 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 64 | */ 65 | + (int)jumpFloor:(int)number { 66 | if (number <= 2) { 67 | return number; 68 | } 69 | 70 | int pre1 = 2, pre2 = 1; 71 | int result = 1; 72 | 73 | for (int i = 2; i < number; i++) { 74 | result = pre2 + pre1; 75 | pre2 = pre1; 76 | pre1 = result; 77 | } 78 | 79 | return result; 80 | } 81 | 82 | /** 83 | 10.3 变态跳台阶 84 | 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级... 它也可以跳上 n 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 85 | */ 86 | + (int)jumpFloor2:(int)number { 87 | if (number <= 0) { 88 | return 0; 89 | } 90 | // 1.构建数组 91 | NSMutableArray *arrM = [NSMutableArray array]; 92 | for (int i = 0; i < number; i++) { 93 | [arrM addObject:[NSNumber numberWithInt:1]]; 94 | } 95 | 96 | // 2. 97 | for (int i = 1; i < number; i++) { 98 | for (int j = 0; j < i; j++) { 99 | arrM[i] = [NSNumber numberWithInt:[arrM[i] intValue] + [arrM[j] intValue]]; 100 | } 101 | } 102 | 103 | return [arrM[number - 1] intValue]; 104 | } 105 | 106 | /** 107 | 10.4 矩形覆盖 108 | 我们可以用 2*1 的小矩形横着或者竖着去覆盖更大的矩形。请问用 n 个 2*1 的小矩形无重叠地覆盖一个 2*n 的大矩形,总共有多少种方法? 109 | */ 110 | + (int)rectCover:(int)number { 111 | if (number <= 2) { 112 | return number; 113 | } 114 | 115 | int pre1 = 2, pre2 = 1; 116 | int result = 0; 117 | 118 | for (int i = 3; i < number + 1; i++) { 119 | result = pre2 + pre1; 120 | pre2 = pre1; 121 | pre1 = result; 122 | } 123 | 124 | return result; 125 | } 126 | 127 | @end 128 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/PrintListFromTailToHead_06.h: -------------------------------------------------------------------------------- 1 | // 2 | // PrintListFromTailToHead_06.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/21. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 06.从尾到头打印链表 */ 12 | @interface PrintListFromTailToHead_06 : NSObject 13 | 14 | /** 使用栈 */ 15 | + (NSArray *)printListFromTailToHeadByShed:(NSArray *)numbers; 16 | 17 | /** 使用递归 */ 18 | + (NSArray *)printListFromTailToHeadByRecursion:(NSArray *)numbers; 19 | 20 | /** 使用头插法 */ 21 | + (NSArray *)printListFromTailToHeadByInsert:(NSArray *)numbers; 22 | 23 | /** 使用Collection reverse*/ 24 | + (NSArray *)printListFromTailToHeadByReverse:(NSArray *)numbers; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/PrintListFromTailToHead_06.m: -------------------------------------------------------------------------------- 1 | // 2 | // PrintListFromTailToHead_06.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/21. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "PrintListFromTailToHead_06.h" 10 | #import "LinkedArray.h" 11 | #import "ListNode.h" 12 | #import "Stack.h" 13 | 14 | /** 15 | 题目描述: 输入链表的第一个节点,从尾到头反过来打印出每个结点的值。 16 | */ 17 | 18 | @implementation PrintListFromTailToHead_06 19 | 20 | /** 使用栈 */ 21 | + (NSArray *)printListFromTailToHeadByShed:(NSArray *)numbers { 22 | LinkedArray *linkedArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 23 | // 第一个节点 24 | ListNode *listNode = [linkedArray getFirstListNode]; 25 | 26 | return [self getListFromTailToHead:listNode]; 27 | } 28 | 29 | /** 使用递归 */ 30 | + (NSArray *)printListFromTailToHeadByRecursion:(NSArray *)numbers { 31 | LinkedArray *linkedArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 32 | // 第一个节点 33 | ListNode *listNode = [linkedArray getFirstListNode]; 34 | 35 | NSMutableArray *values = [NSMutableArray array]; 36 | if (listNode != nil) { 37 | [values addObjectsFromArray:[self getListFromTailToHead:listNode.next]]; 38 | [values addObject:listNode.content]; 39 | } 40 | 41 | return values; 42 | } 43 | 44 | /** 45 | 使用头插法 46 | 利用链表头插法为逆序的特点。 47 | 头结点和第一个节点的区别: 48 | 49 | * 头结点是在头插法中使用的一个额外节点,这个节点不存储值; 50 | * 第一个节点就是链表的第一个真正存储值的节点。 51 | */ 52 | + (NSArray *)printListFromTailToHeadByInsert:(NSArray *)numbers { 53 | LinkedArray *linkedArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 54 | // 第一个节点 55 | ListNode *listNode = [linkedArray getFirstListNode]; 56 | 57 | // 头插法构建逆序链表 58 | ListNode *head = [[ListNode alloc] init]; 59 | while (listNode != nil) { 60 | ListNode *memo = listNode.next; 61 | listNode.next = head.next; 62 | head.next = listNode; 63 | listNode = memo; 64 | } 65 | // 构建 ArrayList 66 | NSMutableArray *values = [NSMutableArray array]; 67 | head = head.next; 68 | while (head != nil) { 69 | [values addObject:head.content]; 70 | head = head.next; 71 | } 72 | 73 | return values; 74 | } 75 | 76 | /** 使用Collection reverse*/ 77 | + (NSArray *)printListFromTailToHeadByReverse:(NSArray *)numbers { 78 | LinkedArray *linkedArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 79 | // 第一个节点 80 | ListNode *listNode = [linkedArray getFirstListNode]; 81 | 82 | // 构建 ArrayList 83 | NSMutableArray *values = [NSMutableArray array]; 84 | while (listNode != nil) { 85 | [values addObject:listNode.content]; 86 | listNode = listNode.next; 87 | } 88 | 89 | NSArray *newArr = [[values reverseObjectEnumerator] allObjects]; 90 | 91 | return newArr; 92 | } 93 | 94 | #pragma mark - private function 95 | 96 | // 使用栈 97 | + (NSArray *)getListFromTailToHead:(ListNode *)listNode { 98 | // 创建一个栈 99 | Stack *stack = [[Stack alloc] init]; 100 | 101 | // 开始从第一个节点依次往后遍历,将数据全部入栈 102 | while (listNode != nil) { 103 | [stack push:listNode.content]; 104 | listNode = listNode.next; 105 | } 106 | 107 | NSMutableArray *values = [NSMutableArray array]; 108 | // 依次将栈出列并存储 109 | while (!stack.isEmpty) { 110 | [values addObject:stack.popObj]; 111 | } 112 | 113 | return values.copy; 114 | } 115 | 116 | @end 117 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/ReConstructBinaryTree_07.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReConstructBinaryTree_07.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | @class BinaryTreeNode; 11 | 12 | /** 13 | 重建二叉树 14 | */ 15 | @interface ReConstructBinaryTree_07 : NSObject 16 | 17 | /** 18 | 根据二叉树的前序遍历和中序遍历的结果,重建出该二叉树 19 | 20 | @param preorders 前序遍历结果 21 | @param inorders 中序遍历结果 22 | */ 23 | + (BinaryTreeNode *)reConstructBinaryTree:(NSArray *)preorders inorders:(NSArray *)inorders; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/ReConstructBinaryTree_07.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReConstructBinaryTree_07.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/25. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "ReConstructBinaryTree_07.h" 10 | #import "BinaryTreeNode.h" 11 | #import "HashMap.h" 12 | 13 | /** 14 | 根据二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 15 | 二叉树如下: 16 | 3 17 | 9 20 18 | 15 7 19 | 20 | 前序遍历 preorder = [3,9,20,15,7] 21 | 中序遍历 inorder = [9,3,15,20,7] 22 | 23 | 解题思路 24 | 前序遍历的第一个值为根节点的值,使用这个值将中序遍历结果分成两部分,左部分为树的左子树中序遍历结果,右部分为树的右子树中序遍历的结果。 25 | */ 26 | 27 | @implementation ReConstructBinaryTree_07 28 | 29 | + (BinaryTreeNode *)reConstructBinaryTree:(NSArray *)preorders inorders:(NSArray *)inorders { 30 | // 初始化哈希表 31 | HashMap *indexForInOrders = [[HashMap alloc] init]; 32 | 33 | for (int i = 0; i < inorders.count; i++) { 34 | [indexForInOrders put:[inorders[i] intValue] index:i]; 35 | } 36 | 37 | return [self reConstructBinaryTreeWithOrders:indexForInOrders preorders:preorders preL:0 preR:(int)preorders.count - 1 inL:0]; 38 | } 39 | 40 | + (BinaryTreeNode *)reConstructBinaryTreeWithOrders:(HashMap *)indexForInOrders preorders:(NSArray *)preorders preL:(int)preL preR:(int)preR inL:(int)inL { 41 | if (preL > preR) { 42 | return nil; 43 | } 44 | 45 | // 取左边二叉树的值构成一个新的节点 46 | BinaryTreeNode *root = [BinaryTreeNode binaryTreeNodeWithValue:[preorders[preL] integerValue]]; 47 | int inIndex = [indexForInOrders get:(int)root.value]; 48 | int leftTreeSize = inIndex - inL; 49 | 50 | root.leftNode = [self reConstructBinaryTreeWithOrders:indexForInOrders preorders:preorders preL:preL + 1 preR:preL + leftTreeSize inL:inL]; 51 | root.rightNode = [self reConstructBinaryTreeWithOrders:indexForInOrders preorders:preorders preL:preL + leftTreeSize + 1 preR:preR inL:inL + leftTreeSize + 1]; 52 | 53 | return root; 54 | } 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/ReplaceBlank_05.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReplaceBlank_05.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/19. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 替换空格 13 | */ 14 | @interface ReplaceBlank_05 : NSObject 15 | 16 | /** 17 | 替换空格为%20 18 | 19 | @param str 返回替换后的字符串 20 | */ 21 | + (NSString *)replaceSpace:(NSString *)str; 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/ReplaceBlank_05.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReplaceBlank_05.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/19. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "ReplaceBlank_05.h" 10 | 11 | /** 12 | 5.替换空格 13 | 题目描述:将一个字符串中的空格替换成 "%20"。 14 | 解题思路:在字符串尾部填充任意字符,使得字符串的长度等于替换之后的长度。因为一个空格要替换成三个字符(%20),因此当遍历到一个空格时,需要在尾部填充两个任意字符。 15 | 16 | 令 P1 指向字符串原来的末尾位置,P2 指向字符串现在的末尾位置。P1 和 P2从后向前遍历,当 P1 遍历到一个空格时,就需要令 P2 指向的位置依次填充 02%(注意是逆序的),否则就填充上 P1 指向字符的值。 17 | 18 | 从后向前遍是为了在改变 P2 所指向的内容时,不会影响到 P1 遍历原来字符串的内容。 19 | */ 20 | 21 | @implementation ReplaceBlank_05 22 | 23 | + (NSString *)replaceSpace:(NSString *)str { 24 | int p1 = (int)str.length - 1; 25 | NSMutableString *strM = [NSMutableString stringWithString:str]; 26 | 27 | // 1.将在原来的空格后面再加上2个空格 28 | for (int i = 0; i < p1 + 1; i++) { 29 | NSString *temp = [str substringWithRange:NSMakeRange(i, 1)]; 30 | if ([temp isEqualToString:@" "]) { 31 | [strM appendString:@" "]; 32 | } 33 | } 34 | 35 | // p1 p2依次从后往前遍历 36 | int p2 = (int)strM.length - 1; 37 | while (p1 >= 0 && p2 >= 0) { 38 | NSString *temp = [strM substringWithRange:NSMakeRange(p1--, 1)]; 39 | if ([temp isEqualToString:@" "]) { // 发现空格 - 逆序填充02% 40 | [strM replaceCharactersInRange:NSMakeRange(p2--, 1) withString:@"0"]; 41 | [strM replaceCharactersInRange:NSMakeRange(p2--, 1) withString:@"2"]; 42 | [strM replaceCharactersInRange:NSMakeRange(p2--, 1) withString:@"%"]; 43 | } else { 44 | [strM replaceCharactersInRange:NSMakeRange(p2--, 1) withString:temp]; 45 | } 46 | } 47 | 48 | return strM.copy; 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/TwoDimensionalArrayFind_04.h: -------------------------------------------------------------------------------- 1 | // 2 | // TwoDimensionalArrayFind_04.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/19. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 二维数组中的查找 13 | */ 14 | @interface TwoDimensionalArrayFind_04 : NSObject 15 | 16 | /** 17 | 在二维数组中查找 18 | 19 | @param number 要查找的数字 20 | @param numbers 二维数组 - 如果传 nil, 则会用默认的二维数组 21 | @return 是否查找到 22 | */ 23 | + (bool)findNumber:(int)number numbers:(NSArray *)numbers; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/TwoDimensionalArrayFind_04.m: -------------------------------------------------------------------------------- 1 | // 2 | // TwoDimensionalArrayFind_04.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/9/19. 6 | // Copyright © 2018年 cs. All rights reserved. 7 | // 8 | 9 | #import "TwoDimensionalArrayFind_04.h" 10 | 11 | /** 12 | 4. 二维数组中的查找 13 | 14 | 题目描述:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 15 | 16 | 解题思路:从右上角开始查找。矩阵中的一个数,它左边的数都比它小,下边的数都比它大。因此,从右上角开始查找,就可以根据 target 和当前元素的大小关系来缩小查找区间。 17 | 18 | 复杂度:O(M + N) + O(1) 19 | 例如 20 | Consider the following matrix: 21 | [ 22 | [1, 4, 7, 11, 15], 23 | [2, 5, 8, 12, 19], 24 | [3, 6, 9, 16, 22], 25 | [10, 13, 14, 17, 24], 26 | [18, 21, 23, 26, 30] 27 | ] 28 | 29 | Given target = 5, return true. 30 | Given target = 20, return false. 31 | 32 | */ 33 | 34 | @implementation TwoDimensionalArrayFind_04 35 | 36 | // 初始化一个二维数组 37 | + (bool)findNumber:(int)number numbers:(NSArray *)numbers { 38 | if (numbers == nil) { 39 | NSArray *number1 = @[@1,@4,@7,@11,@15]; 40 | NSArray *number2 = @[@2,@5,@8,@12,@19]; 41 | NSArray *number3 = @[@3,@6,@9,@16,@22]; 42 | NSArray *number4 = @[@10,@13,@14,@17,@24]; 43 | NSArray *number5 = @[@18,@21,@23,@26,@30]; 44 | numbers = @[number1,number2,number3,number4,number5]; 45 | } 46 | return [self find:number matrix:numbers]; 47 | } 48 | 49 | // 在二维数组 matrix 中查找目标数 target 50 | + (bool)find:(int)target matrix:(NSArray *)matrix { 51 | if (matrix == nil || matrix.count == 0) { 52 | return false; 53 | } 54 | NSUInteger rows = matrix.count; // 行数 55 | NSArray *colArray = matrix[0]; 56 | NSUInteger cols = colArray.count; // 列数 57 | int r = 0; // 第 r 行 58 | int c = (int)cols - 1; // 第 c 列 从右上角开始 59 | 60 | while (r <= rows - 1 && c >= 0) { 61 | if (target == [matrix vFI:r j:c]) { 62 | NSLog(@"target = %d, isFind row = %d, col = %d",target,r,c); 63 | return true; 64 | } else if (target > [matrix vFI:r j:c]) { 65 | r++; // 行数+1 66 | } else { 67 | c--; // 列数减1 68 | } 69 | } 70 | return false; 71 | } 72 | 73 | 74 | @end 75 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/TwoStackQueue.h: -------------------------------------------------------------------------------- 1 | // 2 | // TwoStackQueue.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/9. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 用两个栈实现队列 13 | */ 14 | @interface TwoStackQueue : NSObject 15 | 16 | /** 17 | 用两个栈来实现一个队列,完成队列的 Push 和 Pop 操作。 18 | 19 | @param numbers 需要入栈的数据 20 | @return 出栈 21 | */ 22 | + (NSNumber *)twoStackToQueue:(NSArray *)numbers; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/1-10/TwoStackQueue.m: -------------------------------------------------------------------------------- 1 | // 2 | // TwoStackQueue.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/9. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "TwoStackQueue.h" 10 | #import "Stack.h" 11 | 12 | /** 13 | 题目描述:用两个栈来实现一个队列,完成队列的 Push 和 Pop 操作。 14 | 解题思路:in 栈用来处理入栈(push)操作,out 栈用来处理出栈(pop)操作。一个元素进入 in 栈之后,出栈的顺序被反转。当元素要出栈时,需要先进入 out 栈,此时元素出栈顺序再一次被反转,因此出栈顺序就和最开始入栈顺序是相同的,先进入的元素先退出,这就是队列的顺序。 15 | */ 16 | 17 | @implementation TwoStackQueue 18 | 19 | + (NSNumber *)twoStackToQueue:(NSArray *)numbers { 20 | Stack *inStack = [[Stack alloc] init]; 21 | Stack *outStack = [[Stack alloc] init]; 22 | 23 | // 先将所有元素入栈 24 | for (NSNumber *number in numbers) { 25 | [inStack push:number]; 26 | } 27 | 28 | if (outStack.isEmpty) { 29 | while (!inStack.isEmpty) { 30 | [outStack push:inStack.popObj]; 31 | } 32 | } 33 | 34 | if (outStack.isEmpty) { 35 | NSLog(@"queue is empty"); 36 | } 37 | 38 | return outStack.popObj; 39 | } 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/DeleteNode_18.h: -------------------------------------------------------------------------------- 1 | // 2 | // DeleteNode_18.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/25. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 在 O(1) 时间内删除链表节点 13 | */ 14 | @interface DeleteNode_18 : NSObject 15 | 16 | /** 删除节点 */ 17 | - (void)deleteNode; 18 | 19 | /** 删除 重复节点 */ 20 | - (void)deleteDuplicationNode; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/DeleteNode_18.m: -------------------------------------------------------------------------------- 1 | // 2 | // DeleteNode_18.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/25. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "DeleteNode_18.h" 10 | #import "LinkedArray.h" 11 | 12 | /** 13 | 解题思路: 14 | 1.如果该节点不是尾节点,那么可以直接将下一个节点的值赋给该节点,然后令该节点指向下下个节点,再删除下一个节点,时间复杂度为 O(1)。 15 | 2.否则,就需要先遍历链表,找到节点的前一个节点,然后让前一个节点指向 null,时间复杂度为 O(N)。 16 | 17 | 综上,如果进行 N 次操作,那么大约需要操作节点的次数为 N-1+N=2N-1, 18 | 其中 N-1 表示 N-1 个不是尾节点的每个节点以 O(1) 的时间复杂度操作节点的总次数,N 表示 1 个尾节点以 O(N) 的时间复杂度操作节点的总次数。 19 | (2N-1)/N ~ 2,因此该算法的平均时间复杂度为 O(1)。 20 | */ 21 | @implementation DeleteNode_18 22 | 23 | /** 删除节点 */ 24 | - (void)deleteNode { 25 | NSMutableArray *numbers = [NSMutableArray array]; 26 | for (int i = 0; i < 10; i++) { 27 | [numbers addObject:@(i)]; 28 | } 29 | LinkedArray *linkArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 30 | ListNode *headNode = [linkArray getFirstListNode]; 31 | ListNode *lastDelNode = [linkArray getLastListNode]; 32 | NSLog(@"---------------原始链表数据---------------"); 33 | [headNode printAllListNode]; 34 | 35 | ListNode *newHeadNode = [self deleteNode:headNode tagDelNode:lastDelNode]; 36 | NSLog(@"---------------删除后链表数据---------------"); 37 | [newHeadNode printAllListNode]; 38 | } 39 | 40 | - (ListNode *)deleteNode:(ListNode *)headNode tagDelNode:(ListNode *)tagDelNode { 41 | // 头节点和要删除的节点为空 42 | if (headNode == nil || tagDelNode == nil) { 43 | return nil; 44 | } 45 | 46 | if (tagDelNode.next != nil) { // 要删除的节点不是尾节点 47 | ListNode *next = tagDelNode.next; 48 | tagDelNode.content = next.content; 49 | tagDelNode.next = next.next; 50 | } else { 51 | ListNode *cur = headNode; 52 | while (cur.next != tagDelNode) { 53 | cur = cur.next; 54 | } 55 | cur.next = nil; 56 | } 57 | return headNode; 58 | } 59 | 60 | /** 删除 重复节点 */ 61 | - (void)deleteDuplicationNode { 62 | NSMutableArray *numbers = [NSMutableArray array]; 63 | for (int i = 0; i < 10; i++) { 64 | if (i / 2 == 1) { 65 | [numbers addObject:@(2)]; 66 | } else if (i / 3 == 1) { 67 | [numbers addObject:@(3)]; 68 | } else { 69 | [numbers addObject:@(i)]; 70 | } 71 | } 72 | LinkedArray *linkArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 73 | NSLog(@"----------------原始链表数据----------------"); 74 | [linkArray printAllListNode]; 75 | ListNode *firstNode = [linkArray getFirstListNode]; 76 | ListNode *headNode = [self deleteDuplicationListNode:firstNode]; 77 | NSLog(@"----------------删除重复节点后的链表数据----------------"); 78 | [headNode printAllListNode]; 79 | } 80 | 81 | // 删除重复节点 82 | - (ListNode *)deleteDuplicationListNode:(ListNode *)pHeadNode { 83 | if (pHeadNode == nil || pHeadNode.next == nil) { 84 | return pHeadNode; 85 | } 86 | 87 | ListNode *next = pHeadNode.next; 88 | if (pHeadNode.value == next.value) { 89 | while (next != nil && pHeadNode.value == next.value) { 90 | next = next.next; 91 | } 92 | return [self deleteDuplicationListNode:next]; 93 | } else { 94 | pHeadNode.next = [self deleteDuplicationListNode:pHeadNode.next]; 95 | return pHeadNode; 96 | } 97 | } 98 | 99 | @end 100 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/IsNumeric_20.h: -------------------------------------------------------------------------------- 1 | // 2 | // IsNumeric_20.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 表示数值的字符串 - 题目不明确 13 | */ 14 | @interface IsNumeric_20 : NSObject 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/IsNumeric_20.m: -------------------------------------------------------------------------------- 1 | // 2 | // IsNumeric_20.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "IsNumeric_20.h" 10 | 11 | /** 12 | 表示数值的字符串 13 | 14 | 题目描述: 15 | true:"+100", "5e2", "-123", "3.1416", "-1E-16" 16 | false:"12e", "1a3.14", "1.2.3", "+-5", "12e+4.3" 17 | 18 | 解题思路:使用正则表达式进行匹配。 19 | [] : 字符集合 20 | () : 分组 21 | ? : 重复 0 ~ 1 22 | + : 重复 1 ~ n 23 | * : 重复 0 ~ n 24 | . : 任意字符 25 | \\. : 转义后的 . 26 | \\d : 数字 27 | */ 28 | @implementation IsNumeric_20 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/MatrixHasPath.h: -------------------------------------------------------------------------------- 1 | // 2 | // MatrixHasPath.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/17. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 矩阵中的路径 13 | */ 14 | @interface MatrixHasPath : NSObject 15 | 16 | - (BOOL)hasPath:(NSString *)oriStr rows:(int)rows cols:(int)cols str:(NSString *)str; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/MatrixHasPath.m: -------------------------------------------------------------------------------- 1 | // 2 | // MatrixHasPath.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/17. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "MatrixHasPath.h" 10 | 11 | /** 12 | 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 13 | 例如下面的矩阵包含了一条 bfce 路径。 14 | a b t g 15 | c f c s 16 | j d e h 17 | */ 18 | 19 | @implementation MatrixHasPath { 20 | NSArray *_next; 21 | int _rows; 22 | int _cols; 23 | } 24 | 25 | - (instancetype)init { 26 | self = [super init]; 27 | if (self) { 28 | // 上 下 左 右 四个方向 29 | _next = @[@[@0,@(-1)],@[@0,@1],@[@(-1),@0],@[@1,@0]]; 30 | } 31 | return self; 32 | } 33 | 34 | - (BOOL)hasPath:(NSString *)oriStr rows:(int)rows cols:(int)cols str:(NSString *)str { 35 | if (rows == 0 || cols == 0) { 36 | return NO; 37 | } 38 | _rows = rows; 39 | _cols = cols; 40 | 41 | // 1.先构造一个二维数组,数值都填充为0,表示没有遍历过 42 | NSMutableArray *marked = [NSMutableArray array]; 43 | for (int i = 0; i < _rows; i++) { 44 | NSMutableArray *firstArrM = [NSMutableArray array]; 45 | for (int j = 0; j < _cols; j++) { 46 | [firstArrM addObject:@0]; 47 | } 48 | [marked addObject:firstArrM]; 49 | } 50 | 51 | // 2.构造一个矩阵 array-完整路径的数组,即a,b,t,g,c...h,并且是一个 rows * cols 的矩阵 52 | NSMutableArray *oriStrs = [NSMutableArray array]; 53 | for (int i = 0; i < oriStr.length; i++) { 54 | [oriStrs addObject:[oriStr substringWithRange:NSMakeRange(i, 1)]]; 55 | } 56 | NSMutableArray *matrix = [self buildMatrix:oriStrs.copy]; 57 | 58 | // 一条完整路径的数组 59 | NSMutableArray *strs = [NSMutableArray array]; 60 | for (int i = 0; i < str.length; i++) { 61 | [strs addObject:[str substringWithRange:NSMakeRange(i, 1)]]; 62 | } 63 | 64 | for (int i = 0; i < rows; i++) { 65 | for (int j = 0; j < cols; j++) { 66 | if ([self backetracking:matrix strs:strs marked:marked pathLen:0 r:i c:j]) { 67 | return YES; 68 | } 69 | } 70 | } 71 | 72 | return NO; 73 | } 74 | 75 | - (bool)backetracking:(NSArray *)matrix strs:(NSArray *)strs marked:(NSArray *)marked pathLen:(int)pathLen r:(int)r c:(int)c { 76 | // 0.即路径长度和字符串长度相等 77 | if (pathLen == strs.count) { 78 | NSLog(@"路径:\n%@",[marked getAllObjectsDescription]); 79 | return YES; 80 | } 81 | // r < 0 || r >= _rows || c < 0 || c >= _cols || ![oriPathStr isEqualToString:tarPathStr] || marked[r][c] 82 | if (r < 0 || r >= _rows || c < 0 || c >= _cols || matrix[r][c] != strs[pathLen] || [marked[r][c] intValue]) { 83 | // 原路径 84 | return NO; 85 | } 86 | marked[r][c] = @1; 87 | for (NSArray *next in _next) { 88 | int next0 = [(NSNumber *)(next[0]) intValue]; 89 | int next1 = [(NSNumber *)(next[1]) intValue]; 90 | if ([self backetracking:matrix strs:strs marked:marked pathLen:pathLen + 1 r:(r + next0) c:(c + next1)]) { 91 | return YES; 92 | } 93 | } 94 | marked[r][c] = @0; 95 | return NO; 96 | } 97 | 98 | /** 99 | 构造一个矩阵 array-完整路径的数组,即a,b,t,g,c...h,并且是一个 rows * cols 的矩阵 100 | a b t g 101 | c f c s 102 | j d e h 103 | */ 104 | - (NSMutableArray *)buildMatrix:(NSArray *)array { 105 | // 0.判断是否有值 106 | if (_rows == 0 || _cols == 0) { 107 | return nil; 108 | } 109 | if (array.count < _rows * _cols) { 110 | return nil; 111 | } 112 | 113 | // 1.先构造一个二维数组 114 | NSMutableArray *secondArrM = [NSMutableArray array]; 115 | for (int i = 0; i < _rows; i++) { 116 | NSMutableArray *firstArrM = [NSMutableArray array]; 117 | for (int j = 0; j < _cols; j++) { 118 | [firstArrM addObject:@""]; 119 | } 120 | [secondArrM addObject:firstArrM]; 121 | } 122 | 123 | // 2.依次将值填充进数组中 124 | int idx = 0; 125 | for (int i = 0; i < _rows; i++) { 126 | for (int j = 0; j < _cols; j++) { 127 | secondArrM[i][j] = array[idx++]; 128 | } 129 | } 130 | 131 | return secondArrM; 132 | } 133 | @end 134 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/MinNumberInRotateArray.h: -------------------------------------------------------------------------------- 1 | // 2 | // MinNumberInRotateArray.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/11. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 旋转数组的最小数字 13 | */ 14 | @interface MinNumberInRotateArray : NSObject 15 | 16 | /** 折半查找最小值 */ 17 | + (int)minNumberInRotateArray:(NSArray *)nums; 18 | 19 | /** 折半查找 - 带重复数字*/ 20 | + (int)minNumberInRotateArray2:(NSArray *)nums; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/MinNumberInRotateArray.m: -------------------------------------------------------------------------------- 1 | // 2 | // MinNumberInRotateArray.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/11. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "MinNumberInRotateArray.h" 10 | 11 | /** 12 | 题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 13 | 例如数组 {3, 4, 5, 1, 2} 为 {1, 2, 3, 4, 5} 的一个旋转,该数组的最小值为 1。 14 | 15 | 解题思路:在一个有序数组中查找一个元素可以用二分查找,二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度都为 O(logN)。 16 | 本题可以修改二分查找算法进行求解: 17 | 1.当 nums[m] <= nums[h] 的情况下,说明解在 [l, m] 之间,此时令 h = m; 18 | 2.否则解在 [m + 1, h] 之间,令 l = m + 1。 19 | */ 20 | @implementation MinNumberInRotateArray 21 | 22 | /** 折半查找最小值 */ 23 | + (int)minNumberInRotateArray:(NSArray *)nums { 24 | if (nums.count == 0) { 25 | return 0; 26 | } 27 | int l = 0, h = (int)nums.count - 1; 28 | while (l < h) { 29 | int m = l + (h - l) / 2; // 中间值 30 | if ([nums[m] intValue] <= [nums[h] intValue]) { 31 | h = m; 32 | } else { 33 | l = m + 1; 34 | } 35 | } 36 | 37 | return [nums[l] intValue]; 38 | } 39 | 40 | /** 41 | 如果数组元素允许重复的话,那么就会出现一个特殊的情况:nums[l] == nums[m] == nums[h], 42 | 那么此时无法确定解在哪个区间,需要切换到顺序查找。例如对于数组 {1,1,1,0,1},l、m 和 h 指向的数都为 1, 43 | 此时无法知道最小数字 0 在哪个区间。 44 | */ 45 | + (int)minNumberInRotateArray2:(NSArray *)nums { 46 | if (nums.count == 0) { 47 | return 0; 48 | } 49 | int l = 0, h = (int)nums.count - 1; 50 | while (l < h) { 51 | int m = l + (h - l) / 2; // 中间值 52 | if (nums[l] == nums[m] && nums[m] == nums[h]) { // 三者相等 53 | return [self minNumber:nums l:l h:h]; 54 | } else if ([nums[m] intValue] <= [nums[h] intValue]) { 55 | h = m; 56 | } else { 57 | l = m + 1; 58 | } 59 | } 60 | 61 | return [nums[l] intValue]; 62 | } 63 | 64 | + (int)minNumber:(NSArray *)nums l:(int)l h:(int)h { 65 | for (int i = l; i < h; i++) { 66 | if ([nums[i] intValue] > [nums[i + 1] intValue]) { 67 | return [nums[i + 1] intValue]; 68 | } 69 | } 70 | return [nums[l] intValue]; 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/MovingCount_13.h: -------------------------------------------------------------------------------- 1 | // 2 | // MovingCount_13.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/22. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 机器人的运动范围 - 还有问题,待修正 13 | */ 14 | @interface MovingCount_13 : NSObject 15 | 16 | - (int)movintCount:(int)threshold rows:(int)rows cols:(int)cols; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/MovingCount_13.m: -------------------------------------------------------------------------------- 1 | // 2 | // MovingCount_13.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/22. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "MovingCount_13.h" 10 | 11 | /** 12 | 题目描述: 13 | 地上有一个 m 行和 n 列的方格。一个机器人从坐标 (0, 0) 的格子开始移动,每一次只能向左右上下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于 k 的格子。 14 | 例如,当 k 为 18 时,机器人能够进入方格 (35,37),因为 3+5+3+7=18。但是,它不能进入方格 (35,37),因为 3+5+3+8=19。请问该机器人能够达到多少个格子? 15 | */ 16 | @implementation MovingCount_13 { 17 | NSArray *_next; 18 | int _count; 19 | int _rows; 20 | int _cols; 21 | int _threshold; // 行坐标和列坐标的数位之和小于等于于_threshold 22 | NSMutableArray *_digitSum; 23 | } 24 | 25 | - (instancetype)init { 26 | self = [super init]; 27 | if (self) { 28 | _next = @[@[@0,@(-1)],@[@0,@1],@[@(-1),@0],@[@1,@0]]; 29 | } 30 | return self; 31 | } 32 | 33 | - (int)movintCount:(int)threshold rows:(int)rows cols:(int)cols { 34 | _rows = rows; 35 | _cols = cols; 36 | _threshold = threshold; 37 | [self initDigitSum]; 38 | 39 | NSMutableArray *marked = [NSMutableArray array]; 40 | for (int i = 0; i < _rows; i++) { 41 | NSMutableArray *firstArrM = [NSMutableArray array]; 42 | for (int j = 0; j < _cols; j++) { 43 | [firstArrM addObject:@0]; 44 | } 45 | [marked addObject:firstArrM]; 46 | } 47 | [self dfs:marked r:0 c:0]; 48 | return _count; 49 | } 50 | 51 | - (void)dfs:(NSMutableArray *)marked r:(int)r c:(int)c { 52 | if (r < 0 || r >= _rows || c < 0 || c >= _cols || [marked[r][c] intValue]) { 53 | return; 54 | } 55 | marked[r][c] = @1; 56 | if ([_digitSum[r][c] intValue] > _threshold) { 57 | return; 58 | } 59 | _count++; 60 | for (NSArray *next in _next) { 61 | int next0 = [(NSNumber *)(next[0]) intValue]; 62 | int next1 = [(NSNumber *)(next[1]) intValue]; 63 | [self dfs:marked r:r + next0 c:c + next1]; 64 | } 65 | } 66 | 67 | - (void)initDigitSum { 68 | int maxNumber = MAX(_rows, _cols); 69 | NSMutableArray *numbers = [NSMutableArray array]; 70 | for (int i = 0; i < maxNumber; i++) { 71 | [numbers addObject:@0]; 72 | } 73 | 74 | for (int i = 0; i < numbers.count; i++) { 75 | int n = i; 76 | while (n > 0) { 77 | numbers[i] = @([numbers[i] intValue] + n % 10); 78 | n /= 10; 79 | } 80 | } 81 | 82 | // 初始化原始数组 83 | if (_digitSum == nil) { 84 | _digitSum = [NSMutableArray array]; 85 | for (int i = 0; i < _rows; i++) { 86 | NSMutableArray *firstArrM = [NSMutableArray array]; 87 | for (int j = 0; j < _cols; j++) { 88 | [firstArrM addObject:@0]; 89 | } 90 | [_digitSum addObject:firstArrM]; 91 | } 92 | } 93 | 94 | // 赋值 95 | for (int i = 0; i < _rows; i++) { 96 | for (int j = 0; j < _cols; j++) { 97 | _digitSum[i][j] = @([numbers[i] intValue] + [numbers[j] intValue]); 98 | } 99 | } 100 | } 101 | 102 | @end 103 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/NumberOfOne_15.h: -------------------------------------------------------------------------------- 1 | // 2 | // NumberOfOne_15.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/24. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 二进制中 1 的个数 13 | */ 14 | @interface NumberOfOne_15 : NSObject 15 | 16 | /** 输入一个整数,输出该数二进制表示中 1 的个数。*/ 17 | + (int)numberOfOne:(int)number; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/NumberOfOne_15.m: -------------------------------------------------------------------------------- 1 | // 2 | // NumberOfOne_15.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/24. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "NumberOfOne_15.h" 10 | 11 | /** 12 | 题目描述:二进制中 1 的个数 13 | 解题思路:n&(n-1) 该位运算去除 n 的位级表示中最低的那一位。 14 | 时间复杂度:O(M),其中 M 表示 1 的个数。 15 | */ 16 | @implementation NumberOfOne_15 17 | 18 | /** 输入一个整数,输出该数二进制表示中 1 的个数。*/ 19 | + (int)numberOfOne:(int)number { 20 | int cnt = 0; 21 | while (number != 0) { 22 | cnt++; 23 | number &= (number - 1); 24 | } 25 | return cnt; 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/RegularExpressionMath_19.h: -------------------------------------------------------------------------------- 1 | // 2 | // RegularExpressionMath_19.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 正则表达式匹配 - 题目有错 13 | */ 14 | @interface RegularExpressionMath_19 : NSObject 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/RegularExpressionMath_19.m: -------------------------------------------------------------------------------- 1 | // 2 | // RegularExpressionMath_19.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "RegularExpressionMath_19.h" 10 | 11 | /** 12 | 正则表达式匹配 13 | 14 | 题目描述: 15 | 1.请实现一个函数用来匹配包括 '.' 和 '*' 的正则表达式。模式中的字符 '.' 表示任意一个字符,而 '*' 表示它前面的字符可以出现任意次(包含 0 次)。 16 | 2.在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串 "aaa" 与模式 "a.a" 和 "ab*ac*a" 匹配,但是与 "aa.a" 和 "ab*a" 均不匹配。 17 | 18 | 解题思路:应该注意到,'.' 是用来当做一个任意字符,而 '*' 是用来重复前面的字符。这两个的作用不同,不能把 '.' 的作用和 '*' 进行类比,从而把它当成重复前面字符一次。 19 | */ 20 | @implementation RegularExpressionMath_19 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/power_16.h: -------------------------------------------------------------------------------- 1 | // 2 | // power_16.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/24. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 数值的整数次方 13 | */ 14 | @interface power_16 : NSObject 15 | 16 | /** 数值的整数次方 */ 17 | + (double)power:(double)base exponent:(int)exponent; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/power_16.m: -------------------------------------------------------------------------------- 1 | // 2 | // power_16.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/24. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "power_16.h" 10 | 11 | /** 12 | 题目描述:给定一个 double 类型的浮点数 base 和 int 类型的整数 exponent,求 base 的 exponent 次方。 13 | 解题思路:下面的讨论中 x 代表 base,n 代表 exponent。 14 | 因为 (x*x)n/2 可以通过递归求解,并且每次递归 n 都减小一半,因此整个算法的时间复杂度为 O(logN)。 15 | */ 16 | @implementation power_16 17 | 18 | /** 数值的整数次方 */ 19 | + (double)power:(double)base exponent:(int)exponent { 20 | if (exponent == 0) { 21 | return 1; 22 | } 23 | if (exponent == 1) { 24 | return base; 25 | } 26 | bool isNegative = NO; 27 | if (exponent < 0) { 28 | exponent -= exponent; 29 | isNegative = YES; 30 | } 31 | double pow = [self power:base * base exponent:exponent / 2]; 32 | if (exponent % 2 != 0) { 33 | pow = pow * base; 34 | } 35 | return isNegative ? 1 / pow : pow; 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/printOneToMaxOfNDigits_17.h: -------------------------------------------------------------------------------- 1 | // 2 | // printOneToMaxOfNDigits_17.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/24. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 打印从 1 到最大的 n 位数 - 未完成 13 | */ 14 | @interface printOneToMaxOfNDigits_17 : NSObject 15 | 16 | /** 打印从 1 到最大的 n 位数 */ 17 | - (void)printOneToMaxOfNDigits:(int)number; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/11-20/printOneToMaxOfNDigits_17.m: -------------------------------------------------------------------------------- 1 | // 2 | // printOneToMaxOfNDigits_17.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/24. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "printOneToMaxOfNDigits_17.h" 10 | 11 | /** 12 | 题目描述:输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数即 999。 13 | 解题思路:1.由于 n 可能会非常大,因此不能直接用 int 表示数字,而是用 char 数组进行存储。 14 | 2.使用回溯法得到所有的数。 15 | */ 16 | @implementation printOneToMaxOfNDigits_17 17 | 18 | /** 打印从 1 到最大的 n 位数 */ 19 | - (void)printOneToMaxOfNDigits:(int)number { 20 | if (number <= 0) { 21 | return; 22 | } 23 | 24 | } 25 | 26 | - (void)printOneToMaxOfNDigits:(NSArray *)numbers digit:(int)digit { 27 | if (digit == numbers.count) { 28 | [self printNumber:numbers]; 29 | return; 30 | } 31 | 32 | } 33 | 34 | - (void)printNumber:(NSArray *)numbers { 35 | for (NSString *str in numbers) { 36 | NSLog(@"str = %@",str); 37 | } 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/EntryNodeOfLoop_23.h: -------------------------------------------------------------------------------- 1 | // 2 | // EntryNodeOfLoop_23.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/31. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | @class ListNode; 11 | 12 | /** 13 | 链表中环的入口结点 - 较难理解 14 | */ 15 | @interface EntryNodeOfLoop_23 : NSObject 16 | 17 | + (ListNode *)entryNodeOfLoop; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/EntryNodeOfLoop_23.m: -------------------------------------------------------------------------------- 1 | // 2 | // EntryNodeOfLoop_23.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/31. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "EntryNodeOfLoop_23.h" 10 | #import "ListNode.h" 11 | #import "LinkedArray.h" 12 | 13 | /** 14 | 链表中环的入口结点 15 | 题目描述:一个链表中包含环,请找出该链表的环的入口结点。要求不能使用额外的空间。 16 | 17 | 解题思路: 18 | 使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 y6 位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。 19 | 20 | 在相遇点,slow 要到环的入口点还需要移动 z 个节点,如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,因此 fast 和 slow 将在环入口点相遇。 21 | */ 22 | @implementation EntryNodeOfLoop_23 23 | 24 | + (ListNode *)entryNodeOfLoop { 25 | // 构造闭环链表 26 | NSMutableArray *numbers = [NSMutableArray array]; 27 | for (int i = 0; i < 10; i++) { 28 | [numbers addObject:[NSNumber numberWithInt:i]]; 29 | } 30 | // 1.构造闭环链表 31 | LinkedArray *nodeList = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 32 | ListNode *pHead = [nodeList getFirstListNode]; 33 | ListNode *pLast = [nodeList getLastListNode]; 34 | pLast.next = pHead; // 构造一个闭环 35 | // 入口节点 36 | ListNode *entryNode = [nodeList getListNodeAtIndex:5]; 37 | 38 | // 构造入口节点 39 | ListNode *firstNode = [[ListNode alloc] init]; 40 | firstNode.content = [NSNumber numberWithInt:20]; 41 | ListNode *secondNode = [[ListNode alloc] init]; 42 | secondNode.content = [NSNumber numberWithInt:30]; 43 | firstNode.next = secondNode; 44 | 45 | // 联通入口节点 46 | secondNode.next = entryNode; 47 | 48 | if (pHead == nil || pHead.next == nil) { 49 | return nil; 50 | } 51 | 52 | ListNode *slow = firstNode; 53 | ListNode *fast = firstNode; 54 | 55 | do { 56 | fast = fast.next.next; 57 | slow = slow.next; 58 | } while (slow != fast); 59 | 60 | // 让 fast 重新从头开始移动 61 | fast = pHead; 62 | while (slow != fast) { 63 | slow = slow.next; 64 | fast = fast.next; 65 | } 66 | 67 | return slow; 68 | } 69 | 70 | @end 71 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/FindKthToTail_22.h: -------------------------------------------------------------------------------- 1 | // 2 | // FindKthToTail_22.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/30. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ListNode.h" 11 | 12 | /** 13 | 22.链表中倒数第 K 个结点 14 | */ 15 | @interface FindKthToTail_22 : NSObject 16 | 17 | + (ListNode *)findKthToTail:(NSArray *)numbers index:(int)index; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/FindKthToTail_22.m: -------------------------------------------------------------------------------- 1 | // 2 | // FindKthToTail_22.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/30. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "FindKthToTail_22.h" 10 | #import "LinkedArray.h" 11 | 12 | /** 13 | 22.链表中倒数第 K 个结点 14 | 解题思路:设链表的长度为 N。设两个指针 P1 和 P2,先让 P1 移动 K 个节点,则还有 N - K 个节点可以移动。此时让 P1 和 P2 同时移动,可以知道当 P1 移动到链表结尾时,P2 移动到 N - K 个节点处,该位置就是倒数第 K 个节点。 15 | */ 16 | @implementation FindKthToTail_22 17 | 18 | + (ListNode *)findKthToTail:(NSArray *)numbers index:(int)index { 19 | if (index >= numbers.count) { 20 | return nil; 21 | } 22 | 23 | LinkedArray *linkList = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 24 | ListNode *headNode = [linkList getFirstListNode]; 25 | 26 | if (headNode == nil) { // 头结点为空 27 | return nil; 28 | } 29 | 30 | ListNode *p1 = headNode; 31 | while (p1 != nil && index-- > 0) { 32 | p1 = p1.next; 33 | } 34 | 35 | if (index > 0) { // 表示 index 的值大于链表的长度 36 | return nil; 37 | } 38 | 39 | ListNode *p2 = headNode; 40 | // 此时让 P1 和 P2 同时移动,可以知道当 P1 移动到链表结尾时,P2 移动到 N - K 个节点处,该位置就是倒数第 K 个节点。 41 | while (p1 != nil) { 42 | p1 = p1.next; 43 | p2 = p2.next; 44 | } 45 | 46 | return p2; 47 | } 48 | @end 49 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/HasSubTree_26.h: -------------------------------------------------------------------------------- 1 | // 2 | // HasSubTree_26.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/6. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 树的子结构 13 | */ 14 | @interface HasSubTree_26 : NSObject 15 | 16 | /// 是否有子树 17 | + (bool)hasSubTree; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/HasSubTree_26.m: -------------------------------------------------------------------------------- 1 | // 2 | // HasSubTree_26.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/6. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "HasSubTree_26.h" 10 | 11 | @implementation HasSubTree_26 12 | 13 | /// 是否有子树 14 | + (bool)hasSubTree { 15 | // 构建一个二叉树 16 | return NO; 17 | } 18 | 19 | + (bool)isSubTreeWithRootNode:(BinaryTreeNode *)root1 root2:(BinaryTreeNode *)root2 { 20 | if (root2 == nil) { 21 | return YES; 22 | } 23 | if (root1 == nil){ 24 | return NO; 25 | } 26 | if (root1.value != root2.value) { 27 | return NO; 28 | } 29 | 30 | return [self isSubTreeWithRootNode:root1.leftNode root2:root2.leftNode] && [self isSubTreeWithRootNode:root1.rightNode root2:root2.rightNode]; 31 | } 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/IsSymmetrical_28.h: -------------------------------------------------------------------------------- 1 | // 2 | // IsSymmetrical_28.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/7. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 对称的二叉树 13 | */ 14 | @interface IsSymmetrical_28 : NSObject 15 | 16 | /// 对称的二叉树 17 | + (bool)isSymmetrical; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/IsSymmetrical_28.m: -------------------------------------------------------------------------------- 1 | // 2 | // IsSymmetrical_28.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/7. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "IsSymmetrical_28.h" 10 | 11 | @implementation IsSymmetrical_28 12 | 13 | /// 对称的二叉树 14 | + (bool)isSymmetrical { 15 | // 构建二叉树 16 | BinaryTreeNode *root = [BinaryTreeNode binaryTreeNodeWithValue:1]; 17 | BinaryTreeNode *rLeft = [BinaryTreeNode binaryTreeNodeWithValue:2]; 18 | BinaryTreeNode *rRight = [BinaryTreeNode binaryTreeNodeWithValue:2]; 19 | BinaryTreeNode *rLLeft = [BinaryTreeNode binaryTreeNodeWithValue:3]; 20 | BinaryTreeNode *rLRight = [BinaryTreeNode binaryTreeNodeWithValue:4]; 21 | BinaryTreeNode *rRLeft = [BinaryTreeNode binaryTreeNodeWithValue:4]; 22 | BinaryTreeNode *rRRight = [BinaryTreeNode binaryTreeNodeWithValue:3]; 23 | root.leftNode = rLeft; 24 | root.rightNode = rRight; 25 | rLeft.leftNode = rLLeft; 26 | rLeft.rightNode = rLRight; 27 | rRight.leftNode = rRLeft; 28 | rRight.rightNode = rRRight; 29 | 30 | return [self privateIsSymmetrical:root]; 31 | } 32 | 33 | + (bool)privateIsSymmetrical:(BinaryTreeNode *)pRoot { 34 | if (pRoot == nil) { 35 | return YES; 36 | } 37 | return [self privateIsSymmetrical:pRoot.leftNode t2:pRoot.rightNode]; 38 | } 39 | 40 | + (bool)privateIsSymmetrical:(BinaryTreeNode *)t1 t2:(BinaryTreeNode *)t2 { 41 | if (t1 == nil && t2 == nil) { 42 | return YES; 43 | } 44 | if (t1 == nil || t2 == nil) { 45 | return NO; 46 | } 47 | if (t1.value != t2.value) { 48 | return NO; 49 | } 50 | 51 | return [self privateIsSymmetrical:t1.leftNode t2:t2.rightNode] && [self privateIsSymmetrical:t1.rightNode t2:t2.leftNode]; 52 | } 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/MergeTwoListNode_25.h: -------------------------------------------------------------------------------- 1 | // 2 | // MergeTwoListNode_25.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/5. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 25. 合并两个排序的链表 13 | */ 14 | @interface MergeTwoListNode_25 : UIView 15 | 16 | /** 17 | 递归 合并两个排序的链表 18 | 19 | @param number1s 链表1 20 | @param numbers2 链表2 21 | @return 合并后的链表 22 | */ 23 | + (ListNode *)mergeByRecursionWith:(NSArray *)number1s numbers2:(NSArray *)numbers2; 24 | 25 | /** 26 | 迭代 合并两个排序的链表 27 | 28 | @param number1s 链表1 29 | @param numbers2 链表2 30 | @return 合并后的链表 31 | */ 32 | + (ListNode *)mergeByIterationWith:(NSArray *)number1s numbers2:(NSArray *)numbers2; 33 | 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/MergeTwoListNode_25.m: -------------------------------------------------------------------------------- 1 | // 2 | // MergeTwoListNode_25.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/5. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "MergeTwoListNode_25.h" 10 | 11 | @implementation MergeTwoListNode_25 12 | 13 | /** 14 | 递归 合并两个排序的链表 15 | 16 | @param number1s 链表1 17 | @param numbers2 链表2 18 | @return 合并后的链表 19 | */ 20 | + (ListNode *)mergeByRecursionWith:(NSArray *)number1s numbers2:(NSArray *)numbers2 { 21 | LinkedArray *list1 = [[LinkedArray alloc] initLiknedArrayWithNunbers:number1s]; 22 | ListNode *listNode1 = [list1 getFirstListNode]; 23 | LinkedArray *list2 = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers2]; 24 | ListNode *listNode2 = [list2 getFirstListNode]; 25 | 26 | return [self mergeByRecursionWith:listNode1 listNode2:listNode2]; 27 | } 28 | 29 | + (ListNode *)mergeByRecursionWith:(ListNode *)listNode1 listNode2:(ListNode *)listNode2 { 30 | if (listNode1 == nil) { 31 | return listNode2; 32 | } 33 | if (listNode2 == nil) { 34 | return listNode1; 35 | } 36 | if (listNode1.value <= listNode2.value) { 37 | listNode1.next = [self mergeByRecursionWith:listNode1.next listNode2:listNode2]; 38 | return listNode1; 39 | } else { 40 | listNode2.next = [self mergeByRecursionWith:listNode1 listNode2:listNode2.next]; 41 | return listNode2; 42 | } 43 | } 44 | 45 | /** 46 | 迭代 合并两个排序的链表 47 | 48 | @param number1s 链表1 49 | @param numbers2 链表2 50 | @return 合并后的链表 51 | */ 52 | + (ListNode *)mergeByIterationWith:(NSArray *)number1s numbers2:(NSArray *)numbers2 { 53 | LinkedArray *list1 = [[LinkedArray alloc] initLiknedArrayWithNunbers:number1s]; 54 | ListNode *listNode1 = [list1 getFirstListNode]; 55 | LinkedArray *list2 = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers2]; 56 | ListNode *listNode2 = [list2 getFirstListNode]; 57 | 58 | ListNode *headNode = [[ListNode alloc] init]; 59 | ListNode *curNode = headNode; 60 | 61 | while (listNode1 != nil && listNode2 != nil) { 62 | if (listNode1.value <= listNode2.value) { 63 | curNode.next = listNode1; 64 | listNode1 = listNode1.next; 65 | } else { 66 | curNode.next = listNode2; 67 | listNode2 = listNode2.next; 68 | } 69 | curNode = curNode.next; 70 | } 71 | if (listNode1 != nil) { 72 | curNode.next = listNode1; 73 | } 74 | if (listNode2 != nil) { 75 | curNode.next = listNode2; 76 | } 77 | 78 | return headNode.next; 79 | } 80 | @end 81 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/MirrorTree_27.h: -------------------------------------------------------------------------------- 1 | // 2 | // MirrorTree_27.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/7. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 27 二叉树的镜像 13 | */ 14 | @interface MirrorTree_27 : NSObject 15 | 16 | /** 二叉树的镜像 */ 17 | + (void)mirror; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/MirrorTree_27.m: -------------------------------------------------------------------------------- 1 | // 2 | // MirrorTree_27.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/7. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "MirrorTree_27.h" 10 | 11 | @implementation MirrorTree_27 12 | 13 | /** 二叉树的镜像 */ 14 | + (void)mirror { 15 | // 构建一颗二叉树 16 | BinaryTreeNode *root = [BinaryTreeNode binaryTreeNodeWithValue:1]; 17 | BinaryTreeNode *left = [BinaryTreeNode binaryTreeNodeWithValue:2]; 18 | BinaryTreeNode *right = [BinaryTreeNode binaryTreeNodeWithValue:3]; 19 | root.leftNode = left; 20 | root.rightNode = right; 21 | 22 | NSLog(@"交换前:%@",root); 23 | [self privateMirror:root]; 24 | NSLog(@"交换后:%@",root); 25 | } 26 | 27 | + (void)privateMirror:(BinaryTreeNode *)root { 28 | if (root == nil) { 29 | return; 30 | } 31 | // 交换左右节点 32 | [self swapTreeNode:root]; 33 | [self privateMirror:root.leftNode]; 34 | [self privateMirror:root.rightNode]; 35 | } 36 | 37 | + (void)swapTreeNode:(BinaryTreeNode *)root { 38 | BinaryTreeNode *t = root.leftNode; 39 | root.leftNode = root.rightNode; 40 | root.rightNode = t; 41 | } 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/PrintMatrixClockwise_29.h: -------------------------------------------------------------------------------- 1 | // 2 | // PrintMatrixClockwise_29.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/7. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 顺时针打印矩阵 13 | */ 14 | @interface PrintMatrixClockwise_29 : NSObject 15 | 16 | /** 17 | 顺时针打印矩阵 18 | 19 | @param matrix 二维数组 20 | */ 21 | + (NSArray *)printMatrix:(NSArray *)matrix; 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/PrintMatrixClockwise_29.m: -------------------------------------------------------------------------------- 1 | // 2 | // PrintMatrixClockwise_29.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/7. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "PrintMatrixClockwise_29.h" 10 | 11 | @implementation PrintMatrixClockwise_29 12 | 13 | /** 14 | 顺时针打印矩阵 15 | 16 | @param matrix 二维数组 17 | */ 18 | + (NSArray *)printMatrix:(NSArray *)matrix { 19 | // 先构造一个二维数组 20 | NSMutableArray *digitSum = [NSMutableArray array]; 21 | for (int i = 0; i < 4; i++) { 22 | NSMutableArray *firstArrM = [NSMutableArray array]; 23 | for (int j = 0; j < 4; j++) { 24 | [firstArrM addObject:matrix[i * 4 + j]]; 25 | } 26 | [digitSum addObject:firstArrM]; 27 | } 28 | 29 | // 初始化变量 30 | int r1 = 0, r2 = (int)(digitSum.count) - 1; // 行数 31 | NSArray *numbers = digitSum[0]; 32 | int c1 = 0, c2 = (int)(numbers.count) - 1; // 列数 33 | 34 | NSMutableArray *result = [NSMutableArray array]; 35 | // 开始循环遍历 36 | while (r1 <= r2 && c1 <= c2) { 37 | for (int i = c1; i <= c2; i++) { // 从左到右 38 | [result addObject:digitSum[r1][i]]; 39 | } 40 | for (int i = r1 + 1 ; i <= r2; i++) { // 从上到下 41 | [result addObject:digitSum[i][c2]]; 42 | } 43 | if (r1 != r2) { 44 | for (int i = c2 - 1; i >= c1; i--) { // 从右到左 45 | [result addObject:digitSum[r2][i]]; 46 | } 47 | } 48 | if (c1 != c2) { 49 | for (int i = r2 - 1; i > r1; i--) { // 从下到上 50 | [result addObject:digitSum[i][c1]]; 51 | } 52 | } 53 | 54 | r1++; 55 | r2--; 56 | c1++; 57 | c2--; 58 | } 59 | 60 | return result.copy; 61 | } 62 | 63 | @end 64 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/ReverseList_24.h: -------------------------------------------------------------------------------- 1 | // 2 | // ReverseList_24.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/1. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | @class ListNode; 11 | 12 | /** 13 | 24.反转链表 14 | */ 15 | @interface ReverseList_24 : NSObject 16 | 17 | // 反转链表 18 | + (ListNode *)reverseList:(NSArray *)numbers; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/ReverseList_24.m: -------------------------------------------------------------------------------- 1 | // 2 | // ReverseList_24.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/1. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "ReverseList_24.h" 10 | #import "LinkedArray.h" 11 | 12 | @implementation ReverseList_24 13 | 14 | // 反转链表 15 | + (ListNode *)reverseList:(NSArray *)numbers { 16 | LinkedArray *linkArray = [[LinkedArray alloc] initLiknedArrayWithNunbers:numbers]; 17 | ListNode *headNode = [linkArray getFirstListNode]; 18 | 19 | if (headNode == nil || headNode.next == nil) { 20 | return headNode; 21 | } 22 | 23 | ListNode *next = headNode.next; 24 | headNode.next = nil; 25 | ListNode *newHead = [self reverList:next]; 26 | next.next = headNode; 27 | 28 | return newHead; 29 | } 30 | 31 | + (ListNode *)reverList:(ListNode *)head { 32 | ListNode *newList = [[ListNode alloc] init]; 33 | while (head != nil) { 34 | ListNode *next = head.next; 35 | head.next = newList.next; 36 | newList.next = head; 37 | head = next; 38 | } 39 | return newList.next; 40 | } 41 | @end 42 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/reOrderArray_21.h: -------------------------------------------------------------------------------- 1 | // 2 | // reOrderArray_21.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/29. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 21.调整数组顺序使奇数位于偶数前面 13 | */ 14 | @interface reOrderArray_21 : NSObject 15 | 16 | + (NSArray *)reOrderArray:(NSArray *)numbers; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/21-30/reOrderArray_21.m: -------------------------------------------------------------------------------- 1 | // 2 | // reOrderArray_21.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/10/29. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "reOrderArray_21.h" 10 | 11 | /** 12 | 21.调整数组顺序使奇数位于偶数前面 13 | 题目描述:需要保证奇数和奇数,偶数和偶数之间的相对位置不变,这和书本不太一样。 14 | */ 15 | @implementation reOrderArray_21 16 | 17 | + (NSArray *)reOrderArray:(NSArray *)numbers { 18 | if (numbers.count == 0) { 19 | return nil; 20 | } 21 | int oddCnt = 0; // 奇数个数 22 | for (int i = 0; i < numbers.count; i++) { 23 | if ([numbers vFI:i] % 2 == 1) { 24 | oddCnt++; 25 | } 26 | } 27 | NSMutableArray *numberM = [NSMutableArray arrayWithArray:numbers]; 28 | int i = 0, j = oddCnt; 29 | for (int k = 0; k < numbers.count; k++) { 30 | NSNumber *number = [numbers objectAtIndex:k]; 31 | if (number.intValue % 2 == 1) { 32 | numberM[i++] = number; // 奇数从0开始排序 33 | } else { 34 | numberM[j++] = number; // 偶数从 oddCnt 开始排序,因为奇数的个数为 oddCnt 个. 35 | } 36 | } 37 | 38 | return numberM.copy; 39 | } 40 | @end 41 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/Backtracking_34.h: -------------------------------------------------------------------------------- 1 | // 2 | // Backtracking_34.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/15. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 34._reviewRating 13 | */ 14 | @interface Backtracking_34 : NSObject 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/Backtracking_34.m: -------------------------------------------------------------------------------- 1 | // 2 | // Backtracking_34.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/15. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "Backtracking_34.h" 10 | 11 | /** 12 | 题目描述:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 13 | 14 | */ 15 | @implementation Backtracking_34 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/FindKthSmallest_40.h: -------------------------------------------------------------------------------- 1 | // 2 | // FindKthSmallest_40.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/29. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 40.最小的 K 个数 13 | */ 14 | @interface FindKthSmallest_40 : NSObject 15 | 16 | + (NSArray *)getLeasetNumbersSolution:(NSMutableArray *)nums k:(int)k; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/FindKthSmallest_40.m: -------------------------------------------------------------------------------- 1 | // 2 | // FindKthSmallest_40.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/29. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "FindKthSmallest_40.h" 10 | 11 | /** 12 | 40.最小的 K 个数 13 | 解题思路: 14 | 快速选择:1.复杂度:O(N) + O(1) 2.只有当允许修改数组元素时才可以使用 15 | 备注:快速排序的 partition() 方法,会返回一个整数 j 使得 a[l..j-1] 小于等于 a[j],且 a[j+1..h] 大于等于 a[j],此时 a[j] 就是数组的第 j 大元素。可以利用这个特性找出数组的第 K 个元素,这种找第 K 个元素的算法称为快速选择算法。 16 | 17 | */ 18 | 19 | @implementation FindKthSmallest_40 20 | 21 | + (NSArray *)getLeasetNumbersSolution:(NSMutableArray *)nums k:(int)k { 22 | NSMutableArray *ret = [NSMutableArray array]; 23 | if (k > nums.count || k <= 0) { 24 | return ret; 25 | } 26 | [self findKthSmallest:nums k:k]; 27 | 28 | // findKthSmallest 会改变数组,使得前 k 个数都是最小的 k 个数 29 | for (int i = 0; i < k; i++) { 30 | [ret addObject:nums[i]]; 31 | } 32 | 33 | return ret; 34 | } 35 | 36 | + (void)findKthSmallest:(NSMutableArray *)nums k:(int)k { 37 | int l = 0, h = (int)nums.count - 1; 38 | while (l < h) { 39 | int j = [self partition:nums l:l h:h]; 40 | if (j == k) { 41 | break; 42 | } 43 | if (j > k) { 44 | h = j - 1; 45 | } else { 46 | l = j + 1; 47 | } 48 | } 49 | } 50 | 51 | // 快速排序 - 切分元素 52 | + (int)partition:(NSMutableArray *)nums l:(int)l h:(int)h { 53 | int p = [nums vFI:l]; // 切分元素 54 | int i = l, j = h + 1; 55 | while (true) { 56 | while (i != h && [nums vFI:++i] < p); 57 | while (j != l && [nums vFI:--j] > p); 58 | 59 | if (i >= j) { 60 | break; 61 | } 62 | [self swap:nums i:i j:j]; 63 | } 64 | [self swap:nums i:l j:j]; 65 | return j; 66 | } 67 | 68 | + (void)swap:(NSMutableArray *)nums i:(int)i j:(int)j { 69 | NSNumber *t = nums[i]; 70 | nums[i] = nums[j]; 71 | nums[j] = t; 72 | } 73 | 74 | @end 75 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/InOrder_36.h: -------------------------------------------------------------------------------- 1 | // 2 | // InOrder_36.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/19. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 36.二叉搜索树与双向链表 13 | */ 14 | @interface InOrder_36 : NSObject 15 | 16 | + (TreeNode *)convert; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/InOrder_36.m: -------------------------------------------------------------------------------- 1 | // 2 | // InOrder_36.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/19. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "InOrder_36.h" 10 | 11 | static TreeNode *pre = nil; 12 | static TreeNode *head = nil; 13 | 14 | /** 15 | 题目描述:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 16 | */ 17 | @implementation InOrder_36 18 | 19 | + (TreeNode *)convert { 20 | TreeNode *root = nil; 21 | [self inOrder:root]; 22 | return head; 23 | } 24 | 25 | + (void)inOrder:(TreeNode *)node { 26 | if (node == nil) { 27 | return; 28 | } 29 | [self inOrder:node.leftNode]; 30 | node.leftNode = pre; 31 | 32 | if (pre != nil) { 33 | pre.rightNode = node; 34 | } 35 | pre = node; 36 | if (head == nil) { 37 | head = node; 38 | } 39 | [self inOrder:node.rightNode]; 40 | } 41 | 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/IsPopOrder_31.h: -------------------------------------------------------------------------------- 1 | // 2 | // IsPopOrder_31.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/12. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 31.栈的压入、弹出序列 13 | 题目描述: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。 14 | 例如序列 1,2,3,4,5 是某栈的压入顺序,序列 4,5,3,2,1 是该压栈序列对应的一个弹出序列,但 4,3,5,1,2 就不可能是该压栈序列的弹出序列。 15 | 16 | */ 17 | @interface IsPopOrder_31 : NSObject 18 | 19 | // pushSequence-压入顺序 popSequence-弹出序列 20 | + (bool)isPopOrder:(NSArray *)pushSequence popSequence:(NSArray *)popSequence; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/IsPopOrder_31.m: -------------------------------------------------------------------------------- 1 | // 2 | // IsPopOrder_31.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/12. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "IsPopOrder_31.h" 10 | 11 | @implementation IsPopOrder_31 12 | 13 | // pushSequence-压入顺序 popSequence-弹出序列 14 | + (bool)isPopOrder:(NSArray *)pushSequence popSequence:(NSArray *)popSequence { 15 | Stack *pushStack = [[Stack alloc] initWithNumbers:pushSequence]; 16 | int n = (int)pushStack.stackLength; 17 | 18 | Stack *popStack = [[Stack alloc] initWithNumbers:popSequence]; 19 | Stack *stack = [[Stack alloc] init]; 20 | 21 | // for (int pushIndex = 0, popIndex = 0; pushIndex < n; pushIndex++) { 22 | // stack push:pushStack 23 | // while (<#condition#>) { 24 | // <#statements#> 25 | // } 26 | // } 27 | 28 | return YES; 29 | } 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/MoreThanHalfNumSolution_39.h: -------------------------------------------------------------------------------- 1 | // 2 | // MoreThanHalfNumSolution_39.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MoreThanHalfNumSolution_39 : NSObject 12 | 13 | + (int)MoreThanHalfNum_Solution:(NSArray *)nums; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/MoreThanHalfNumSolution_39.m: -------------------------------------------------------------------------------- 1 | // 2 | // MoreThanHalfNumSolution_39.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "MoreThanHalfNumSolution_39.h" 10 | 11 | /** 12 | 题目描述:数组中出现次数超过一半的数字 13 | 解题思路:多数投票问题,可以利用 Boyer-Moore Majority Vote Algorithm 来解决这个问题,使得时间复杂度为 O(N)。 14 | 15 | 使用 cnt 来统计一个元素出现的次数,当遍历到的元素和统计元素相等时,令 cnt++,否则令 cnt--。如果前面查找了 i 个元素,且 cnt == 0,说明前 i 个元素没有 majority,或者有 majority,但是出现的次数少于 i / 2 ,因为如果多于 i / 2 的话 cnt 就一定不会为 0 。此时剩下的 n - i 个元素中,majority 的数目依然多于 (n - i) / 2,因此继续查找就能找出 majority。 16 | */ 17 | 18 | @implementation MoreThanHalfNumSolution_39 19 | 20 | + (int)MoreThanHalfNum_Solution:(NSArray *)nums { 21 | int majority = [nums vFI:0]; 22 | for (int i = 1,cnt = 1; i < nums.count; i++) { 23 | cnt = [nums vFI:i] == majority ? cnt + 1 : cnt - 1; 24 | if (cnt == 0) { 25 | majority = [nums vFI:i]; 26 | cnt = 1; 27 | } 28 | } 29 | int cnt = 0; 30 | for (NSNumber *num in nums) { 31 | if (num.intValue == majority) { 32 | cnt++; 33 | } 34 | } 35 | return cnt > nums.count / 2 ? majority : 0; 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/Permutation_38.h: -------------------------------------------------------------------------------- 1 | // 2 | // Permutation_38.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 38.字符串的排列 13 | */ 14 | @interface Permutation_38 : NSObject 15 | 16 | + (NSArray *)permutation:(NSString *)str; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/Permutation_38.m: -------------------------------------------------------------------------------- 1 | // 2 | // Permutation_38.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/27. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "Permutation_38.h" 10 | 11 | static NSArray *ret; 12 | 13 | /** 14 | 题目描述: 15 | 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串 abc,则打印出由字符 a, b, c 所能排列出来的所有字符串 abc, acb, bac, bca, cab 和 cba。 16 | */ 17 | 18 | @implementation Permutation_38 19 | 20 | + (NSArray *)permutation:(NSString *)str{ 21 | if (str.length == 0) { 22 | return ret; 23 | } 24 | NSArray *chars = [self strToArray:str]; 25 | 26 | return ret; 27 | } 28 | 29 | + (NSArray *)strToArray:(NSString *)str { 30 | NSMutableArray *strs = [NSMutableArray array]; 31 | for (int i = 0; i < str.length; i++) { 32 | [strs addObject:[str substringWithRange:NSMakeRange(i, i)]]; 33 | } 34 | return strs.copy; 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/PrintFromTopToBottom_32.h: -------------------------------------------------------------------------------- 1 | // 2 | // PrintFromTopToBottom_32.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/13. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 32 打印二叉树 13 | */ 14 | @interface PrintFromTopToBottom_32 : NSObject 15 | 16 | /// 从上往下打印出二叉树的每个节点,同层节点从左至右打印。 17 | + (void)printFromTopToBottom:(NSArray *)numbers; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/PrintFromTopToBottom_32.m: -------------------------------------------------------------------------------- 1 | // 2 | // PrintFromTopToBottom_32.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/13. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "PrintFromTopToBottom_32.h" 10 | 11 | @implementation PrintFromTopToBottom_32 12 | 13 | /** 14 | 题目描述:从上往下打印出二叉树的每个节点,同层节点从左至右打印。 15 | 解题思路:使用队列来进行层次遍历。 16 | 不需要使用两个队列分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。 17 | */ 18 | + (void)printFromTopToBottom:(NSArray *)numbers { 19 | 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/RandomListNode_35.h: -------------------------------------------------------------------------------- 1 | // 2 | // RandomListNode_35.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/15. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 35.复杂链表的复制 13 | */ 14 | @interface RandomListNode_35 : NSObject 15 | 16 | + (ListNode *)randomListNodeClone:(NSArray *)numbers; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/RandomListNode_35.m: -------------------------------------------------------------------------------- 1 | // 2 | // RandomListNode_35.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/15. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "RandomListNode_35.h" 10 | 11 | /** 12 | 题目描述:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的 head。 13 | 第一步:在每个节点的后面插入复制的节点。 14 | 第二步,对复制节点的 random 链接进行赋值。 15 | 第三步,拆分。 16 | */ 17 | @implementation RandomListNode_35 18 | 19 | + (ListNode *)randomListNodeClone:(NSArray *)numbers { 20 | // 构建一个二叉树 21 | return nil; 22 | } 23 | 24 | - (ListNode *)createBinaryTree:(NSArray *)numbers { 25 | return nil; 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/Serializer_37.h: -------------------------------------------------------------------------------- 1 | // 2 | // Serializer_37.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/20. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 37 题目描述:请实现两个函数,分别用来序列化和反序列化二叉树。 13 | */ 14 | @interface Serializer_37 : NSObject 15 | 16 | // 序列化 17 | + (NSString *)serialize; 18 | 19 | // 发序列化 20 | + (TreeNode *)deserialize:(NSString *)str; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/Serializer_37.m: -------------------------------------------------------------------------------- 1 | // 2 | // Serializer_37.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/20. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "Serializer_37.h" 10 | 11 | static NSString *deserializeStr = nil; 12 | 13 | @implementation Serializer_37 14 | 15 | + (NSString *)serialize { 16 | // 先构建一颗二叉树 17 | TreeNode *root = [TreeNode treeNodeWithValue:1]; 18 | TreeNode *left = [TreeNode treeNodeWithValue:2]; 19 | TreeNode *right = [TreeNode treeNodeWithValue:3]; 20 | root.leftNode = left; 21 | root.rightNode = right; 22 | 23 | return [self serialize:root]; 24 | } 25 | 26 | + (NSString *)serialize:(TreeNode *)root { 27 | if (root == nil) { 28 | return @"#"; 29 | } 30 | return [NSString stringWithFormat:@"%ld %@ %@",root.value, [self serialize:root.leftNode], [self serialize:root.rightNode]]; 31 | } 32 | 33 | // 反序列化二叉树 34 | + (TreeNode *)deserialize:(NSString *)str { 35 | deserializeStr = str; 36 | return [self deserialize]; 37 | } 38 | 39 | + (TreeNode *)deserialize { 40 | if (deserializeStr.length == 0) { 41 | return nil; 42 | } 43 | NSRange range = [deserializeStr rangeOfString:@" "]; 44 | NSString *node = range.location == -1 ? deserializeStr : [deserializeStr substringWithRange:range]; 45 | deserializeStr = range.location == -1 ? @"" : [deserializeStr substringFromIndex:range.location + 1]; 46 | 47 | if ([node isEqualToString:@"#"]) { 48 | return nil; 49 | } 50 | NSUInteger value = [node integerValue]; 51 | TreeNode *treeNode = [TreeNode treeNodeWithValue:value]; 52 | treeNode.leftNode = [self deserialize]; 53 | treeNode.rightNode = [self deserialize]; 54 | 55 | return treeNode; 56 | } 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/VerifySquenceOfBST_33.h: -------------------------------------------------------------------------------- 1 | // 2 | // VerifySquenceOfBST_33.h 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/13. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。假设输入的数组的任意两个数字都互不相同。 13 | */ 14 | @interface VerifySquenceOfBST_33 : NSObject 15 | 16 | + (bool)verifySquenceOfBST:(NSArray *)sequence; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /OC_算法_1/OC_算法/算法/31-40/VerifySquenceOfBST_33.m: -------------------------------------------------------------------------------- 1 | // 2 | // VerifySquenceOfBST_33.m 3 | // OC_算法 4 | // 5 | // Created by cs on 2018/11/13. 6 | // Copyright © 2018 cs. All rights reserved. 7 | // 8 | 9 | #import "VerifySquenceOfBST_33.h" 10 | 11 | @implementation VerifySquenceOfBST_33 12 | 13 | + (bool)verifySquenceOfBST:(NSArray *)sequence { 14 | if (sequence == nil || sequence.count == 0) { 15 | return NO; 16 | } 17 | return [self verify:sequence first:0 last:sequence.count - 1]; 18 | } 19 | 20 | + (bool)verify:(NSArray *)sequence first:(NSUInteger)first last:(NSUInteger)last { 21 | if (last - first <= 1) { 22 | return YES; 23 | } 24 | int rootValue = [sequence vFI:last]; 25 | int cutIndex = (int)first; 26 | while (cutIndex < last && [sequence vFI:cutIndex] <= rootValue) { 27 | cutIndex++; 28 | } 29 | for (int i = cutIndex; i < last; i++) { 30 | if ([sequence vFI:i] < rootValue) { 31 | return NO; 32 | } 33 | } 34 | return [self verify:sequence first:first last:cutIndex - 1] && [self verify:sequence first:cutIndex last:last - 1]; 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArithmeticDemo 2 | 备注:该项目主要是在 [CS-Notes](https://github.com/CyC2018/CS-Notes/blob/master/notes/%E5%89%91%E6%8C%87%20offer%20%E9%A2%98%E8%A7%A3.md#1-%E5%89%8D%E8%A8%80) 的基础上编写的。只是本人用 OC 语法实现而已,非常感谢该作者,如有侵权,请告知。 3 | 4 | 算法目录 5 | 1. 前言 6 | 2. 实现 Singleton 7 | 3. 数组中重复的数字 8 | 4. 二维数组中的查找 9 | 5. 替换空格 10 | 6. 从尾到头打印链表 11 | 7. 重建二叉树 12 | 8. 二叉树的下一个结点 13 | 9. 用两个栈实现队列 14 | 10.1 斐波那契数列 15 | 10.2 跳台阶 16 | 10.3 矩形覆盖 17 | 10.4 变态跳台阶 18 | 11. 旋转数组的最小数字 19 | 12. 矩阵中的路径 20 | 13. 机器人的运动范围 21 | 14. 剪绳子 22 | 15. 二进制中 1 的个数 23 | 16. 数值的整数次方 24 | 17. 打印从 1 到最大的 n 位数 25 | 18.1 在 O(1) 时间内删除链表节点 26 | 18.2 删除链表中重复的结点 27 | 19. 正则表达式匹配 28 | 20. 表示数值的字符串 29 | 21. 调整数组顺序使奇数位于偶数前面 30 | 22. 链表中倒数第 K 个结点 31 | 23. 链表中环的入口结点 32 | 24. 反转链表 33 | 25. 合并两个排序的链表 34 | 26. 树的子结构 35 | 27. 二叉树的镜像 36 | 28 对称的二叉树 37 | 29. 顺时针打印矩阵 38 | 30. 包含 min 函数的栈 39 | 31. 栈的压入、弹出序列 40 | 32.1 从上往下打印二叉树 41 | 32.2 把二叉树打印成多行 42 | 32.3 按之字形顺序打印二叉树 43 | 33. 二叉搜索树的后序遍历序列 44 | 34. 二叉树中和为某一值的路径 45 | 35. 复杂链表的复制 46 | 36. 二叉搜索树与双向链表 47 | 37. 序列化二叉树 48 | 38. 字符串的排列 49 | 39. 数组中出现次数超过一半的数字 50 | 40. 最小的 K 个数 51 | 41.1 数据流中的中位数 52 | 41.2 字符流中第一个不重复的字符 53 | 42. 连续子数组的最大和 54 | 43. 从 1 到 n 整数中 1 出现的次数 55 | 44. 数字序列中的某一位数字 56 | 45. 把数组排成最小的数 57 | 46. 把数字翻译成字符串 58 | 47. 礼物的最大价值 59 | 48. 最长不含重复字符的子字符串 60 | 49. 丑数 61 | 50. 第一个只出现一次的字符位置 62 | 51. 数组中的逆序对 63 | 52. 两个链表的第一个公共结点 64 | 53. 数字在排序数组中出现的次数 65 | 54. 二叉查找树的第 K 个结点 66 | 55.1 二叉树的深度 67 | 55.2 平衡二叉树 68 | 56. 数组中只出现一次的数字 69 | 57.1 和为 S 的两个数字 70 | 57.2 和为 S 的连续正数序列 71 | 58.1 翻转单词顺序列 72 | 58.2 左旋转字符串 73 | 59. 滑动窗口的最大值 74 | 60. n 个骰子的点数 75 | 61. 扑克牌顺子 76 | 62. 圆圈中最后剩下的数 77 | 63. 股票的最大利润 78 | 64. 求 1+2+3+...+n 79 | 65. 不用加减乘除做加法 80 | 66. 构建乘积数组 81 | 67. 把字符串转换成整数 82 | 68. 树中两个节点的最低公共祖先 83 | --------------------------------------------------------------------------------