iOS算法

算法

  • 字符串翻转
//实现
void char_reverse(char * cha) {
    char * begain = cha;
    char * end = cha + strlen(cha) - 1;
    while (begain < end) {
        char temp = * begain;
        * (begain++) = * end;
        * (end--) = temp;
    }
}

//调用
char arr[] = "gpf";
char_reverse(arr);
printf("%s   
", arr);

  • 单链表反转
//在ReverseList文件中定义链表结构体
struct Node {
    int data;
    struct Node * next;
};

//调用方法的声明
struct Node * reverseList(struct Node * head);

struct Node * constructList(void);

void printList(struct Node * head);

//调用方法的实现
struct Node * reverseList(struct Node * head)
{
    //定义遍历指针,初始化为投机诶单
    struct Node *p = head;
    //反转后的链表头部
    struct Node *newH = NULL;
    
    //遍历连接
    while (p != NULL) {
        //记录下一个接点
        struct Node * temp = p->next;
        
        //将
        p->next=newH;
        
        newH = 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) {
        printf("node is %d 
", temp->data);
        temp = temp->next;
    }
    
}

//外部测试调用
    struct Node * node = constructList();
    printList(node);
    printf("------ 
");
    struct Node * newHead = reverseList(node);
    printList(newHead);

  • 有序数组合并
//有序数组合并实现
void mergeList(int a[], int aLen, int b[], int bLen, int result[]){
    int q = 0;
    int p = 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 list[5] = {1, 4, 6, 7, 9};
    int list2[8] = {2, 3, 5, 6, 8, 10, 11, 12};
    int result[13];
    mergeList(list, 5, list2, 8, result);
    for (int i = 0; i < 13; i++) {
        printf("%d,", result[i]);
    }

  • hash查找第一个只出现一次的字符
//实现
char findFirstChar(char * cha){
    char result = '';
    unsigned long length = strlen(cha);
    int count[128];
    for (int i = 0; i < 128; i++) {
        count[i] = 0;
    }
    for (int i = 0; i < length; i++) {
        int a;
        char b = cha[i];
        a = b;
        count[a]++;
    }
    
    char * p = cha;
    for (int i = 0; i < 128; i++) {
        if (count[*p] == 1) {
//            printf("第一个出现的字符 %c 
", *p);
            result = *p;
            break;
        }
        p++;
    }
    return result;
}

//调用
	 char cha[] = "dfddfd";
    char result = findFirstChar(cha);
    printf("获取的结果为 %c", result);

原文地址:https://www.cnblogs.com/GoodmorningMr/p/11417057.html