熟悉常见算法

iOS面试中熟悉常见算法

1、 对以下一组数据进行降序排序(冒泡排序)。“24,17,85,13,9,54,76,45,5,63”


int main(int argc, char *argv[]) {

    int array[10] = {24, 17, 85, 13, 9, 54, 76, 45, 5, 63};

    int num = sizeof(array)/sizeof(int);

    for(int i = 0; i < num-1; i++) {

        for(int j = 0; j < num - 1 - i; j++) {

            if(array[j] < array[j+1]) {

                int tmp = array[j];

                array[j] = array[j+1];

                array[j+1] = tmp;

            }

        }

    }

    for(int i = 0; i < num; i++) {

        printf("%d", array[i]);

        if(i == num-1) {

            printf("\n");

        }

        else {

            printf(" ");

        }

    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

2、 对以下一组数据进行升序排序(选择排序)。“86, 37, 56, 29, 92, 73, 15, 63, 30, 8”


void sort(int a[],int n)
{

    int i, j, index;

    for(i = 0; i < n - 1; i++) {

        index = i;

        for(j = i + 1; j < n; j++) {

            if(a[index] > a[j]) {

                index = j;

            }

        }

        if(index != i) {

            int temp = a[i];

            a[i] = a[index];

            a[index] = temp;

        }

    }

}

int main(int argc, const char * argv[]) {

    int numArr[10] = {86, 37, 56, 29, 92, 73, 15, 63, 30, 8};

    sort(numArr, 10);

    for (int i = 0; i < 10; i++) {

        printf("%d, ", numArr[i]);

    }

    printf("\n");

    return 0;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

3、 快速排序算法


void sort(int *a, int left, int right) {

if(left >= right) {

return ;

}

int i = left;

int j = right;

int key = a[left];

while (i < j) {

while (i < j && key >= a[j]) {

j--;

}

a[i] = a[j];

while (i < j && key <= a[i]) {

    i++;

}

a[j] = a[i];

}

a[i] = key;

sort(a, left, i-1);

sort(a, i+1, right);

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

4、 归并排序


void merge(int sourceArr[], int tempArr[], int startIndex, int midIndex, int endIndex) {

    int i = startIndex;

    int j = midIndex + 1;

    int k = startIndex;

    while (i != midIndex + 1 && j != endIndex + 1) {

        if (sourceArr[i] >= sourceArr[j]) {

            tempArr[k++] = sourceArr[j++];

        } else {

            tempArr[k++] = sourceArr[i++];

        }

    }

    while (i != midIndex + 1) {

        tempArr[k++] = sourceArr[i++];

    }

    while (j != endIndex + 1) {

        tempArr[k++] = sourceArr[j++];

    }

    for (i = startIndex; i <= endIndex; i++) {

        sourceArr[i] = tempArr[i];

    }

}


void sort(int souceArr[], int tempArr[], int startIndex, int endIndex) {

    int midIndex;

    if (startIndex < endIndex) {

        midIndex = (startIndex + endIndex) / 2;

        sort(souceArr, tempArr, startIndex, midIndex);

        sort(souceArr, tempArr, midIndex + 1, endIndex);

        merge(souceArr, tempArr, startIndex, midIndex, endIndex);

    }

}


int main(int argc, const char * argv[]) {

    int numArr[10] = {86, 37, 56, 29, 92, 73, 15, 63, 30, 8};

    int tempArr[10];

    sort(numArr, tempArr, 0, 9);

    for (int i = 0; i < 10; i++) {

        printf("%d, ", numArr[i]);

    }

    printf("\n");

    return 0;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

5、 实现二分查找算法(编程语言不限)


int bsearchWithoutRecursion(int array[],int low,int high,int target) {

while(low <= high) {

int mid = (low + high) / 2;

if(array[mid] > target)

high = mid - 1;

else if(array[mid] < target)

low = mid + 1;

else    //findthetarget

return mid;

}

//the array does not contain the target

return -1;

}

----------------------------------------

递归实现

int binary_search(const int arr[],int low,int high,int key)
{

int mid=low + (high - low) / 2;

if(low > high)

return -1;

else{

if(arr[mid] == key)

return mid;

else if(arr[mid] > key)

return binary_search(arr, low, mid-1, key);

else

return binary_search(arr, mid+1, high, key);

}

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

6、 如何实现链表翻转(链表逆序)? 
思路:每次把第二个元素提到最前面来。


#include <stdio.h>

#include <stdlib.h>


typedef struct NODE {

    struct NODE *next;

    int num;

}node;


node *createLinkList(int length) {

    if (length <= 0) {

        return NULL;

    }

    node *head,*p,*q;

    int number = 1;

    head = (node *)malloc(sizeof(node));

    head->num = 1;

    head->next = head;

    p = q = head;

    while (++number <= length) {

        p = (node *)malloc(sizeof(node));

        p->num = number;

        p->next = NULL;

        q->next = p;

        q = p;

    }

    return head;
}


void printLinkList(node *head) {

    if (head == NULL) {

        return;

    }

    node *p = head;

    while (p) {

        printf("%d ", p->num);

        p = p -> next;

    }

    printf("\n");

}


node *reverseFunc1(node *head) {

    if (head == NULL) {

        return head;


    }


    node *p,*q;

    p = head;

    q = NULL;

    while (p) {

        node *pNext = p -> next;

        p -> next = q;

        q = p;

        p = pNext;

    }

    return q;

}


int main(int argc, const char * argv[]) {

    node *head = createLinkList(7);

    if (head) {

        printLinkList(head);

        node *reHead = reverseFunc1(head);

        printLinkList(reHead);

        free(reHead);

    }

    free(head);

    return 0;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

7、 实现一个字符串“how are you”的逆序输出(编程语言不限)。如给定字符串为“hello world”,输出结果应当为“world hello”。


int spliterFunc(char *p) {

    char c[100][100];

    int i = 0;

    int j = 0;


    while (*p != '\0') {

        if (*p == ' ') {

            i++;

            j = 0;

        } else {

            c[i][j] = *p;

            j++;

        }

        p++;


    }


    for (int k = i; k >= 0; k--) {

        printf("%s", c[k]);

        if (k > 0) {

            printf(" ");

        } else {

            printf("\n");

        }

    }

    return 0;


}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

8、 给定一个字符串,输出本字符串中只出现一次并且最靠前的那个字符的位置?如“abaccddeeef”,字符是b,输出应该是2。


char *strOutPut(char *);


int compareDifferentChar(char, char *);


int main(int argc, const char * argv[]) {


    char *inputStr = "abaccddeeef";

    char *outputStr = strOutPut(inputStr);

    printf("%c \n", *outputStr);

    return 0;

}


char *strOutPut(char *s) {

    char str[100];

    char *p = s;

    int index = 0;

    while (*s != '\0') {

        if (compareDifferentChar(*s, p) == 1) {

            str[index] = *s;

            index++;

        }

        s++;

    }

    return &str;
}


int compareDifferentChar(char c, char *s) {

    int i = 0;

    while (*s != '\0' && i<= 1) {

        if (*s == c) {

            i++;

        }

        s++;
    }

    if (i == 1) {

        return 1;

    } else {

        return 0;

    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

9、 二叉树的先序遍历为FBACDEGH,中序遍历为:ABDCEFGH,请写出这个二叉树的后序遍历结果。

ADECBHGF

  • 先序+中序遍历还原二叉树:先序遍历是:ABDEGCFH 中序遍历是:DBGEACHF

首先从先序得到第一个为A,就是二叉树的根,回到中序,可以将其分为三部分:

左子树的中序序列DBGE,根A,右子树的中序序列CHF

接着将左子树的序列回到先序可以得到B为根,这样回到左子树的中序再次将左子树分割为三部分:

左子树的左子树D,左子树的根B,左子树的右子树GE

同样地,可以得到右子树的根为C

类似地将右子树分割为根C,右子树的右子树HF,注意其左子树为空 

如果只有一个就是叶子不用再进行了,刚才的GE和HF再次这样运作,就可以将二叉树还原了。

10、 打印2-100之间的素数。


int main(int argc, const char * argv[]) {

    for (int i = 2; i < 100; i++) {

        int r = isPrime(i);

        if (r == 1) {

            printf("%ld ", i);

        }

    }

    return 0;

}


int isPrime(int n)
{

    int i, s;

    for(i = 2; i <= sqrt(n); i++)

        if(n % i == 0)  return 0;

    return 1;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

11、 求两个整数的最大公约数。


int gcd(int a, int b) {

    int temp = 0;

    if (a < b) {

        temp = a;

        a = b;

        b = temp;

    }

    while (b != 0) {

        temp = a % b;

        a = b;

        b = temp;

    }

    return a;

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

本文内容中部分来自网络,后续会持续更新完善。欢迎一起学习交流!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 我熟悉的机器学习算法包括: - 线性回归 - 逻辑回归 - 决策树 - 随机森林 - 支持向量机 - K近邻算法 - 朴素贝叶斯 - 神经网络 - 深度学习 - k-means - 聚类 - PCA, LDA - XGBoost - Gradient Boosting Machine - LightGBM - Adaboost, etc. ### 回答2: 我熟悉的机器学习算法包括以下几种: 1.线性回归(Linear Regression):拟合一个线性方程来预测输出变量与输入变量之间的关系。 2.逻辑回归(Logistic Regression):用于分类问题,通过将数据映射到一个概率函数,来预测离散的输出变量。 3.决策树(Decision Tree):通过构建一颗分层决策树,根据特征来预测输出变量。 4.随机森林(Random Forest):由多个决策树组成的集成学习算法,通过投票或平均值来预测输出变量。 5.K近邻算法(K-Nearest Neighbors):通过计算最邻近的K个样本来预测输出变量。 6.支持向量机(Support Vector Machines):通过构建一个超平面来将数据点分为不同的分类,用于分类和回归问题。 7.朴素贝叶斯(Naive Bayes):基于贝叶斯定理,用于分类问题,假设所有特征都是独立的。 8.聚类算法(Clustering):将相似的数据点分组成簇,常见算法包括K均值聚类(K-means clustering)和层次聚类(Hierarchical clustering)。 9.主成分分析(Principal Component Analysis, PCA):用于数据降维,通过将数据投影到新的空间中,保留最重要的特征。 10.神经网络(Neural Networks):通过模拟人脑的神经元之间的连接来学习和预测输出变量。 这些是我所熟悉的一些常见机器学习算法,每个算法都有其独特的应用场景和特点,根据具体问题选择不同的算法能够更好地解决实际挑战。 ### 回答3: 我熟悉的机器学习算法主要包括以下几种: 1.线性回归:通过建立线性模型,预测因变量与自变量之间的关系,常用于回归问题。 2.逻辑回归:用于二分类问题,通过构建逻辑回归模型,预测因变量的概率。 3.决策树:通过树状结构进行分类和回归分析,根据特征的不同判断样本的类别。 4.支持向量机:通过在数据集中找到一个最优超平面,来实现分类或回归任务。 5.朴素贝叶斯:基于贝叶斯定理和特征之间的条件独立性假设,用于文本分类和垃圾邮件过滤等任务。 6.K近邻算法:通过计算样本之间的距离,将测试样本分类为与其距离最近的K个样本类别中占比最多的类别。 7.聚类算法:包括K均值聚类和层次聚类等,将相似样本分为一组。 8.神经网络:模拟人脑神经元的连接方式,通过前向传播和反向传播等方式进行学习。 9.随机森林:利用多个决策树进行分类、回归和特征选择等任务,综合多个模型结果进行综合判断。 以上是我熟悉的一些常见的机器学习算法,它们适用于不同类型的问题和数据集。但对于更复杂的问题,可能需要采用组合不同的算法或使用更高级的算法来解决。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值