2020-10-28

快排

/// 当重复的个数比较多的时候我们就需要这样了  我这个下面是二路快排
+ (void)fastSortWithArr:(NSMutableArray *)arr left:(NSInteger)l right:(NSInteger)r {
    
    ///< 递归结束条件
    if (l == r || l > r) return;
    
    NSInteger left = l;
    NSInteger right = r;
    NSInteger key = [arr[l] integerValue];
    
    while (left < right) {
        ///< 排右面  小的往前面移动  因为要和交换位交换
        while ([arr[right] integerValue] >= key && left < right) {
            right--;
        }
        arr[left] = arr[right];
        
        
        ///< 排左面  大的往后面移动
        while ([arr[left] integerValue] <= key && left < right) {
            left++;
        }
        arr[right] = arr[left];
    }
    
    ///< 交换位归位
    arr[left] = @(key);
    
    NSLog(@"main  left:%zd   right:%zd %@",l,r,arr);

    
    ///
    [self fastSortWithArr:arr left:l right:left - 1];
    
    [self fastSortWithArr:arr left:left + 1 right:r];
}

链表反转

LinkList* reverseList(LinkList*head) {
    LinkList *node0 = head->next;
    LinkList *temp = node0;
    if (head == NULL || node0 == NULL) {
        return head;
    }
    
    while (node0->next) {
        LinkList *node1 = node0->next;
        LinkList *node2 = node1->next;
        
        ///< 反转
        head->next = node1;
        node1->next = temp;
        
        ///< 指向下一个
        node0->next = node2;
        ///< 保存now
        temp = node1;
    }
    
    return head;
}

链表反转

TwoWayLinkedList *reverseTwoWayLinkedList(TwoWayLinkedList*head) {
    
    if (head == NULL || head->next == NULL) {
        return head;
    }
    
    TwoWayLinkedList *temp;
    TwoWayLinkedList *current = head->next;
    while (current != NULL) {
        temp = current->pre;
        current->pre = current->next;
        current->next = temp;
        if (current->pre == NULL) {
            break;
        } else {
            current = current->pre;
        }
    }
    head->next->next = NULL;
    head->next = current;
    return head;
}

二叉树非递归实现 按层排序

void LevelTraversal(BiTree *T,int size) {
    if (T) {
        int index = 0;
        
        ///< 指的是节点数
        int i = 0;
        
        BiTree *queue[size];
        queue[index] = T;
        
        while (i <= index) {
            if (queue[i]->lBi) {
                index++;
                queue[index] = queue[i]->lBi;
            }
            if (queue[i]->rBi) {
                index++;
                queue[index] = queue[i]->rBi;
            }
            i++;
        }
        
        for (NSInteger i = 0; i < size; i++) {
            printf(" %c ",queue[i]->c);
        }
    }
}

二叉树深度

    static int treeDepth(TreeLinkNode root) {
        if (root != null) {
            int left = treeDepth(root.left);
            int right = treeDepth(root.right);
            return left > right ? left + 1 : right + 1;
        }
        return 0;
    }

合并有序数组

+ (NSMutableArray *)mergeArr1:(NSMutableArray *)arr1 arr2:(NSMutableArray *)arr2 {
    NSMutableArray *mergeArr = [NSMutableArray arrayWithCapacity:arr1.count + arr2.count];
    
    NSInteger a = 0;
    NSInteger b = 0;
    NSInteger i = 0;
    
    while (a < arr1.count && b < arr2.count) {
        if (arr1[a] < arr2[b]) {
            mergeArr[i] = arr1[a];
            a++;
        } else {
            mergeArr[i] = arr2[b];
            b++;
        }
        i++;
    }
    
    /// 添加左面剩余
    for (NSInteger i = a; i < arr1.count; i++) {
        mergeArr[i] = arr1[a];
        i++;
    }
    
    /// 添加右面剩余
    for (NSInteger i = b; i < arr2.count; i++) {
        mergeArr[i] = arr2[b];
        i++;
    }
    
    return mergeArr;
}

二分查找 有序数组 查找

+ (NSInteger)isContainsNumber:(NSInteger)number arr:(NSMutableArray *)arr {
    NSInteger left = 0;
    NSInteger right = arr.count - 1;
    while (left <= right) {
        NSInteger mid = (left + right) / 2;
        if ([arr[mid] integerValue] == number) {
            return mid;
        } else if ([arr[mid] integerValue] < number) {
            left = mid + 1;
        } else if ([arr[mid] integerValue] > number) {
            right = mid - 1;
        }
    }
    return NO;
}

楼梯问题

int stariway(int num) {

    if(num < 1) {
        return 0;
    } else if (num == 1 || num == 2)
        return 1;

    return stariway(num - 1) + stariway(num - 2);
}

int noDiGui(int n) {
    if (n < 0) {
        return 0;
    } else if (n == 1 || n == 2) {
        return 1;
    }
    
    int temp = 0;
    int result = 1;
    int pre = 1;
    for (NSInteger i = 3; i < n; i++) {
        temp = result;
        result = temp + pre;
        pre = temp;
    }
    return  result;
}

你可能感兴趣的:(2020-10-28)