算法

189 阅读1分钟

字符串反转

void char_reverse(char* cha)
{
    char* begin = cha;
    char* end = cha + strlen(cha) - 1;
    
    while (begin < end) {
    	char temp = *begin;
        *(begin++) = *end;
        *(end--) = temp;
    }
}

char ch[] = "hello world";
char_reverse(ch);

链表反转算法

struct Node {
    int data;
    struct Node *next;
};

@interface ReverseList : NSObject

struct Node* reverseList(struct Node *head);
struct Node* constructList(void);
void printList(struct Node *head);

@end


struct Node* reverseList(struct Node *head)
{
    struct Node *p = head;
    // 翻转后的链表头部
    struct Node *newH = NULL;
    
    // 遍历链表
    while (p != NULL) {
        // 记录下一个节点
        struct Node *temp = p->next;
        // 当前节点的next指向新的链表头部
        p->next = newH;
        // 更改新链表头部为当前节点
        newH = p;
        // 移动p指针
        p = temp;
    }
    
    // 返回翻转后的链表头结点
    return newH;
}

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 {
            cur->next = node;
        }
        
        cur = node;
    }
    
    return head;
}

void printList(struct Node *head)
{
    struct Node *temp = head;
    while (temp != NULL) {
        NSLog(@"node is %d \n", temp->data);
        temp = temp->next;
    }
}

struct Node *head = constructList();
printList(head);
struct Node *newHead = reverseList(head);

有序数组合并

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];
            p++;
        } else {
            result[i] = b[q];
            q++;
        }
        
        i++;
    }
    
    
    while (p < aLen) {
        result[i] = a[p++];
        i++;
    }
    
    while (q < bLen) {
        result[i] = b[q++];
        i++;
    }
}

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);

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;
}

char cha[] = "aeaccdeff";
char fc = findFirstChar(cha);

查找子视图的共同父视图

- (NSArray<UIView *> *)findCommonSupperView:(UIView *)view other:(UIView *)viewOther {
    NSMutableArray *result = [NSMutableArray array];
    
    NSArray *array = [self findSuperViews:view];
    NSArray *arrayOther = [self findSuperViews:viewOther];
    
    int i = 0;
    while (i < MIN((int)array.count, (int)arrayOther.count)) {
        UIView *superOne = [array objectAtIndex:array.count -i -1];
        UIView *superOther = [array objectAtIndex:arrayOther.count -i -1];
        
        if (superOne == superOther) {
            [result addObject:superOne];
            i++;
        } else {
            break;
        }
    }
    
    return result;
}

- (NSArray<UIView *> *)findSuperViews:(UIView *)view {
    UIView *temp = view.superview;
    
    NSMutableArray *result = [NSMutableArray array];
    while (temp) {
        [result addObject:temp];
        temp = temp.superview;
    }
    
    return result;
}

无序数组中位数

int findMedian(int a[], int aLen)
{
    int low = 0;
    int high = aLen - 1;
    
    int mid = (aLen - 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[high] >= key) {
            --high;
        }
        
        if (low < high) {
            int temp = a[low];
            a[low] = a[high];
            a[high] = temp;
        }
    }
    
    int temp = a[high];
    a[high] = a[end];
    a[end] = temp;
    
    return low;
}