iOS算法

  • 字符串反转

     //调用
     char cha[] = "hello,word";
     char_reverse(cha);
     printf("revers result is %s",cha);
    
     //实现
     void char_reverse(char * cha){
         char * begin = cha;
         char * end = cha + strlen(cha)-1;
         while (begin<end) {
             char temp = *begin;
             *(begin++) = *end;
             *(end--) = temp;
         }
     }
    
  • 链表反转

     //调用
     struct Node * head = constructList();
     printList(head);
     struct Node * newH = reversList(head);
     printList(newH);
    
     //.h文件
     struct Node {
         int data;
         struct Node * next;
     };
     @interface ReversList : NSObject
     //链表反转
     struct Node * reversList(struct  Node * head);
     //构建链表
     struct Node * constructList(void);
     //打印链表中的数据
     void printList(struct Node * head);
     @end
    
     //.m文件
     #import "ReversList.h"
     @implementation ReversList
    
     //链表反转
     struct Node * reversList(struct  Node * head){
         //定义遍历指针,初始化为头节点
         struct Node * p = head;
         //反转后的链表头部
         struct Node * newHead = NULL;
         //遍历链表
         while (p != NULL) {
             // 记录下一个节点
             struct Node * temp = p->next;
             //当前节点指的next指向新的链表头部
             p->next = newHead;
             //更改新链表头部为当前节点
             newHead = p;
             //移动p指针
             p = temp;
         }
         
         //返回反转后的头节点
         return newHead;
     }
     //构建链表
     struct Node * constructList(void){
         //头节点定义
         struct Node * head = NULL;
         //记录当前尾节点
         struct Node * cur = NULL;
         
         for (int i=1; i<5; 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;
         
     }
     //打印链表中的数据
     void printList(struct Node * head){
         struct Node * tmp = head;
         while (tmp != NULL) {
             printf("node id %d \n",tmp->data);
             tmp = tmp->next;
         }
     }
     @end
    
  • 有序数组合并

          //有序数组合并  归并算法 调用
          int a[5] = {1,4,6,7,9};
          int b[8] = {2,3,5,6,8,10,11,12};
          int result[13];
          mergeList(a, 5, b, 8, result);
          for (int i=0; i<13; i++) {
              printf("%d,",result[I]);
          }
      //有序数组合并  归并算法 实现
      void mergeList(int a[],int aLen,int b[],int bLen,int result[]){
          int p = 0;
          int q = 0;
          int i = 0;
          while (p<aLen && q<bLen) {
              if (a[p]<=b[q]) {
                  result[i] = a[p++];
              }else{
                  result[i] = b[q++];
              }
              I++;
          }
          while (p<aLen) {
              result[i] = a[p++];
              I++;
          }
          while (q<bLen) {
              result[i] = b[q++];
              I++;
          }
      }
    
  • hash 哈希算法

         char cha[] = "abaccdef";
         char result = findFirstChar(cha);
         printf("this is %c",result);
    
     //hash 哈希算法 找出第一个只出现一次的字母
     char findFirstChar(char * cha){
         char result = '\0';
         int array[256];
         for (int i=0; i<256; i++) {
             array[i] = 0;
         }
         char *p = cha;
         while (*p != '\0') {
             array[*(p++)]++;
         }
         p = cha;
         while (*p != '\0') {
             if (array[*p]==1) {
                 result = *p;
                 break;
             }
             p++;
         }
         return result;
     }
    
  • 查找无需数组的中位数
    1.排序算法+中位数
    当数组个数n为基数时:中位数=array[(n/2)]
    当数组个数n为偶数时:中位数=(array[(n/2-1)]+array[(n/2)])/2
    2.利用快排思想

image.png
      //利用快排思想求中位数
      |2|4|6|8|
      //调用
        int list[9] = {12,3,10,8,6,7,11,13,9};
        int median = medianFind(list, 9);
        printf("the median is %d",median);
        
      //求一个无需数组的我中位数
    int medianFind(int a[],int len)
    {
        int low = 0;
        int high = len-1;
        
        int mid = (len-1)/2;
        int div = PartSort(a,low,high);
        
        while (div != mid) {
            if (mid<div) {
                //左半区查找
                div = PartSort(a,low,div-1);
            }else{
                //右半区查找
                div = PartSort(a,div+1,high);
            }
        }
        //找到了
        return a[mid];
    }

    //
    int PartSort(int a[],int start,int end){
        int low = start;
        int high = end;
        
        int key = a[end];
        while (low<high) {
            //左边找比key大的值
            while (low<high&& a[low]<=key) {
                ++low;
            }
            //右边找比key小的值
            while (low<high&& a[end]>=key) {
                --high;
            }
            //找到之后交换左右的值
            if (low<high) {
                int tmp = a[low];
                a[low] = a[high];
                a[high] = tmp;
            }
        }
        
        int tmp = a[high];
        a[high] = a[end];
        a[end] = tmp;
        
        return low;
    }
  • 查找两个子视图共同所有的父视图

      -(NSArray<UIView *> *)findCommonSuperView:(UIView *)view1 :(UIView *)view2{
          NSArray * arr1 = [self findSuperView:view1];
          NSArray * arr2 = [self findSuperView:view2];
          
          NSMutableArray * results = [NSMutableArray array];
          
          int I=0;
          
          while (i<MIN(arr1.count, arr2.count)) {
              if (arr1[arr1.count-i-1] == arr2[arr2.count-i-1]) {
                  [results addObject:arr1[arr1.count-i-1]];
                  I++;
              }else{
                  break;
              }
          }
          return results;
          
      }
      -(NSArray<UIView *> *)findSuperView:(UIView *)view{
          NSMutableArray * results = [NSMutableArray array];
          UIView * v1 = view.superview;
          while (v1) {
              [results addObject:v1];
              v1 = v1.superview;
          }
          return results;
      }
    
  • m*n的矩阵点(m>0,n>0),m为x坐标,n为y坐标(x轴向右,y轴向下),从(0,0)位置,初始方向向右移动,移动到边或者下一节点已经走过时,需要改变方向,改变的顺序为顺时针。
    求最后走到的点坐标。

    #import "matrixObject.h"
     @interface matrixObject()
     @property (nonatomic,strong)NSMutableArray * poinaArray;
     @end
     @implementation matrixObject
     - (instancetype)init
     {
         self = [super init];
         if (self) {
             _poinaArray =  [NSMutableArray array];
             [self testSatrtArray:@[@0,@0] endArray:@[@3,@3]];//调用
         }
         return self;
     }
    
     #pragma mark -- 递归
     -(void)testSatrtArray:(NSArray *)startArray endArray:(NSArray *)pointArray{
         //起点(startX,startY) 矩阵点(m,n) 移动的点(x,y)
         int x= [startArray[0] intValue];
         int startX= [startArray[0] intValue];
         
         int y = [startArray[1] intValue];
         int startY = [startArray[1] intValue];
         
         int m = [pointArray[0] intValue];
         int n = [pointArray[1] intValue];
    
         while (x<m && y<n) {
             NSString * pointStr = [NSString stringWithFormat:@"%d,%d",x,y];
    
             if (![_poinaArray containsObject: pointStr]) {
                 if (x<m && y<n) {
                     //向右移动 x++
                     while (x<m) {
                         [_poinaArray addObject:[NSString stringWithFormat:@"%d,%d",x,y]];
                         x++;
                     }
                 }
                 if (x==m && y<n) {
                     //向下移动 y++
                     while (y<n) {
                         [_poinaArray addObject:[NSString stringWithFormat:@"%d,%d",x,y]];
                         y++;
                     }
                 }
                 if (x==m && y==n) {
                     //向左移动 x--
                     while (x>startX) {
                         [_poinaArray addObject:[NSString stringWithFormat:@"%d,%d",x,y]];
                         x--;
                     }
    
                 }
                 if (x==startX && y==n) {
                     //向上移动 y--
                     while (y>startY) {
                         [_poinaArray addObject:[NSString stringWithFormat:@"%d,%d",x,y]];
                         y--;
                     }
                 }
             }else{
                 ++x;
                 ++y;
                 --m;
                 --n;
                 if (x<m && y<n) {
                     [self testSatrtArray:@[@(x),@(y)] endArray:@[@(m),@(n)]];
                 }else{
                     if (x==m && y==n) {//当起点和矩阵点相同时只记录原点
                         [_poinaArray addObject:[NSString stringWithFormat:@"%d,%d",x,y]];
                     }
                     NSLog(@"最后走到的点坐标:%@",[_poinaArray lastObject]);//(1,2)
                     NSLog(@"x:%d y:%d   m:%d n:%d  \n _poinaArray:(%@)",x,y,m,n,[_poinaArray componentsJoinedByString:@")("]);
                     //x:2 y:2   m:1 n:1
                     //_poinaArray:(0,0)(1,0)(2,0)(3,0)(3,1)(3,2)(3,3)(2,3)(1,3)(0,3)(0,2)(0,1)(1,1)(2,1)(2,2)(1,2)
                     break;
                 }
             }
         };
     }
    
     @end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341