【iOS面試機試題】實現(xiàn)一個功能函數(shù)

函數(shù)功能如下:
對于輸入的字符串s,忽略大小寫,返回按照英文字母出現(xiàn)的頻率從高到低排序的小寫字符串,對于出現(xiàn)次數(shù)相同的字母,按照字典序排序。如果輸入的字符串不包含英文字母,返回空字符串。注意數(shù)字,標點,空格都不是英文字母。
例如:輸入字符:We Attack at Dawn ,輸出:atwcdekn
解釋:在這個例子中不區(qū)分大小寫,字母 a 出現(xiàn)了3次,字母 t 出現(xiàn)了3次,字母 w 出現(xiàn)2次,字母 c,d,e,k,n 均出現(xiàn)一次,對于出現(xiàn)次數(shù)相同的字母按照字典書序排序,例如按照字典序字母 a 排在字母 t 之前,所以輸出結(jié)果為:atwcdekn

代碼實現(xiàn)

void PrintOut(NSString *text){
    NSString *result = @"";
    NSMutableArray *array1 = [NSMutableArray array];
    //取出小寫字母放入數(shù)組中
    text = text.lowercaseString;
    for(int i =0; i < [text length]; i++){
        char character = [text characterAtIndex:i];
        if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:character]) {
            [array1 addObject:[NSString stringWithFormat:@"%c",character]];
        }
        
    }
    
    // 相同的字符放入同一個數(shù)組中變?yōu)槎S數(shù)組
    NSMutableArray *dateMutablearray = [@[] mutableCopy];
    NSMutableArray *array = [NSMutableArray arrayWithArray:array1];
    for (int i = 0; i < array.count; i ++) {
        NSString *string = array[i];
        NSMutableArray *tempArray = [@[] mutableCopy];
        [tempArray addObject:string];
        for (int j = i+1; j < array.count; j ++) {
            NSString *jstring = array[j];
            if([string isEqualToString:jstring]){
                [tempArray addObject:jstring];
                [array removeObjectAtIndex:j];
            }
        }
        [dateMutablearray addObject:tempArray];
    }
//    NSLog(@"dateMutable:%@",dateMutablearray);
    
//    按照二維數(shù)組元素個數(shù)排序
    for (int i = 0; i < dateMutablearray.count; i++) {
        for (int j = i+1; j < dateMutablearray.count; j++) {
            NSInteger inum = ((NSArray*)dateMutablearray[i]).count;
            NSInteger jnum = ((NSArray*)dateMutablearray[j]).count;
            if (inum < jnum) {
                [dateMutablearray exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    NSLog(@"dateMutable:%@",dateMutablearray);
    
    BOOL continueNext = YES;
    //把二維數(shù)組中數(shù)組個數(shù)相同的放入一個數(shù)組中
    NSMutableArray *numberarray = [@[] mutableCopy];
    for (int i = 0; i < dateMutablearray.count; i ++) {
        NSArray *arr = dateMutablearray[i];
        NSInteger num = arr.count;
        NSMutableArray *tempArray = [@[] mutableCopy];
        NSString *istr = arr[0];
        [tempArray addObject:istr];
        if (num == 1) {
            if (continueNext) {
                for (int j = i+1; j < dateMutablearray.count; j ++) {
                    NSArray *jarr = dateMutablearray[j];
//                    NSInteger jnum = jarr.count;
                    NSString *jstr = jarr[0];
                    [tempArray addObject:jstr];
                    
                }
                [numberarray addObject:tempArray];
            }
            
            continueNext = NO;
        }else{
            for (int j = i+1; j < dateMutablearray.count; j ++) {
                NSArray *jarr = dateMutablearray[j];
                NSInteger jnum = jarr.count;
                NSString *jstr = jarr[0];
                if(num == jnum){
                    [tempArray addObject:jstr];
                    [dateMutablearray removeObjectAtIndex:j];
                }
            }
            [numberarray addObject:tempArray];
        }
    
    }
    NSLog(@"numberarray:%@",numberarray);
    
//    二維數(shù)組中每個數(shù)組的元素排序
    NSMutableArray *sortarray = [@[] mutableCopy];
    for (int i = 0; i < numberarray.count; i++) {
        NSArray *arr = numberarray[i];
        NSArray *newArray = [arr sortedArrayUsingSelector:@selector(compare:)];
        [sortarray addObject:newArray];
    }
    NSLog(@"sortarray:%@",sortarray);
    
//    拼接所有數(shù)組所有元素
    for (int i = 0; i < sortarray.count; i++) {
        NSArray *arr = sortarray[i];
        for (int j = 0; j < arr.count; j++) {
            result = [result stringByAppendingString:arr[j]];
            
        }
    }
    
    NSLog(@"result:%@",result);
    
}

以上代碼實現(xiàn)可能思路比較按部就班,歡迎大家提供更好的方法交流

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容