ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

面试题(算法列举)

2021-05-15 21:03:02  阅读:164  来源: 互联网

标签:面试题 struct temp ++ 结点 算法 result 数组 列举


面试题—算法相关

  • 字符串反转
  • 链表反转
  • 有序数组合并
  • Hash算法
  • 查找两个子视图的共同父视图
  • 求无序数组当中的中位数

一、字符串反转
给定字符串 “hello,world”,实现将其反转。输出结果:dlrow,olleh

- (void)charReverse
{
    NSString * string = @"hello,world";
    
    NSLog(@"%@",string);
    
    NSMutableString * reverString = [NSMutableString stringWithString:string];
    
    for (NSInteger i = 0; i < (string.length + 1)/2; i++) {
        
        [reverString replaceCharactersInRange:NSMakeRange(i, 1) withString:[string substringWithRange:NSMakeRange(string.length - i - 1, 1)]];
        
        [reverString replaceCharactersInRange:NSMakeRange(string.length - i - 1, 1) withString:[string substringWithRange:NSMakeRange(i, 1)]];
    }
    
    NSLog(@"reverString:%@",reverString);
    
    //C
    char ch[100];
    
    memcpy(ch, [string cStringUsingEncoding:NSUTF8StringEncoding], [string length]);

   //设置两个指针,一个指向字符串开头,一个指向字符串末尾
    char * begin = ch;
    
    char * end = ch + strlen(ch) - 1;
   
//遍历字符数组,逐步交换两个指针所指向的内容,同时移动指针到对应的下个位置,直至begin>=end 
    while (begin < end) {
        
        char temp = *begin;
        
        *(begin++) = *end;
        
        *(end--) = temp;
    }
    
    NSLog(@"reverseChar[]:%s",ch);
}

二、链表反转
反转前:1->2->3->4->NULL
反转后:4->3->2->1->NULL

/**  定义一个链表  */
struct Node {
    
    NSInteger data;
    
    struct Node * next;
};

- (void)listReverse
{
    struct Node * p = [self constructList];
    
    [self printList:p];
    
    //反转后的链表头部
    struct Node * newH = NULL;
    //头插法
    while (p != NULL) {
        
        //记录下一个结点
        struct Node * temp = p->next;
        //当前结点的next指向新链表的头部
        p->next = newH;
        //更改新链表头部为当前结点
        newH = p;
        //移动p到下一个结点
        p = temp;
    }
    
    [self printList:newH];
}
/**
 打印链表

 @param head 给定链表
 */
- (void)printList:(struct Node *)head
{
    struct Node * temp = head;
    
    printf("list is : ");
    
    while (temp != NULL) {
        
        printf("%zd ",temp->data);
        
        temp = temp->next;
    }
    
    printf("\n");
}


/**  构造链表  */
- (struct Node *)constructList
{
    //头结点
    struct Node *head = NULL;
    //尾结点
    struct Node *cur = NULL;
    
    for (NSInteger i = 0; i < 10; i++) {
        
        struct Node *node = malloc(sizeof(struct Node));
        
        node->data = i;
        
        //头结点为空,新结点即为头结点
        if (head == NULL) {
            
            head = node;
            
        }else{
            //当前结点的next为尾结点
            cur->next = node;
        }
        
        //设置当前结点为新结点
        cur = node;
    }
    
    return head;
}

三、有序数组合并
将有序数组 {1,4,6,7,9} 和 {2,3,5,6,8,9,10,11,12} 合并为
{1,2,3,4,5,6,6,7,8,9,9,10,11,12}

 - (void)orderListMerge
{
    int aLen = 5,bLen = 9;
    
    int a[] = {1,4,6,7,9};
    
    int b[] = {2,3,5,6,8,9,10,11,12};
    
    [self printList:a length:aLen];
    
    [self printList:b length:bLen];
    
    int result[14];
    
    int p = 0,q = 0,i = 0;//p和q分别为a和b的下标,i为合并结果数组的下标
    
    //任一数组没有达到s边界则进行遍历
    while (p < aLen && q < bLen) {
        
        //如果a数组对应位置的值小于b数组对应位置的值,则存储a数组的值,并移动a数组的下标与合并结果数组的下标
        if (a[p] < b[q]) result[i++] = a[p++];
        
        //否则存储b数组的值,并移动b数组的下标与合并结果数组的下标
        else result[i++] = b[q++];
    }
    
    //如果a数组有剩余,将a数组剩余部分拼接到合并结果数组的后面
    while (++p < aLen) {
        
        result[i++] = a[p];
    }
    
    //如果b数组有剩余,将b数组剩余部分拼接到合并结果数组的后面
    while (q < bLen) {
        
        result[i++] = b[q++];
    }
    
    [self printList:result length:aLen + bLen];
}
 - (void)printList:(int [])list length:(int)length
{
    for (int i = 0; i < length; i++) {
        
        printf("%d ",list[i]);
    }
    
    printf("\n");
}

四、HASH算法

  • 哈希表
    例:给定值是字母a,对应ASCII码值是97,数组索引下标为97。
    这里的ASCII码,就算是一种哈希函数,存储和查找都通过该函数,有效地提高查找效率。
  • 在一个字符串中找到第一个只出现一次的字符。如输入"abaccdeff",输出’b’
    字符(char)是一个长度为8的数据类型,因此总共有256种可能。每个字母根据其ASCII码值作为数组下标对应数组种的一个数字。数组中存储的是每个字符出现的次数。
- (void)hashTest
{
    NSString * testString = @"hhaabccdeef";
    
    char testCh[100];
    
    memcpy(testCh, [testString cStringUsingEncoding:NSUTF8StringEncoding], [testString length]);
    
    int list[256];
    
    for (int i = 0; i < 256; i++) {
        
        list[i] = 0;
    }
    
    char *p = testCh;
    
    char result = '\0';
    
    while (*p != result) {
        
        list[*(p++)]++;
    }
    
    p = testCh;
    
    while (*p != result) {
        
        if (list[*p] == 1) {

            result = *p;
            
            break;
        }
        
        p++;
    }
    
    printf("result:%c",result);
}

五、查找两个子视图的共同父视图
思路:分别记录两个子视图的所有父视图并保存到数组中,然后倒序寻找,直至找到第一个不一样的父视图。

 - (void)findCommonSuperViews:(UIView *)view1 view2:(UIView *)view2
{
    NSArray * superViews1 = [self findSuperViews:view1];
    
    NSArray * superViews2 = [self findSuperViews:view2];
    
    NSMutableArray * resultArray = [NSMutableArray array];
    
    int i = 0;
    
    while (i < MIN(superViews1.count, superViews2.count)) {
        
        UIView *super1 = superViews1[superViews1.count - i - 1];
        
        UIView *super2 = superViews2[superViews2.count - i - 1];
        
        if (super1 == super2) {
            
            [resultArray addObject:super1];
            
            i++;
            
        }else{
            
            break;
        }
    }
    
    NSLog(@"resultArray:%@",resultArray);
    
}
 - (NSArray <UIView *>*)findSuperViews:(UIView *)view
{
    UIView * temp = view.superview;
    
    NSMutableArray * result = [NSMutableArray array];
    
    while (temp) {
        
        [result addObject:temp];
        
        temp = temp.superview;
    }
    
    return result;
}

六、求无序数组中的中位数
中位数:当数组个数n为奇数时,为(n + 1)/2,即是最中间那个数字;当n为偶数时,为(n/2 + (n/2 + 1))/2,即是中间两个数字的平均数。
首先要先去了解一些几种排序算法:iOS排序算法
思路:

  • 排序算法+中位数 首先用冒泡排序、快速排序、堆排序、希尔排序等排序算法将所给数组排序,然后取出其中位数即可。
  • 利用快排思想

作为一个开发者,有一个学习的氛围跟一个交流圈子特别重要,这是一个我的iOS交流群:642363427不管你是小白还是大牛欢迎入驻
,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!

标签:面试题,struct,temp,++,结点,算法,result,数组,列举
来源: https://blog.51cto.com/u_15010671/2778149

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有