• 删除重复字符


    需求:

    1、为可变字符串类添加一个检测是不是有重复字母的方法:例如:@“abccd”调用这个函数后变成@”abcd”;

    2、给学员定义一个“私有”方法:判断学员是不是有时间。定义一个协议:服务员和学员的协议,服务员给学员送餐。如果学员忙,就让服务员送餐。


    1:

    NSMutableString+RemoveRep.h

    #import <Foundation/Foundation.h>
    
    @interface NSMutableString (RemoveRep)
    -(void)removeRepeat:(NSString *)aNum;
    @end

    NSMutableString+RemoveRep.m

    #import "NSMutableString+RemoveRep.h"
    
    @implementation NSMutableString (RemoveRep)
    -(void)removeRepeat:(NSString *)aNum
    {
        NSMutableArray *mArr = [[NSMutableArray alloc]initWithCapacity:10];
        for (int i = 0;i < aNum.length;i++)
        {
            [mArr addObject:[aNum substringWithRange:NSMakeRange(i,1)]];
        }
        [self compareNum:mArr];
        NSLog(@"%@",mArr);
        
    }
    //比较是否相等
    -(NSMutableArray *)compareNum:(NSMutableArray *)mArr//abcbcbcbda
    {
        int count = mArr.count;//重新定义了,count不会减一
    /*错误,因为冒泡排序只比较两个相邻的数
        for (int j = 0; j < count - 1; j++)
        {
            for (int i = 0; i < count - 1 - j; i++)
            {
                NSString *a = [mArr objectAtIndex:i];
                NSString *b = [mArr objectAtIndex:i+1];
                
                if ([a isEqualToString:b])
                {
                    NSLog(@"repeat = %@",[mArr objectAtIndex:i]);
                   [mArr removeObjectAtIndex:i];
                    count--;//数组长度发生变化
                }
            }
        }
    */
        for (int j = 0; j < count - 1; j++)//百钱买百鸡式的冒泡遍历
        {
            for (int i = j; i < count - 1; i++)
            {
                NSString *a = [mArr objectAtIndex:j];
                NSString *b = [mArr objectAtIndex:i+1];
                if ([a isEqualToString:b])
                {
                    NSLog(@"repeat = %@",[mArr objectAtIndex:i+1]);
                    [mArr removeObjectAtIndex:i+1];
                    count--;//数组长度发生变化
                    i--;//没有这步.如果abcccd的情况就会跳过一个c
                }
            }
        }
    
        return mArr;
    }
    
    @end

    2:

    Student.h

    #import <Foundation/Foundation.h>
    @protocol OrderDelegate;
    
    @interface Student : NSObject
    
    @property(nonatomic,assign)id<OrderDelegate> delegate;
    
    -(void)orderMeal;
    @end
    
    
    
    
    @protocol OrderDelegate <NSObject>
    @required
    -(void)order:(NSString *)aName
       withMoney:(int)aMoney;
    @end

    Student.m

    #import "Student.h"
    
    @implementation Student
    @synthesize delegate = _delegate;
    
    -(void)orderMeal
    {
        if ([self.delegate conformsToProtocol:@protocol(OrderDelegate)])
        {
            [self.delegate order:@"腊味" withMoney:10];
        }
    }
    
    
    @end

     Waiter.h

    #import <Foundation/Foundation.h>
    
    #import "Student.h"
    @interface Waiter : NSObject <OrderDelegate>
    
    @end

    Waiter.m

    #import "Waiter.h"
    
    
    @implementation Waiter
    
    -(void)order:(NSString *)aName withMoney:(int)aMoney
    {
        NSLog(@"买到了%d元的%@",aMoney,aName);
    }
    
    @end

    AppDelegate.m

    Waiter *w = [[Waiter alloc]init];
        
        Student *s = [[Student alloc]init];
        
        s.delegate = w;
        [s orderMeal];
  • 相关阅读:
    织梦插件开发
    yiic使用笔记
    yii2.0学习及变化比较(一)
    yii框架设计学习笔记(一)
    Maven生成可以直接运行的jar包的多种方式(转)
    Linux下查看CPU型号,内存大小,硬盘空间的命令(详解)
    从Google Earth 中下载三维模型
    Hadoop安装所遇问题及解决方法
    智慧家居体系结构
    .Net 数据库(SqlServer2008)的备份、还原
  • 原文地址:https://www.cnblogs.com/huen/p/3544975.html
Copyright © 2020-2023  润新知